org.eclipse.wst.html.core_1.1.502.v201202091936

16:49:00.679 INFO  jd.cli.Main - Decompiling org.eclipse.wst.html.core_1.1.502.v201202091936.jar
package org.eclipse.wst.html.core.internal;

import org.eclipse.core.runtime.Preferences;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNode;
import org.eclipse.wst.xml.core.internal.contentmodel.util.DOMContentBuilderImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class HTMLContentBuilder
  extends DOMContentBuilderImpl
{
  private int fTagCase;
  private int fAttrCase;
  
  public HTMLContentBuilder(Document document)
  {
    super(document);
    Preferences prefs = HTMLCorePlugin.getDefault().getPluginPreferences();
    fTagCase = prefs.getInt("tagNameCase");
    fAttrCase = prefs.getInt("attrNameCase");
  }
  
  public String computeName(CMNode cmnode, Node parent)
  {
    String name = super.computeName(cmnode, parent);
    if (shouldIgnoreCase(cmnode)) {
      if (cmnode.getNodeType() == 5)
      {
        if (fTagCase == 1) {
          name = name.toLowerCase();
        } else if (fTagCase == 2) {
          name = name.toUpperCase();
        }
      }
      else if (cmnode.getNodeType() == 2) {
        if (fAttrCase == 1) {
          name = name.toLowerCase();
        } else if (fAttrCase == 2) {
          name = name.toUpperCase();
        }
      }
    }
    return name;
  }
  
  private boolean shouldIgnoreCase(CMNode cmnode)
  {
    if (!cmnode.supports("shouldIgnoreCase")) {
      return false;
    }
    return ((Boolean)cmnode.getProperty("shouldIgnoreCase")).booleanValue();
  }
}

/* Location:
 * Qualified Name:     org.eclipse.wst.html.core.internal.HTMLContentBuilder
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.wst.html.core.internal;

import org.eclipse.osgi.util.NLS;

public class HTMLCoreMessages
  extends NLS
{
  private static final String BUNDLE_NAME = "org.eclipse.wst.html.core.internal.HTMLCorePluginResources";
  public static String No_error__UI_;
  public static String Undefined_attribute_name___ERROR_;
  public static String Undefined_attribute_value__ERROR_;
  public static String Multiple_values_specified__ERROR_;
  public static String Attribute_name___0___uses__ERROR_;
  public static String Invalid_attribute_name___0_ERROR_;
  public static String Invalid_attribute___0____ERROR_;
  public static String Invalid_location_of_tag____ERROR_;
  public static String Duplicate_tag___0____ERROR_;
  public static String No_start_tag____0_____ERROR_;
  public static String No_end_tag_____0_____ERROR_;
  public static String End_tag_____0____not_neede_ERROR_;
  public static String Unknown_tag___0____ERROR_;
  public static String Tag_name___0___uses_wrong__ERROR_;
  public static String Invalid_tag_name___0____ERROR_;
  public static String Invalid_JSP_directive___0__ERROR_;
  public static String Invalid_text_string___0____ERROR_;
  public static String Invalid_character_used_in__ERROR_;
  public static String Unknown_error__ERROR_;
  public static String Start_tag____0____not_clos_ERROR_;
  public static String End_tag_____0____not_close_ERROR_;
  public static String Attribute_value___0___uses_ERROR_;
  public static String Comment_not_closed__ERROR_;
  public static String DOCTYPE_declaration_not_cl_ERROR_;
  public static String Processing_instruction_not_ERROR_;
  public static String CDATA_section_not_closed__ERROR_;
  public static String _ERROR_Tag___0___should_be_an_empty_element_tag_1;
  public static String _ERROR_Attribute_value___0___not_closed__1;
  public static String HTMLContentPropertiesManager_Updating;
  public static String HTMLContentPropertiesManager_Problems_Updating;
  public static String _ERROR_Resource_not_found_0;
  public static String Obsolete_attribute_name___ERROR_;
  public static String Obsolete_tag___ERROR_;
  public static String Invalid_text_in_tag__ERROR_;
  public static String Missing_attribute_value_equals_ERROR_;
  
  static
  {
    NLS.initializeMessages("org.eclipse.wst.html.core.internal.HTMLCorePluginResources", HTMLCoreMessages.class);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.wst.html.core.internal.HTMLCoreMessages
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.wst.html.core.internal;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.wst.html.core.internal.contentproperties.HTMLContentPropertiesManager;
import org.osgi.framework.BundleContext;

public class HTMLCorePlugin
  extends Plugin
{
  private static HTMLCorePlugin plugin;
  
  public HTMLCorePlugin()
  {
    plugin = this;
  }
  
  public static HTMLCorePlugin getDefault()
  {
    return plugin;
  }
  
  public static IWorkspace getWorkspace()
  {
    return ResourcesPlugin.getWorkspace();
  }
  
  public void start(BundleContext context)
    throws Exception
  {
    super.start(context);
    
    HTMLContentPropertiesManager.startup();
  }
  
  public void stop(BundleContext context)
    throws Exception
  {
    HTMLContentPropertiesManager.shutdown();
    
    super.stop(context);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.wst.html.core.internal.HTMLCorePlugin
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.wst.html.core.internal;

import com.ibm.icu.util.StringTokenizer;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.osgi.framework.Bundle;

public class Logger
{
  private static final String PLUGIN_ID = "org.eclipse.wst.html.core";
  private static final String TRACEFILTER_LOCATION = "/debug/tracefilter";
  public static final int OK = 0;
  public static final int INFO = 1;
  public static final int WARNING = 2;
  public static final int ERROR = 4;
  public static final int OK_DEBUG = 200;
  public static final int INFO_DEBUG = 201;
  public static final int WARNING_DEBUG = 202;
  public static final int ERROR_DEBUG = 204;
  
  protected static void _log(int level, String message, Throwable exception)
  {
    if (((level == 200) || (level == 201) || (level == 202) || (level == 204)) && 
      (!isDebugging())) {
      return;
    }
    int severity = 0;
    switch (level)
    {
    case 1: 
    case 201: 
      severity = 1;
      break;
    case 2: 
    case 202: 
      severity = 2;
      break;
    case 4: 
    case 204: 
      severity = 4;
    }
    message = message != null ? message : "null";
    Status statusObj = new Status(severity, "org.eclipse.wst.html.core", severity, message, exception);
    Bundle bundle = Platform.getBundle("org.eclipse.wst.html.core");
    if (bundle != null) {
      Platform.getLog(bundle).log(statusObj);
    }
  }
  
  protected static void _trace(String category, String message, Throwable exception)
  {
    if (isTracing(category))
    {
      message = message != null ? message : "null";
      Status statusObj = new Status(0, "org.eclipse.wst.html.core", 0, message, exception);
      Bundle bundle = Platform.getBundle("org.eclipse.wst.html.core");
      if (bundle != null) {
        Platform.getLog(bundle).log(statusObj);
      }
    }
  }
  
  public static boolean isDebugging()
  {
    return Platform.inDebugMode();
  }
  
  public static boolean isTracing(String category)
  {
    if (!isDebugging()) {
      return false;
    }
    String traceFilter = Platform.getDebugOption("org.eclipse.wst.html.core/debug/tracefilter");
    if (traceFilter != null)
    {
      StringTokenizer tokenizer = new StringTokenizer(traceFilter, ",");
      while (tokenizer.hasMoreTokens())
      {
        String cat = tokenizer.nextToken().trim();
        if (category.equals(cat)) {
          return true;
        }
      }
    }
    return false;
  }
  
  public static void log(int level, String message)
  {
    _log(level, message, null);
  }
  
  public static void log(int level, String message, Throwable exception)
  {
    _log(level, message, exception);
  }
  
  public static void logException(String message, Throwable exception)
  {
    _log(4, message, exception);
  }
  
  public static void logException(Throwable exception)
  {
    _log(4, exception.getMessage(), exception);
  }
  
  public static void traceException(String category, String message, Throwable exception)
  {
    _trace(category, message, exception);
  }
  
  public static void traceException(String category, Throwable exception)
  {
    _trace(category, exception.getMessage(), exception);
  }
  
  public static void trace(String category, String message)
  {
    _trace(category, message, null);
  }
}

/* Location:
 * Qualified Name:     org.eclipse.wst.html.core.internal.Logger
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.wst.html.core.internal.cleanup;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupHandler;
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupPreferences;
import org.eclipse.wst.sse.core.internal.cleanup.StructuredCleanupPreferences;
import org.eclipse.wst.sse.core.internal.provisional.events.StructuredDocumentEvent;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;

abstract class AbstractNodeCleanupHandler
  implements IStructuredCleanupHandler
{
  protected IStructuredCleanupPreferences fCleanupPreferences = null;
  protected IProgressMonitor fProgressMonitor = null;
  
  public void setCleanupPreferences(IStructuredCleanupPreferences cleanupPreferences)
  {
    fCleanupPreferences = cleanupPreferences;
  }
  
  public IStructuredCleanupPreferences getCleanupPreferences()
  {
    if (fCleanupPreferences == null)
    {
      fCleanupPreferences = new StructuredCleanupPreferences();
      
      Preferences preferences = getModelPreferences();
      if (preferences != null)
      {
        fCleanupPreferences.setTagNameCase(preferences.getInt("cleanupTagNameCase"));
        fCleanupPreferences.setAttrNameCase(preferences.getInt("cleanupAttrNameCase"));
        fCleanupPreferences.setCompressEmptyElementTags(preferences.getBoolean("compressEmptyElementTags"));
        fCleanupPreferences.setInsertRequiredAttrs(preferences.getBoolean("insertRequiredAttrs"));
        fCleanupPreferences.setInsertMissingTags(preferences.getBoolean("insertMissingTags"));
        fCleanupPreferences.setQuoteAttrValues(preferences.getBoolean("quoteAttrValues"));
        fCleanupPreferences.setFormatSource(preferences.getBoolean("formatSource"));
        fCleanupPreferences.setConvertEOLCodes(preferences.getBoolean("convertEOLCodes"));
        fCleanupPreferences.setEOLCode(preferences.getString("cleanupEOLCode"));
      }
    }
    return fCleanupPreferences;
  }
  
  public void setProgressMonitor(IProgressMonitor progressMonitor)
  {
    fProgressMonitor = progressMonitor;
  }
  
  protected static StructuredDocumentEvent replaceSource(IDOMModel model, Object requester, int offset, int length, String source)
  {
    StructuredDocumentEvent result = null;
    if (model == null) {
      return result;
    }
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null) {
      return result;
    }
    if (source == null) {
      source = new String();
    }
    if (structuredDocument.containsReadOnly(offset, length)) {
      return result;
    }
    if (requester == null) {
      requester = structuredDocument;
    }
    return structuredDocument.replaceText(requester, offset, length, source);
  }
  
  protected Preferences getModelPreferences()
  {
    return HTMLCorePlugin.getDefault().getPluginPreferences();
  }
}

/* Location:
 * Qualified Name:     org.eclipse.wst.html.core.internal.cleanup.AbstractNodeCleanupHandler
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.wst.html.core.internal.cleanup;

import org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter;
import org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatterFactory;
import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleSheetAdapter;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupPreferences;
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Node;

public class CSSTextNodeCleanupHandler
  extends AbstractNodeCleanupHandler
{
  public Node cleanup(Node node)
  {
    if (node == null) {
      return node;
    }
    IDOMModel model = ((IDOMNode)node).getModel();
    if (model == null) {
      return node;
    }
    IStructuredDocument structuredDocument = model.getStructuredDocument();
    if (structuredDocument == null) {
      return node;
    }
    if (!getCleanupPreferences().getFormatSource()) {
      return node;
    }
    String content = getCSSContent(node);
    if (content == null) {
      return node;
    }
    int offset = ((IDOMNode)node).getStartOffset();
    int length = ((IDOMNode)node).getEndOffset() - offset;
    replaceSource(model, this, offset, length, content);
    return (IDOMNode)model.getIndexedRegion(offset);
  }
  
  private String getCSSContent(Node text)
  {
    ICSSModel model = getCSSModel(text);
    if (model == null) {
      return null;
    }
    ICSSNode document = model.getDocument();
    if (document == null) {
      return null;
    }
    INodeNotifier notifier = (INodeNotifier)document;
    CSSSourceFormatter formatter = (CSSSourceFormatter)notifier.getAdapterFor(CSSSourceFormatter.class);
    if (formatter == null) {
      formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter(notifier);
    }
    if (formatter == null) {
      return null;
    }
    StringBuffer buffer = formatter.cleanup(document);
    if (buffer == null) {
      return null;
    }
    return buffer.toString();
  }
  
  private ICSSModel getCSSModel(Node text)
  {
    if (text == null) {
      return null;
    }
    INodeNotifier notifier = (INodeNotifier)text.getParentNode();
    if (notifier == null) {
      return null;
    }
    INodeAdapter adapter = notifier.getAdapterFor(IStyleSheetAdapter.class);
    if (adapter == null) {
      return null;
    }
    if (!(adapter instanceof IStyleSheetAdapter)) {
      return null;
    }
    IStyleSheetAdapter styleAdapter = (IStyleSheetAdapter)adapter;
    return styleAdapter.getModel();
  }
}

/* Location:
 * Qualified Name:     org.eclipse.wst.html.core.internal.cleanup.CSSTextNodeCleanupHandler
 * Java Class Version: 1.2 (46.0)
 * JD-Core Version:    0.7.1
 */
package org.eclipse.wst.html.core.internal.cleanup;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatter;
import org.eclipse.wst.css.core.internal.formatter.CSSSourceFormatterFactory;
import org.eclipse.wst.css.core.internal.provisional.adapters.IStyleDeclarationAdapter;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSNode;
import org.eclipse.wst.html.core.internal.Logger;
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupHandler;
import org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupPreferences;
import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
import org.eclipse.wst.sse.core.utils.StringUtils;
import org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
import org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap;
import org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery;
import org.eclipse.wst.xml.core.internal.modelquery.ModelQueryUtil;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.eclipse.wst.xml.core.internal.provisional.document.ISourceGenerator;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ElementNodeCleanupHandler
  extends AbstractNodeCleanupHandler
{
  protected static final String START_TAG_OPEN = "<";
  protected static final String END_TAG_OPEN = "</";
  protected static final String TAG_CLOSE = ">";
  protected static final String EMPTY_TAG_CLOSE = "/>";
  protected static final String SINGLE_QUOTES = "''";
  protected static final String DOUBLE_QUOTES = "\"\"";
  protected static final char SINGLE_QUOTE = '\'';
  protected static final char DOUBLE_QUOTE = '"';
  
  public Node cleanup(Node node)
  {
    IDOMNode renamedNode = (IDOMNode)cleanupChildren(node);
    
    renamedNode = quoteAttrValue(renamedNode);
    if ((!((IDOMElement)renamedNode).isCommentTag()) && (renamedNode.getStartStructuredDocumentRegion() != null))
    {
      IDOMModel structuredModel = renamedNode.getModel();
      
      int startTagStartOffset = renamedNode.getStartOffset();
      
      IStructuredDocumentRegion startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
      insertTagClose(structuredModel, startTagStructuredDocumentRegion);
      
      renamedNode = (IDOMNode)structuredModel.getIndexedRegion(startTagStartOffset);
      startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
      
      IStructuredDocumentRegion endTagStructuredDocumentRegion = renamedNode.getEndStructuredDocumentRegion();
      if (endTagStructuredDocumentRegion != startTagStructuredDocumentRegion) {
        insertTagClose(structuredModel, endTagStructuredDocumentRegion);
      }
    }
    renamedNode = insertMissingTags(renamedNode);
    renamedNode = compressEmptyElementTag(renamedNode);
    renamedNode = insertRequiredAttrs(renamedNode);
    renamedNode = applyTagNameCase(renamedNode);
    applyAttrNameCase(renamedNode);
    cleanupCSSAttrValue(renamedNode);
    
    return renamedNode;
  }
  
  private boolean shouldPreserveCase(IDOMElement element)
  {
    return !element.isGlobalTag();
  }
  
  private boolean isXMLTag(IDOMElement element)
  {
    return element.isXMLTag();
  }
  
  protected void applyAttrNameCase(IDOMNode node)
  {
    IDOMElement element = (IDOMElement)node;
    if (element.isCommentTag()) {
      return;
    }
    int attrNameCase = 0;
    if (!shouldPreserveCase(element)) {
      if (isXMLTag(element)) {
        attrNameCase = 1;
      } else {
        attrNameCase = getCleanupPreferences().getAttrNameCase();
      }
    }
    NamedNodeMap attributes = node.getAttributes();
    int attributesLength = attributes.getLength();
    for (int i = 0; i < attributesLength; i++)
    {
      IDOMNode eachAttr = (IDOMNode)attributes.item(i);
      if (!hasNestedRegion(eachAttr.getNameRegion()))
      {
        String oldAttrName = eachAttr.getNodeName();
        String newAttrName = oldAttrName;
        if (attrNameCase == 1) {
          newAttrName = oldAttrName.toLowerCase(Locale.US);
        } else if (attrNameCase == 2) {
          newAttrName = oldAttrName.toUpperCase(Locale.US);
        }
        if (newAttrName.compareTo(oldAttrName) != 0)
        {
          int attrNameStartOffset = eachAttr.getStartOffset();
          int attrNameLength = oldAttrName.length();
          
          IDOMModel structuredModel = node.getModel();
          IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
          replaceSource(structuredModel, structuredDocument, attrNameStartOffset, attrNameLength, newAttrName);
        }
      }
    }
  }
  
  private boolean hasNestedRegion(ITextRegion container)
  {
    if (!(container instanceof ITextRegionContainer)) {
      return false;
    }
    ITextRegionList regions = ((ITextRegionContainer)container).getRegions();
    if (regions == null) {
      return false;
    }
    return true;
  }
  
  protected IDOMNode applyTagNameCase(IDOMNode node)
  {
    IDOMElement element = (IDOMElement)node;
    if (element.isCommentTag()) {
      return node;
    }
    int tagNameCase = 0;
    if (!shouldPreserveCase(element)) {
      if (isXMLTag(element)) {
        tagNameCase = 1;
      } else {
        tagNameCase = getCleanupPreferences().getTagNameCase();
      }
    }
    String oldTagName = node.getNodeName();
    String newTagName = oldTagName;
    IDOMNode newNode = node;
    if (tagNameCase == 1) {
      newTagName = oldTagName.toLowerCase(Locale.US);
    } else if (tagNameCase == 2) {
      newTagName = oldTagName.toUpperCase(Locale.US);
    }
    IDOMModel structuredModel = node.getModel();
    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
    
    IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
    if (startTagStructuredDocumentRegion != null)
    {
      ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
      if ((regions != null) && (regions.size() > 0))
      {
        ITextRegion startTagNameRegion = regions.get(1);
        int startTagNameStartOffset = startTagStructuredDocumentRegion.getStartOffset(startTagNameRegion);
        int startTagNameLength = startTagStructuredDocumentRegion.getTextEndOffset(startTagNameRegion) - startTagNameStartOffset;
        if (!newTagName.equals(oldTagName)) {
          replaceSource(structuredModel, structuredDocument, startTagNameStartOffset, startTagNameLength, newTagName);
        }
        newNode = (IDOMNode)structuredModel.getIndexedRegion(startTagNameStartOffset);
      }
    }
    IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getEndStructuredDocumentRegion();
    if (endTagStructuredDocumentRegion != null)
    {
      ITextRegionList regions = endTagStructuredDocumentRegion.getRegions();
      if ((regions != null) && (regions.size() > 0))
      {
        ITextRegion endTagNameRegion = regions.get(1);
        int endTagNameStartOffset = endTagStructuredDocumentRegion.getStartOffset(endTagNameRegion);
        int endTagNameLength = endTagStructuredDocumentRegion.getTextEndOffset(endTagNameRegion) - endTagNameStartOffset;
        if ((startTagStructuredDocumentRegion != endTagStructuredDocumentRegion) && (!newTagName.equals(oldTagName))) {
          replaceSource(structuredModel, structuredDocument, endTagNameStartOffset, endTagNameLength, newTagName);
        }
      }
    }
    return newNode;
  }
  
  protected Node cleanupChildren(Node node)
  {
    Node parentNode = node;
    if (node != null)
    {
      Node childNode = node.getFirstChild();
      HTMLCleanupHandlerFactory factory = HTMLCleanupHandlerFactory.getInstance();
      while (childNode != null)
      {
        IStructuredCleanupHandler cleanupHandler = factory.createHandler(childNode, getCleanupPreferences());
        childNode = cleanupHandler.cleanup(childNode);
        
        parentNode = childNode.getParentNode();
        
        childNode = childNode.getNextSibling();
      }
    }
    return parentNode;
  }
  
  protected void cleanupCSSAttrValue(IDOMNode node)
  {
    if ((node == null) || (node.getNodeType() != 1)) {
      return;
    }
    IDOMElement element = (IDOMElement)node;
    if (!element.isGlobalTag()) {
      return;
    }
    Attr attr = element.getAttributeNode("style");
    if (attr == null) {
      return;
    }
    String value = getCSSValue(attr);
    if (value == null) {
      return;
    }
    String oldValue = ((IDOMNode)attr).getValueSource();
    if ((oldValue != null) && (value.equals(oldValue))) {
      return;
    }
    attr.setValue(value);
  }
  
  private ICSSModel getCSSModel(Attr attr)
  {
    if (attr == null) {
      return null;
    }
    INodeNotifier notifier = (INodeNotifier)attr.getOwnerElement();
    if (notifier == null) {
      return null;
    }
    INodeAdapter adapter = notifier.getAdapterFor(IStyleDeclarationAdapter.class);
    if (adapter == null) {
      return null;
    }
    if (!(adapter instanceof IStyleDeclarationAdapter)) {
      return null;
    }
    IStyleDeclarationAdapter styleAdapter = (IStyleDeclarationAdapter)adapter;
    return styleAdapter.getModel();
  }
  
  private String getCSSValue(Attr attr)
  {
    ICSSModel model = getCSSModel(attr);
    if (model == null) {
      return null;
    }
    ICSSNode document = model.getDocument();
    if (document == null) {
      return null;
    }
    INodeNotifier notifier = (INodeNotifier)document;
    CSSSourceFormatter formatter = (CSSSourceFormatter)notifier.getAdapterFor(CSSSourceFormatter.class);
    if (formatter == null) {
      formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter(notifier);
    }
    if (formatter == null) {
      return null;
    }
    StringBuffer buffer = formatter.cleanup(document);
    if (buffer == null) {
      return null;
    }
    return buffer.toString();
  }
  
  private boolean isEmptyElement(IDOMElement element)
  {
    Document document = element.getOwnerDocument();
    if (document == null) {
      return false;
    }
    ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
    if (modelQuery == null) {
      return false;
    }
    CMElementDeclaration decl = modelQuery.getCMElementDeclaration(element);
    if (decl == null) {
      return false;
    }
    return decl.getContentType() == 1;
  }
  
  protected IDOMNode insertEndTag(IDOMNode node)
  {
    IDOMElement element = (IDOMElement)node;
    
    int startTagStartOffset = node.getStartOffset();
    IDOMModel structuredModel = node.getModel();
    IDOMNode newNode = null;
    if (!element.isCommentTag()) {
      if (isEmptyElement(element))
      {
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
        ITextRegionList regions = startStructuredDocumentRegion.getRegions();
        ITextRegion lastRegion = regions.get(regions.size() - 1);
        replaceSource(structuredModel, structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), lastRegion.getLength(), "/>");
        if (regions.size() > 1)
        {
          ITextRegion regionBeforeTagClose = regions.get(regions.size() - 1 - 1);
          if (regionBeforeTagClose.getTextLength() == regionBeforeTagClose.getLength()) {
            replaceSource(structuredModel, structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), 0, " ");
          }
        }
      }
      else
      {
        String tagName = node.getNodeName();
        String endTag = "</".concat(tagName).concat(">");
        
        IDOMNode lastChild = (IDOMNode)node.getLastChild();
        int endTagStartOffset = 0;
        if (lastChild != null) {
          endTagStartOffset = lastChild.getEndOffset();
        } else {
          endTagStartOffset = node.getEndOffset();
        }
        IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
        replaceSource(structuredModel, structuredDocument, endTagStartOffset, 0, endTag);
      }
    }
    newNode = (IDOMNode)structuredModel.getIndexedRegion(startTagStartOffset);
    
    return newNode;
  }
  
  protected IDOMNode insertMissingTags(IDOMNode node)
  {
    boolean insertMissingTags = getCleanupPreferences().getInsertMissingTags();
    IDOMNode newNode = node;
    if (insertMissingTags)
    {
      IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
      if (startTagStructuredDocumentRegion == null)
      {
        newNode = insertStartTag(node);
        startTagStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
      }
      IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getEndStructuredDocumentRegion();
      
      ITextRegionList regionList = startTagStructuredDocumentRegion.getRegions();
      if ((startTagStructuredDocumentRegion == null) || (regionList == null) || (regionList.get(regionList.size() - 1).getType() != "XML_EMPTY_TAG_CLOSE")) {
        if (startTagStructuredDocumentRegion == null)
        {
          if (isStartTagRequired(newNode)) {
            newNode = insertStartTag(newNode);
          }
        }
        else if (endTagStructuredDocumentRegion == null) {
          if (isEndTagRequired(newNode)) {
            newNode = insertEndTag(newNode);
          }
        }
      }
    }
    return newNode;
  }
  
  protected IDOMNode insertStartTag(IDOMNode node)
  {
    IDOMElement element = (IDOMElement)node;
    if (element.isCommentTag()) {
      return node;
    }
    IDOMNode newNode = null;
    
    String tagName = node.getNodeName();
    String startTag = "<".concat(tagName).concat(">");
    int startTagStartOffset = node.getStartOffset();
    
    IDOMModel structuredModel = node.getModel();
    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
    replaceSource(structuredModel, structuredDocument, startTagStartOffset, 0, startTag);
    newNode = (IDOMNode)structuredModel.getIndexedRegion(startTagStartOffset);
    
    return newNode;
  }
  
  protected void insertTagClose(IDOMModel structuredModel, IStructuredDocumentRegion flatNode)
  {
    if ((flatNode != null) && (flatNode.getRegions() != null))
    {
      ITextRegionList regionList = flatNode.getRegions();
      ITextRegion lastRegion = regionList.get(regionList.size() - 1);
      if (lastRegion != null)
      {
        String regionType = lastRegion.getType();
        if ((regionType != "XML_EMPTY_TAG_CLOSE") && (regionType != "XML_TAG_CLOSE"))
        {
          IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
          
          replaceSource(structuredModel, structuredDocument, flatNode.getTextEndOffset(lastRegion), 0, ">");
        }
      }
    }
  }
  
  protected boolean isEndTagRequired(IDOMNode node)
  {
    if (node == null) {
      return false;
    }
    return node.isContainer();
  }
  
  protected boolean isStartTagRequired(IDOMNode node)
  {
    if (node == null) {
      return false;
    }
    return node.isContainer();
  }
  
  protected boolean isXMLType(IDOMModel structuredModel)
  {
    boolean result = false;
    if (structuredModel != null)
    {
      IDOMDocument document = structuredModel.getDocument();
      if (document != null) {
        result = document.isXMLType();
      }
    }
    return result;
  }
  
  protected IDOMNode quoteAttrValue(IDOMNode node)
  {
    IDOMElement element = (IDOMElement)node;
    if (element.isCommentTag()) {
      return node;
    }
    boolean quoteAttrValues = getCleanupPreferences().getQuoteAttrValues();
    IDOMNode newNode = node;
    if (quoteAttrValues)
    {
      NamedNodeMap attributes = newNode.getAttributes();
      int attributesLength = attributes.getLength();
      ISourceGenerator generator = node.getModel().getGenerator();
      for (int i = 0; i < attributesLength; i++)
      {
        attributes = newNode.getAttributes();
        attributesLength = attributes.getLength();
        IDOMAttr eachAttr = (IDOMAttr)attributes.item(i);
        
        String oldAttrValue = eachAttr.getValueRegionText();
        if (oldAttrValue == null)
        {
          IDOMModel structuredModel = node.getModel();
          if (isXMLType(structuredModel))
          {
            String newAttrValue = "=\"" + eachAttr.getNameRegionText() + "\"";
            
            IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
            replaceSource(structuredModel, structuredDocument, eachAttr.getNameRegionEndOffset(), 0, newAttrValue);
            newNode = (IDOMNode)structuredModel.getIndexedRegion(node.getStartOffset());
          }
        }
        else
        {
          char quote = StringUtils.isQuoted(oldAttrValue) ? oldAttrValue.charAt(0) : '"';
          String newAttrValue = generator.generateAttrValue(eachAttr, quote);
          if (oldAttrValue.length() == 1)
          {
            char firstChar = oldAttrValue.charAt(0);
            if (firstChar == '\'') {
              newAttrValue = "''";
            } else if (firstChar == '"') {
              newAttrValue = "\"\"";
            }
          }
          if ((newAttrValue != null) && 
            (newAttrValue.compareTo(oldAttrValue) != 0))
          {
            int attrValueStartOffset = eachAttr.getValueRegionStartOffset();
            int attrValueLength = oldAttrValue.length();
            int startTagStartOffset = node.getStartOffset();
            
            IDOMModel structuredModel = node.getModel();
            IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
            replaceSource(structuredModel, structuredDocument, attrValueStartOffset, attrValueLength, newAttrValue);
            newNode = (IDOMNode)structuredModel.getIndexedRegion(startTagStartOffset);
          }
        }
      }
    }
    return newNode;
  }
  
  private IDOMNode insertRequiredAttrs(IDOMNode node)
  {
    boolean insertRequiredAttrs = getCleanupPreferences().getInsertRequiredAttrs();
    IDOMNode newNode = node;
    if (insertRequiredAttrs)
    {
      List requiredAttrs = getRequiredAttrs(newNode);
      if (requiredAttrs.size() > 0)
      {
        NamedNodeMap currentAttrs = node.getAttributes();
        List insertAttrs = new ArrayList();
        if (currentAttrs.getLength() == 0) {
          insertAttrs.addAll(requiredAttrs);
        } else {
          for (int i = 0; i < requiredAttrs.size(); i++)
          {
            String requiredAttrName = ((CMAttributeDeclaration)requiredAttrs.get(i)).getAttrName();
            boolean found = false;
            for (int j = 0; j < currentAttrs.getLength(); j++)
            {
              String currentAttrName = currentAttrs.item(j).getNodeName();
              if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0)
              {
                found = true;
                break;
              }
            }
            if (!found) {
              insertAttrs.add(requiredAttrs.get(i));
            }
          }
        }
        if (insertAttrs.size() > 0)
        {
          IStructuredDocumentRegion startStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
          int index = startStructuredDocumentRegion.getEndOffset();
          ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
          if (lastRegion.getType() == "XML_TAG_CLOSE")
          {
            index--;
            lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
          }
          else if (lastRegion.getType() == "XML_EMPTY_TAG_CLOSE")
          {
            index -= 2;
            lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
          }
          MultiTextEdit multiTextEdit = new MultiTextEdit();
          try
          {
            for (int i = insertAttrs.size() - 1; i >= 0; i--)
            {
              CMAttributeDeclaration attrDecl = (CMAttributeDeclaration)insertAttrs.get(i);
              String requiredAttributeName = attrDecl.getAttrName();
              String defaultValue = attrDecl.getDefaultValue();
              if (defaultValue == null) {
                defaultValue = "";
              }
              String nameAndDefaultValue = " ";
              if ((i == 0) && (lastRegion.getLength() > lastRegion.getTextLength())) {
                nameAndDefaultValue = "";
              }
              nameAndDefaultValue = nameAndDefaultValue + requiredAttributeName + "=\"" + defaultValue + "\"";
              multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
            }
            multiTextEdit.apply(newNode.getStructuredDocument());
          }
          catch (BadLocationException e)
          {
            Logger.log(1, e.getMessage());
          }
        }
      }
    }
    return newNode;
  }
  
  protected ModelQuery getModelQuery(Node node)
  {
    ModelQuery result = null;
    if (node.getNodeType() == 9) {
      result = ModelQueryUtil.getModelQuery((Document)node);
    } else {
      result = ModelQueryUtil.getModelQuery(node.getOwnerDocument());
    }
    return result;
  }
  
  protected List getRequiredAttrs(Node node)
  {
    List result = new ArrayList();
    
    ModelQuery modelQuery = getModelQuery(node);
    if (modelQuery != null)
    {
      CMElementDeclaration elementDecl = modelQuery.getCMElementDeclaration((Element)node);
      if (elementDecl != null)
      {
        CMNamedNodeMap attrMap = elementDecl.getAttributes();
        Iterator it = attrMap.iterator();
        CMAttributeDeclaration attr = null;
        while (it.hasNext())
        {
          attr = (CMAttributeDeclaration)it.next();
          if (attr.getUsage() == 2) {
            result.add(attr);
          }
        }
      }
    }
    return result;
  }
  
  private IDOMNode compressEmptyElementTag(IDOMNode node)
  {
    boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
    IDOMNode newNode = node;
    
    IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
    IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();
    if ((compressEmptyElementTags) && (startTagStructuredDocumentRegion != endTagStructuredDocumentRegion) && 
      (startTagStructuredDocumentRegion != null)) {
      if ((isXMLTag((IDOMElement)newNode)) || (!newNode.isContainer()))
      {
        ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
        ITextRegion lastRegion = regions.get(regions.size() - 1);
        if (lastRegion.getType() != "XML_EMPTY_TAG_CLOSE")
        {
          NodeList childNodes = newNode.getChildNodes();
          if ((childNodes == null) || (childNodes.getLength() == 0) || ((childNodes.getLength() == 1) && (childNodes.item(0).getNodeType() == 3) && (childNodes.item(0).getNodeValue().trim().length() == 0)))
          {
            IDOMModel structuredModel = newNode.getModel();
            IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
            
            int startTagStartOffset = newNode.getStartOffset();
            int offset = endTagStructuredDocumentRegion.getStart();
            int length = endTagStructuredDocumentRegion.getLength();
            structuredDocument.repla
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd