Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format')
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/EmbeddedCSSFormatter.java94
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLElementFormatter.java394
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatContraintsImpl.java42
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatProcessorImpl.java61
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatter.java826
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatterFactory.java109
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormattingUtil.java90
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLTextFormatter.java299
-rw-r--r--bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/SpaceConverter.java235
9 files changed, 0 insertions, 2150 deletions
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/EmbeddedCSSFormatter.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/EmbeddedCSSFormatter.java
deleted file mode 100644
index 74ec679e83..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/EmbeddedCSSFormatter.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-
-
-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.html.core.internal.provisional.HTMLFormatContraints;
-import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter;
-import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
-
-// nakamori_TODO: check and remove
-
-public class EmbeddedCSSFormatter extends HTMLFormatter {
-
- // private IAdapterFactory factory = new
- // CSSSourceFormatterFactory(CSSSourceFormatter.class, true);
- /**
- */
- protected EmbeddedCSSFormatter() {
- super();
- }
-
- /**
- */
- protected void formatNode(IDOMNode node, HTMLFormatContraints contraints) {
- if (node == null)
- return;
- IDOMText text = (IDOMText) node;
-
- String source = getCSSContent(node);
- if (source == null) { // fallback
- source = text.getSource();
- }
-
- int offset = text.getStartOffset();
- int length = text.getEndOffset() - offset;
- replaceSource(text.getModel(), offset, length, source);
- setWidth(contraints, source);
- }
-
- /**
- */
- private String getCSSContent(IDOMNode 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);
- // try another way to get formatter
- if (formatter == null)
- formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter(notifier);
- if (formatter == null)
- return null;
- StringBuffer buffer = formatter.format(document);
- if (buffer == null)
- return null;
- return buffer.toString();
- }
-
- /**
- */
- private ICSSModel getCSSModel(IDOMNode 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();
- }
-} \ No newline at end of file
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLElementFormatter.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLElementFormatter.java
deleted file mode 100644
index ce14df4cac..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLElementFormatter.java
+++ /dev/null
@@ -1,394 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2008 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-import java.util.Iterator;
-
-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.provisional.HTMLFormatContraints;
-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.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.xml.core.internal.provisional.document.IDOMElement;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
-import org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML;
-import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
-import org.w3c.dom.Attr;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-
-// nakamori_TODO: check and remove CSS formatting
-
-public class HTMLElementFormatter extends HTMLFormatter {
-
- /**
- */
- protected HTMLElementFormatter() {
- super();
- }
-
- /**
- */
- private void compressTailingSpaces(IStructuredDocumentRegion flatNode, ITextRegion region) {
- int offset = region.getTextEnd();
- int count = region.getEnd() - offset;
- if (count == 1) {
- String source = flatNode.getFullText(region);
- int start = region.getStart();
- if (source != null && source.charAt(offset - start) == ' ') {
- // nothing to do
- return;
- }
- }
- replaceSource(flatNode, offset, count, " ");//$NON-NLS-1$
- }
-
- /**
- */
- private void formatEndTag(IDOMElement element, HTMLFormatContraints contraints) {
- Node lastChild = element.getLastChild();
-
- if (lastChild != null && lastChild instanceof IDOMElement && lastChild.getNodeName().equals("jsp:scriptlet")) { //$NON-NLS-1$
- insertBreakAfter((IDOMElement) lastChild, contraints);
- return;
- }
-
-
- IStructuredDocumentRegion endStructuredDocumentRegion = element.getEndStructuredDocumentRegion();
- if (endStructuredDocumentRegion == null)
- return;
-
- if (element.isJSPTag() || element.isCommentTag()) {
- String endTag = endStructuredDocumentRegion.getText();
- if (endTag != null && endTag.length() > 0) {
- setWidth(contraints, endTag);
- }
- return;
- }
-
- ITextRegion prevRegion = null;
- ITextRegionList regions = endStructuredDocumentRegion.getRegions();
- Iterator e = regions.iterator();
- while (e.hasNext()) {
- ITextRegion region = (ITextRegion) e.next();
- if (region == null)
- continue;
- String regionType = region.getType();
- if (regionType == DOMRegionContext.XML_TAG_NAME || isNestedTag(regionType)) {
- if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_END_TAG_OPEN) {
- removeTailingSpaces(endStructuredDocumentRegion, prevRegion);
- }
- }
- else if (regionType == DOMRegionContext.XML_TAG_CLOSE) {
- if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_NAME || isNestedRootTag(prevRegion.getType()))) {
- removeTailingSpaces(endStructuredDocumentRegion, prevRegion);
- }
- }
- prevRegion = region;
- }
- if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_NAME || isNestedRootTag(prevRegion.getType()))) {
- removeTailingSpaces(endStructuredDocumentRegion, prevRegion);
- }
-
- // BUG123890 (end tag length was already prefactored into
- // formatStartTag so no need to do it here)
- // String newEndTag = endStructuredDocumentRegion.getText();
- // if (newEndTag != null && newEndTag.length() > 0) {
- // setWidth(contraints, newEndTag);
- // }
- }
-
- /**
- */
- protected void formatNode(IDOMNode node, HTMLFormatContraints contraints) {
- if (node == null)
- return;
- IDOMElement element = (IDOMElement) node;
-
- formatStartTag(element, contraints);
-
- formatChildNodes(element, contraints);
-
- formatEndTag(element, contraints);
- }
-
- /**
- */
- private void formatStartTag(IDOMElement element, HTMLFormatContraints contraints) {
-
- if (element.getNodeName().equals("jsp:scriptlet")) { //$NON-NLS-1$
- insertBreakBefore(element, contraints);
- return;
- }
-
- IStructuredDocumentRegion startStructuredDocumentRegion = element.getStartStructuredDocumentRegion();
- if (startStructuredDocumentRegion == null)
- return;
-
- // We should format attributes in JSPTag?
- // if (element.isJSPTag() || element.isCommentTag()) {
- if (element.isCommentTag()) {
- String startTag = startStructuredDocumentRegion.getText();
- if (startTag != null && startTag.length() > 0) {
- setWidth(contraints, startTag);
- }
- return;
- }
-
- // first process style attribute
- if (element.isGlobalTag()) {
- Attr attr = element.getAttributeNode("style");//$NON-NLS-1$
- if (attr != null)
- formatStyleAttr(attr);
- }
- boolean insertBreak = false;
- insertBreak = ((StructuredFormatPreferencesXML) getFormatPreferences()).getSplitMultiAttrs();
- boolean alignEndBracket = ((StructuredFormatPreferencesXML) getFormatPreferences()).isAlignEndBracket();
- boolean attributesSplitted = false;
-
- if (insertBreak) {
- NamedNodeMap attributes = element.getAttributes();
- if (attributes == null || attributes.getLength() < 2)
- insertBreak = false;
- }
- String breakSpaces = getBreakSpaces(element);
- String originalBreakSpaces = breakSpaces;
- String indent = getIndent();
- if (indent != null && indent.length() > 0) {
- breakSpaces += indent;
- }
- ITextRegion lastBreakRegion = null;
-
- ITextRegion prevRegion = null;
- ITextRegionList regions = startStructuredDocumentRegion.getRegions();
- Iterator e = regions.iterator();
- while (e.hasNext()) {
- ITextRegion region = (ITextRegion) e.next();
- if (region == null)
- continue;
-
- ITextRegion breakRegion = null;
-
- String regionType = region.getType();
- if (regionType == DOMRegionContext.XML_TAG_NAME || isNestedTag(regionType)) {
- if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_TAG_OPEN) {
- removeTailingSpaces(startStructuredDocumentRegion, prevRegion);
- }
- breakRegion = region;
- }
- else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
- if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS)) {
- // attribute name without value
- breakRegion = prevRegion;
- }
- }
- else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
- if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
- removeTailingSpaces(startStructuredDocumentRegion, prevRegion);
- }
- }
- else if (regionType == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE) {
- if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS) {
- removeTailingSpaces(startStructuredDocumentRegion, prevRegion);
- }
- breakRegion = region;
- }
- else if (regionType == DOMRegionContext.XML_TAG_CLOSE || regionType == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
- if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS)) {
- // attribute name without value
- breakRegion = prevRegion;
- }
- }
-
- if (breakRegion != null) {
- int end = breakRegion.getTextEnd();
- if (lastBreakRegion != null) {
- int offset = lastBreakRegion.getEnd();
- int count = end - offset;
- if (insertBreak || !isWidthAvailable(contraints, count + 1)) {
- replaceTailingSpaces(startStructuredDocumentRegion, lastBreakRegion, breakSpaces);
- setWidth(contraints, breakSpaces);
- attributesSplitted = true;
- }
- else {
- compressTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
- addWidth(contraints, 1);
- }
- addWidth(contraints, count);
- }
- else {
- addWidth(contraints, end);
- }
- lastBreakRegion = breakRegion;
- }
-
- prevRegion = region;
- }
- if (prevRegion != null && (prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME || prevRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS)) {
- // attribute name without value
- int end = prevRegion.getTextEnd();
- if (lastBreakRegion != null) {
- int offset = lastBreakRegion.getEnd();
- int count = end - offset;
- if (insertBreak || !isWidthAvailable(contraints, count + 1)) {
- replaceTailingSpaces(startStructuredDocumentRegion, lastBreakRegion, breakSpaces);
- setWidth(contraints, breakSpaces);
- attributesSplitted = true;
- }
- else {
- compressTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
- addWidth(contraints, 1);
- }
- addWidth(contraints, count);
- }
- else {
- addWidth(contraints, end);
- }
- lastBreakRegion = prevRegion;
- }
-
- if (lastBreakRegion != null) {
- int offset = lastBreakRegion.getTextEnd();
- int count = startStructuredDocumentRegion.getLength() - offset;
- if (prevRegion != null && prevRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
- compressTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
- count++;
- }
- else {
- removeTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
- // BUG123890 (pre-factor in end tag)
- count += element.getTagName().length() + 3;
- }
- addWidth(contraints, count);
- }
- else {
- addWidth(contraints, startStructuredDocumentRegion.getLength());
- }
- // BUG113584 - align last bracket
- if (alignEndBracket && attributesSplitted) {
- removeTailingSpaces(startStructuredDocumentRegion, lastBreakRegion);
- replaceTailingSpaces(startStructuredDocumentRegion, lastBreakRegion, originalBreakSpaces);
- contraints.setAvailableLineWidth(getLineWidth() - originalBreakSpaces.length() - 1);
- }
- }
-
- /**
- * ISSUE: this is a bit of hidden JSP knowledge that was implemented this
- * way for expedency. Should be evolved in future to depend on
- * "nestedContext".
- */
- private boolean isNestedTag(String regionType) {
- final String JSP_ROOT_TAG_NAME = "JSP_ROOT_TAG_NAME"; //$NON-NLS-1$
- final String JSP_DIRECTIVE_NAME = "JSP_DIRECTIVE_NAME"; //$NON-NLS-1$
- boolean result = regionType.equals(JSP_ROOT_TAG_NAME) || regionType.equals(JSP_DIRECTIVE_NAME);
- return result;
- }
-
- /**
- * ISSUE: this is a bit of hidden JSP knowledge that was implemented this
- * way for expedency. Should be evolved in future to depend on
- * "nestedContext".
- */
- private boolean isNestedRootTag(String regionType) {
- final String JSP_ROOT_TAG_NAME = "JSP_ROOT_TAG_NAME"; //$NON-NLS-1$
- boolean result = regionType.equals(JSP_ROOT_TAG_NAME);
- return result;
- }
-
-
- /**
- */
- private void formatStyleAttr(Attr attr) {
- if (attr == null)
- return;
- // if someone's made it a container somehow, CSS can't format it
- if (((IDOMNode) attr).getValueRegion() instanceof ITextRegionContainer)
- 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);
- // try another way to get formatter
- if (formatter == null)
- formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter(notifier);
- if (formatter == null)
- return null;
- StringBuffer buffer = formatter.format(document);
- if (buffer == null)
- return null;
- return buffer.toString();
- }
-
- /**
- */
- private void removeTailingSpaces(IStructuredDocumentRegion flatNode, ITextRegion region) {
- int offset = region.getTextEnd();
- int count = region.getEnd() - offset;
- if (count <= 0)
- return;
- replaceSource(flatNode, offset, count, null);
- }
-
- /**
- */
- private void replaceTailingSpaces(IStructuredDocumentRegion flatNode, ITextRegion region, String spaces) {
- int offset = region.getTextEnd();
- int count = region.getEnd() - offset;
- if (count == spaces.length()) {
- String source = flatNode.getFullText(region);
- if (source != null && source.endsWith(spaces)) {
- // nothing to do
- return;
- }
- }
- replaceSource(flatNode, offset, count, spaces);
- }
-} \ No newline at end of file
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatContraintsImpl.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatContraintsImpl.java
deleted file mode 100644
index bf81533ea5..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatContraintsImpl.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2005 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-import org.eclipse.wst.html.core.internal.provisional.HTMLFormatContraints;
-import org.eclipse.wst.sse.core.internal.format.StructuredFormatContraints;
-
-/**
- * @deprecated
- * Please un-deprecate this if Page Designer thinks they are needed.
- */
-public class HTMLFormatContraintsImpl extends StructuredFormatContraints implements HTMLFormatContraints {
- protected int fAvailableLineWidth;
-
- /**
- * @deprecated
- * It's very hard to keep the available line width accurate.
- * Sometimes a node wants to start on a new line, sometimes it doesn't.
- * It's best for the node to figure out the available line width on the fly.
- */
- public int getAvailableLineWidth() {
- return fAvailableLineWidth;
- }
-
- /**
- * @deprecated
- * It's very hard to keep the available line width accurate.
- * Sometimes a node wants to start on a new line, sometimes it doesn't.
- * It's best for the node to figure out the available line width on the fly.
- */
- public void setAvailableLineWidth(int availableLineWidth) {
- fAvailableLineWidth = availableLineWidth;
- }
-}
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatProcessorImpl.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatProcessorImpl.java
deleted file mode 100644
index 0552bc2d5e..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatProcessorImpl.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-import org.eclipse.core.runtime.Preferences;
-import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
-import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
-import org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences;
-import org.eclipse.wst.sse.core.internal.format.IStructuredFormatter;
-import org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML;
-import org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML;
-import org.w3c.dom.Node;
-
-public class HTMLFormatProcessorImpl extends FormatProcessorXML {
- protected String getFileExtension() {
- return "html"; //$NON-NLS-1$
- }
-
- protected IStructuredFormatter getFormatter(Node node) {
- IStructuredFormatter formatter = HTMLFormatterFactory.getInstance().createFormatter(node, getFormatPreferences());
-
- return formatter;
- }
-
- public IStructuredFormatPreferences getFormatPreferences() {
- if (fFormatPreferences == null) {
- fFormatPreferences = new StructuredFormatPreferencesXML();
-
- Preferences preferences = HTMLCorePlugin.getDefault().getPluginPreferences();
- if (preferences != null) {
- fFormatPreferences.setLineWidth(preferences.getInt(HTMLCorePreferenceNames.LINE_WIDTH));
- ((StructuredFormatPreferencesXML) fFormatPreferences).setSplitMultiAttrs(preferences.getBoolean(HTMLCorePreferenceNames.SPLIT_MULTI_ATTRS));
- ((StructuredFormatPreferencesXML) fFormatPreferences).setAlignEndBracket(preferences.getBoolean(HTMLCorePreferenceNames.ALIGN_END_BRACKET));
- fFormatPreferences.setClearAllBlankLines(preferences.getBoolean(HTMLCorePreferenceNames.CLEAR_ALL_BLANK_LINES));
-
- char indentChar = ' ';
- String indentCharPref = preferences.getString(HTMLCorePreferenceNames.INDENTATION_CHAR);
- if (HTMLCorePreferenceNames.TAB.equals(indentCharPref)) {
- indentChar = '\t';
- }
- int indentationWidth = preferences.getInt(HTMLCorePreferenceNames.INDENTATION_SIZE);
-
- StringBuffer indent = new StringBuffer();
- for (int i = 0; i < indentationWidth; i++) {
- indent.append(indentChar);
- }
- fFormatPreferences.setIndent(indent.toString());
- }
- }
-
- return fFormatPreferences;
- }
-} \ No newline at end of file
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatter.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatter.java
deleted file mode 100644
index 07fb2060a9..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatter.java
+++ /dev/null
@@ -1,826 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2011 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- * Genuitec - fix for bug #203252
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.Preferences;
-import org.eclipse.jface.text.BadLocationException;
-import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
-import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
-import org.eclipse.wst.html.core.internal.provisional.HTMLCMProperties;
-import org.eclipse.wst.html.core.internal.provisional.HTMLFormatContraints;
-import org.eclipse.wst.sse.core.internal.format.IStructuredFormatContraints;
-import org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences;
-import org.eclipse.wst.sse.core.internal.format.IStructuredFormatter;
-import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
-import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
-import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
-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.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.format.StructuredFormatPreferencesXML;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.Text;
-
-public class HTMLFormatter implements IStructuredFormatter {
-
- private static final String HTML_NAME = "html";//$NON-NLS-1$
- private static final String HEAD_NAME = "head"; //$NON-NLS-1$
- private static final String BODY_NAME = "BODY";//$NON-NLS-1$
- // hidden jsp logic that should be removed when jsp formatter is created
- private static final String JSP = "jsp";//$NON-NLS-1$
-
- private HTMLFormattingUtil formattingUtil;
-
- public HTMLFormatter() {
- formattingUtil = new HTMLFormattingUtil();
- }
-
- /**
- */
- protected void addWidth(HTMLFormatContraints contraints, int width) {
- if (contraints == null)
- return;
- if (!splitLines() || getLineWidth() < 0)
- return;
-
- int availableWidth = contraints.getAvailableLineWidth() - width;
- if (availableWidth < 0)
- availableWidth = 0;
- contraints.setAvailableLineWidth(availableWidth);
- }
-
- /**
- */
- protected boolean canFormatChild(Node node) {
- while (node != null) {
- if (node.getNodeType() != Node.ELEMENT_NODE)
- return true;
- CMElementDeclaration decl = getElementDeclaration((Element) node);
- if (decl != null) {
- if (decl.getContentType() == CMElementDeclaration.CDATA)
- return false;
- if (decl.supports(HTMLCMProperties.SHOULD_KEEP_SPACE)) {
- boolean shouldKeepSpace = ((Boolean) decl.getProperty(HTMLCMProperties.SHOULD_KEEP_SPACE)).booleanValue();
- if (shouldKeepSpace)
- return false;
- }
- }
- node = node.getParentNode();
- }
- return false;
- }
-
- /**
- */
- protected boolean canInsertBreakAfter(CMElementDeclaration decl) {
- if (decl == null)
- return false;
- if (!decl.supports(HTMLCMProperties.LINE_BREAK_HINT))
- return false;
- String hint = (String) decl.getProperty(HTMLCMProperties.LINE_BREAK_HINT);
- if (hint == null)
- return false;
- return (hint.equals(HTMLCMProperties.Values.BREAK_BEFORE_START_AND_AFTER_END) || hint.equals(HTMLCMProperties.Values.BREAK_AFTER_START));
- }
-
- /**
- */
- protected boolean canInsertBreakAfter(Node node) {
- if (node == null)
- return false;
- Node parent = node.getParentNode();
- if (parent == null)
- return false;
- Node next = node.getNextSibling();
-
- // special exception if this node is a non-HTML tag (like JSP
- // elements)
- // BUG188093 - only preserve whitespace for jsp (not custom) tags
- String prefix = node.getPrefix();
- if (prefix != null && JSP.equals(prefix)) {
- boolean canInsertBreakAfter = false;
- // if a whitespace does not exist after it, do not add one
- if (next != null && next.getNodeType() == Node.TEXT_NODE) {
- String theText = ((Text) next).getData();
- if (theText != null && theText.length() > 0) {
- char theChar = theText.charAt(0);
- canInsertBreakAfter = Character.isWhitespace(theChar);
- }
- }
- // if cannot insert break, go ahead and return false (otherwise,
- // continue processing)
- if (!canInsertBreakAfter)
- return false;
- }
-
- // special exception if next node is a non-HTML tag (like JSP
- // elements)
- // BUG188093 - only preserve whitespace for jsp (not custom) tags
- if (next != null) {
- prefix = next.getPrefix();
- if (prefix != null && JSP.equals(prefix)) {
- boolean canInsertBreakAfterPrevious = false;
- // if a whitespace does not exist before it, do not add one
- if (node.getNodeType() == Node.TEXT_NODE) {
- String theText = ((Text) node).getData();
- if (theText != null && theText.length() > 0) {
- char theChar = theText.charAt(theText.length() - 1);
- canInsertBreakAfterPrevious = Character.isWhitespace(theChar);
- }
- }
- // if cannot insert break, go ahead and return false
- // (otherwise,
- // continue processing)
- if (!canInsertBreakAfterPrevious)
- return false;
- }
- }
- if (parent.getNodeType() == Node.DOCUMENT_NODE) {
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- // do not insert break after unclosed tag
- if (!((IDOMElement) node).isClosed())
- return false;
- }
- return true;
- }
- else if (parent.getNodeType() == Node.ELEMENT_NODE) {
- IDOMElement element = (IDOMElement) parent;
- // do not insert break before missing end tag
- if (next == null && element.getEndStructuredDocumentRegion() == null)
- return false;
-
- // insert line break under non-HTML elements including JSP
- // elements
- if (element.getPrefix() != null)
- return true;
-
- CMElementDeclaration decl = getElementDeclaration(element);
- if (decl != null) {
- if (decl.getContentType() == CMElementDeclaration.ELEMENT)
- return true;
- // causes all closing tags to wrap to a new line
- boolean allowsText = decl.getContentType() == CMElementDeclaration.MIXED
- || decl.getContentType() == CMElementDeclaration.PCDATA;
- if (allowsNewlineAfter(allowsText, node, element))
- return true;
- String tagName = element.getTagName();
- // special for direct children under BODY
- if (tagName != null && tagName.equalsIgnoreCase(BODY_NAME))
- return true;
- }
- }
-
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- IDOMElement element = (IDOMElement) node;
- CMElementDeclaration decl = getElementDeclaration(element);
- if (canInsertBreakAfter(decl)) {
- // spcial for BR
- return canFormatChild(parent);
- }
- }
- if (next != null && next.getNodeType() == Node.ELEMENT_NODE) {
- CMElementDeclaration decl = getElementDeclaration((Element) next);
- if (canInsertBreakBefore(decl))
- return true;
- }
- return false;
- }
-
- /**
- */
- protected boolean canInsertBreakBefore(CMElementDeclaration decl) {
- if (decl == null)
- return false;
- if (!decl.supports(HTMLCMProperties.LINE_BREAK_HINT))
- return false;
- String hint = (String) decl.getProperty(HTMLCMProperties.LINE_BREAK_HINT);
- if (hint == null)
- return false;
- return hint.equals(HTMLCMProperties.Values.BREAK_BEFORE_START_AND_AFTER_END);
- }
-
- /**
- */
- protected boolean canInsertBreakBefore(Node node) {
- if (node == null)
- return false;
- Node parent = node.getParentNode();
- if (parent == null)
- return false;
- Node prev = node.getPreviousSibling();
-
- // special exception if this node is a non-HTML tag (like JSP
- // elements)
- // BUG188093 - only preserve whitespace for jsp (not custom) tags
- String prefix = node.getPrefix();
- if (prefix != null && JSP.equals(prefix)) {
- boolean canInsertBreakBefore = false;
- // if a whitespace does not exist before it, do not add one
- if (prev != null && prev.getNodeType() == Node.TEXT_NODE) {
- String theText = ((Text) prev).getData();
- if (theText != null && theText.length() > 0) {
- char theChar = theText.charAt(theText.length() - 1);
- canInsertBreakBefore = Character.isWhitespace(theChar);
- }
- }
- // if cannot insert break, go ahead and return false (otherwise,
- // continue processing)
- if (!canInsertBreakBefore)
- return false;
- }
-
- // special exception if previous node is a non-HTML tag (like JSP
- // elements)
- // BUG188093 - only preserve whitespace for jsp (not custom) tags
- if (prev != null) {
- prefix = prev.getPrefix();
- if (prefix != null && JSP.equals(prefix)) {
- boolean canInsertBreakBeforeNext = false;
- // if a whitespace does not exist after it, do not add one
- if (node.getNodeType() == Node.TEXT_NODE) {
- String theText = ((Text) node).getData();
- if (theText != null && theText.length() > 0) {
- char theChar = theText.charAt(0);
- canInsertBreakBeforeNext = Character.isWhitespace(theChar);
- }
- }
- // if cannot insert break, go ahead and return false
- // (otherwise,
- // continue processing)
- if (!canInsertBreakBeforeNext)
- return false;
- }
- }
-
- if (parent.getNodeType() == Node.DOCUMENT_NODE) {
- if (prev == null)
- return false;
- return true;
- }
- else if (parent.getNodeType() == Node.ELEMENT_NODE) {
- IDOMElement element = (IDOMElement) parent;
- // do not insert break after missing start tag
- if (prev == null && element.getStartStructuredDocumentRegion() == null)
- return false;
-
- // insert line break under non-HTML elements including JSP
- // elements
- if (element.getPrefix() != null)
- return true;
-
- CMElementDeclaration decl = getElementDeclaration(element);
- if (decl != null) {
- return allowNewlineBefore(node, element);
- }
- }
-
- if (node.getNodeType() == Node.ELEMENT_NODE) {
- return true;
- }
- if (prev != null && prev.getNodeType() == Node.ELEMENT_NODE) {
- CMElementDeclaration decl = getElementDeclaration((Element) prev);
- if (canInsertBreakAfter(decl)) {
- // spcial for BR
- return canFormatChild(parent);
- }
- }
- return false;
- }
-
- /**
- */
- public void format(Node node) {
- format(node, getFormatContraints());
- }
-
- /**
- */
- public void format(Node node, IStructuredFormatContraints contraints) {
- if (node instanceof IDOMNode && contraints instanceof HTMLFormatContraints)
- format((IDOMNode) node, (HTMLFormatContraints) contraints);
- }
-
- public void format(IDOMNode node, HTMLFormatContraints contraints) {
- if (node == null)
- return;
- if (node.getParentNode() == null)
- return; // do not format removed node
-
- setWidth(contraints, node);
-
- if (canInsertBreakBefore(node))
- insertBreakBefore(node, contraints);
-
- formatNode(node, contraints);
-
- if (canInsertBreakAfter(node))
- insertBreakAfter(node, contraints);
- }
-
- /**
- */
- protected void formatChildNodes(IDOMNode node, HTMLFormatContraints contraints) {
- if (node == null)
- return;
- if (!node.hasChildNodes())
- return;
-
- // concat adjacent texts
- node.normalize();
-
- // disable sibling indent during formatting all the children
- boolean indent = false;
- if (contraints != null) {
- indent = contraints.getFormatWithSiblingIndent();
- contraints.setFormatWithSiblingIndent(false);
- }
-
- boolean insertBreak = true;
- IDOMNode child = (IDOMNode) node.getFirstChild();
- while (child != null) {
- if (child.getParentNode() != node)
- break;
- IDOMNode next = (IDOMNode) child.getNextSibling();
-
- if (insertBreak && canInsertBreakBefore(child)) {
- insertBreakBefore(child, contraints);
- }
-
- IStructuredFormatter formatter = HTMLFormatterFactory.getInstance().createFormatter(child, getFormatPreferences());
- if (formatter != null) {
- if (formatter instanceof HTMLFormatter) {
- HTMLFormatter htmlFormatter = (HTMLFormatter) formatter;
- htmlFormatter.formatNode(child, contraints);
- }
- else {
- formatter.format(child);
- }
- }
-
- if (canInsertBreakAfter(child)) {
- insertBreakAfter(child, contraints);
- insertBreak = false; // not to insert twice
- }
- else {
- insertBreak = true;
- }
-
- child = next;
- }
-
- if (contraints != null)
- contraints.setFormatWithSiblingIndent(indent);
- }
-
- /**
- */
- protected void formatNode(IDOMNode node, HTMLFormatContraints contraints) {
- if (node == null)
- return;
-
- if (node.hasChildNodes()) { // container
- formatChildNodes(node, contraints);
- }
- else { // leaf
- IStructuredDocumentRegion flatNode = node.getStartStructuredDocumentRegion();
- if (flatNode != null) {
- String source = flatNode.getText();
- if (source != null && source.length() > 0) {
- setWidth(contraints, source);
- }
- }
- }
- }
-
- /**
- */
- protected String getBreakSpaces(Node node) {
- if (node == null)
- return null;
- StringBuffer buffer = new StringBuffer();
-
- String delim = ((IDOMNode) node).getModel().getStructuredDocument().getLineDelimiter();
- if (delim != null && delim.length() > 0)
- buffer.append(delim);
-
- String indent = getIndent();
- if (indent != null && indent.length() > 0) {
- for (Node parent = node.getParentNode(); parent != null; parent = parent.getParentNode()) {
- if (parent.getNodeType() != Node.ELEMENT_NODE)
- break;
- // ignore omitted tag
- if (((IDOMNode) parent).getStartStructuredDocumentRegion() == null)
- continue;
-
- IDOMElement element = (IDOMElement) parent;
- if (element.getPrefix() != null) {
- String localName = element.getLocalName();
- // special for html:html
- if (localName != null && !localName.equals(HTML_NAME)) {
- buffer.append(indent);
- }
- continue;
- }
- else {
- String localName = element.getLocalName();
- if (HTML_NAME.equalsIgnoreCase(localName) || HEAD_NAME.equalsIgnoreCase(localName))
- break;
- }
-
- CMElementDeclaration decl = getElementDeclaration(element);
- if (decl != null && decl.supports(HTMLCMProperties.SHOULD_INDENT_CHILD_SOURCE)) {
- boolean shouldIndent = isIdentable(node, parent);
- if (shouldIndent)
- buffer.append(indent);
- }
-
- }
- }
-
- return buffer.toString();
- }
-
- /**
- */
- protected String getIndent() {
- return getFormatPreferences().getIndent();
- }
-
- /**
- */
- protected int getLineWidth() {
- return getFormatPreferences().getLineWidth();
- }
-
- /**
- */
- protected CMElementDeclaration getElementDeclaration(Element element) {
- if (element == null)
- return null;
- Document document = element.getOwnerDocument();
- if (document == null)
- return null;
- ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
- if (modelQuery == null)
- return null;
- return modelQuery.getCMElementDeclaration(element);
- }
-
- /**
- */
- protected void insertBreakAfter(IDOMNode node, HTMLFormatContraints contraints) {
- if (node == null)
- return;
- if (node.getNodeType() == Node.TEXT_NODE)
- return;
- // don't insert break if node is on the last line
- int documentLength = node.getStructuredDocument().getLength();
- if (documentLength < 1 || (node.getEndOffset() >= (documentLength - 1)))
- return;
- Node parent = node.getParentNode();
- if (parent == null)
- return;
- Node next = node.getNextSibling();
-
- String spaces = null;
- if (next == null) { // last spaces
- // use parent indent for the end tag
- spaces = getBreakSpaces(parent);
- }
- else if (next.getNodeType() == Node.TEXT_NODE) {
- if (contraints != null && contraints.getFormatWithSiblingIndent()) {
- IDOMNode text = (IDOMNode) next;
- IStructuredFormatter formatter = HTMLFormatterFactory.getInstance().createFormatter(text, getFormatPreferences());
- if (formatter instanceof HTMLTextFormatter) {
- HTMLTextFormatter textFormatter = (HTMLTextFormatter) formatter;
- textFormatter.formatText(text, contraints, HTMLTextFormatter.FORMAT_HEAD);
- }
- }
- return;
- }
- else {
- spaces = getBreakSpaces(node);
- }
- if (spaces == null || spaces.length() == 0)
- return;
-
- replaceSource(node.getModel(), node.getEndOffset(), 0, spaces);
- setWidth(contraints, spaces);
- }
-
- /**
- */
- protected void insertBreakBefore(IDOMNode node, HTMLFormatContraints contraints) {
- if (node == null)
- return;
- if (node.getNodeType() == Node.TEXT_NODE)
- return;
- Node parent = node.getParentNode();
- if (parent == null)
- return;
- Node prev = node.getPreviousSibling();
-
- String spaces = null;
- if (prev != null && prev.getNodeType() == Node.TEXT_NODE) {
- if (contraints != null && contraints.getFormatWithSiblingIndent()) {
- IDOMNode text = (IDOMNode) prev;
- IStructuredFormatter formatter = HTMLFormatterFactory.getInstance().createFormatter(text, getFormatPreferences());
- if (formatter instanceof HTMLTextFormatter) {
- HTMLTextFormatter textFormatter = (HTMLTextFormatter) formatter;
- textFormatter.formatText(text, contraints, HTMLTextFormatter.FORMAT_TAIL);
- }
- }
- return;
- }
- else {
- spaces = getBreakSpaces(node);
- }
- if (spaces == null || spaces.length() == 0)
- return;
-
- replaceSource(node.getModel(), node.getStartOffset(), 0, spaces);
- setWidth(contraints, spaces);
- }
-
- /**
- */
- protected boolean isWidthAvailable(HTMLFormatContraints contraints, int width) {
- if (contraints == null)
- return true;
- if (!splitLines() || getLineWidth() < 0)
- return true;
- return (contraints.getAvailableLineWidth() >= width);
- }
-
- /**
- */
- protected boolean keepBlankLines(HTMLFormatContraints contraints) {
- if (contraints == null)
- return true;
- return (!contraints.getClearAllBlankLines());
- }
-
- /**
- */
- protected void replaceSource(IStructuredDocumentRegion flatNode, int offset, int length, String source) {
- if (flatNode == null)
- return;
- IStructuredDocument structuredDocument = flatNode.getParentDocument();
- if (structuredDocument == null)
- return;
- if (source == null)
- source = new String();
- int startOffset = flatNode.getStartOffset();
- if (structuredDocument.containsReadOnly(startOffset + offset, length))
- return;
- // We use 'structuredDocument' as the requester object just so this
- // and the other
- // format-related 'repalceText' (in replaceSource) can use the same
- // requester.
- // Otherwise, if requester is not identical,
- // the undo group gets "broken" into multiple pieces based
- // on the requesters being different. Technically, any unique, common
- // requester object would work.
- structuredDocument.replaceText(structuredDocument, startOffset + offset, length, source);
- }
-
- /**
- */
- protected void replaceSource(IDOMModel model, int offset, int length, String source) {
- if (model == null)
- return;
- IStructuredDocument structuredDocument = model.getStructuredDocument();
- if (structuredDocument == null)
- return;
- if (source == null)
- source = new String();
- if (structuredDocument.containsReadOnly(offset, length))
- return;
- // We use 'structuredDocument' as the requester object just so this
- // and the other
- // format-related 'repalceText' (in replaceSource) can use the same
- // requester.
- // Otherwise, if requester is not identical,
- // the undo group gets "broken" into multiple pieces based
- // on the requesters being different. Technically, any unique, common
- // requester object would work.
- structuredDocument.replaceText(structuredDocument, offset, length, source);
- }
-
- /**
- */
- protected void setWidth(HTMLFormatContraints contraints, String source) {
- if (contraints == null)
- return;
- if (source == null)
- return;
- int length = source.length();
- if (length == 0)
- return;
-
- if (!splitLines())
- return;
- int lineWidth = getLineWidth();
- if (lineWidth < 0)
- return;
-
- int offset = source.lastIndexOf('\n');
- int offset2 = source.lastIndexOf('\r');
- if (offset2 > offset)
- offset = offset2;
- if (offset >= 0)
- offset++;
-
- int availableWidth = 0;
- if (offset >= 0) {
- availableWidth = lineWidth - (length - offset);
- }
- else {
- availableWidth = contraints.getAvailableLineWidth() - length;
- }
- if (availableWidth < 0)
- availableWidth = 0;
- contraints.setAvailableLineWidth(availableWidth);
- }
-
- /**
- */
- protected void setWidth(HTMLFormatContraints contraints, Node node) {
- if (contraints == null)
- return;
- if (node == null)
- return;
- IStructuredDocument structuredDocument = ((IDOMNode) node).getStructuredDocument();
- if (structuredDocument == null)
- return; // error
-
- if (!splitLines())
- return;
- int lineWidth = getLineWidth();
- if (lineWidth < 0)
- return;
-
- int offset = ((IDOMNode) node).getStartOffset();
- int line = structuredDocument.getLineOfOffset(offset);
- int lineOffset = 0;
- try {
- lineOffset = structuredDocument.getLineOffset(line);
- }
- catch (BadLocationException ex) {
- return; // error
- }
- if (lineOffset > offset)
- return; // error
-
- int availableWidth = lineWidth - (offset - lineOffset);
- if (availableWidth < 0)
- availableWidth = 0;
-
- contraints.setAvailableLineWidth(availableWidth);
- }
-
- /**
- */
- protected boolean splitLines() {
- return true;// getFormatPreferences().getSplitLines();
- }
-
- protected IStructuredFormatPreferences fFormatPreferences = null;
- protected HTMLFormatContraints fFormatContraints = null;
- protected IProgressMonitor fProgressMonitor = null;
-
- // public void format(XMLNode node, FormatContraints formatContraints) {
- // if (formatContraints.getFormatWithSiblingIndent())
- // formatContraints.setCurrentIndent(getSiblingIndent(node));
- //
- // formatNode(node, formatContraints);
- // }
-
- public void setFormatPreferences(IStructuredFormatPreferences formatPreferences) {
- fFormatPreferences = formatPreferences;
- }
-
- public IStructuredFormatPreferences getFormatPreferences() {
- if (fFormatPreferences == null) {
- fFormatPreferences = new StructuredFormatPreferencesXML();
-
- Preferences preferences = HTMLCorePlugin.getDefault().getPluginPreferences();
- if (preferences != null) {
- fFormatPreferences.setLineWidth(preferences.getInt(HTMLCorePreferenceNames.LINE_WIDTH));
- ((StructuredFormatPreferencesXML) fFormatPreferences).setSplitMultiAttrs(preferences.getBoolean(HTMLCorePreferenceNames.SPLIT_MULTI_ATTRS));
- ((StructuredFormatPreferencesXML) fFormatPreferences).setAlignEndBracket(preferences.getBoolean(HTMLCorePreferenceNames.ALIGN_END_BRACKET));
- fFormatPreferences.setClearAllBlankLines(preferences.getBoolean(HTMLCorePreferenceNames.CLEAR_ALL_BLANK_LINES));
-
- char indentChar = ' ';
- String indentCharPref = preferences.getString(HTMLCorePreferenceNames.INDENTATION_CHAR);
- if (HTMLCorePreferenceNames.TAB.equals(indentCharPref)) {
- indentChar = '\t';
- }
- int indentationWidth = preferences.getInt(HTMLCorePreferenceNames.INDENTATION_SIZE);
-
- StringBuffer indent = new StringBuffer();
- for (int i = 0; i < indentationWidth; i++) {
- indent.append(indentChar);
- }
- fFormatPreferences.setIndent(indent.toString());
- }
- }
-
- return fFormatPreferences;
- }
-
- public IStructuredFormatContraints getFormatContraints() {
- if (fFormatContraints == null) {
- fFormatContraints = new HTMLFormatContraintsImpl();
-
- fFormatContraints.setAvailableLineWidth(getFormatPreferences().getLineWidth());
- fFormatContraints.setClearAllBlankLines(getFormatPreferences().getClearAllBlankLines());
- }
-
- return fFormatContraints;
- }
-
- public void setProgressMonitor(IProgressMonitor progressMonitor) {
- fProgressMonitor = progressMonitor;
- }
-
- /* Check to see if current text Node is a child of an inline element. */
- public boolean isInlinableTextNode(Node theNode, Element theParentElement) {
- return formattingUtil.isInline(theParentElement) &&
- theNode.getNodeType() == Node.TEXT_NODE;
- }
-
- public boolean allowsNewlineAfter(boolean theBool, Node theNode, Element theParentElement) {
- boolean result = theBool;
- if ((theNode.getNodeType() == Node.TEXT_NODE) && formattingUtil.isInline(theParentElement)) {
- result = false;
- } else if (theNode.getNodeType() == Node.ELEMENT_NODE
- && formattingUtil.isInline(theNode.getNextSibling())) {
- result = false;
- }
- else if (theNode.getNodeType() == Node.TEXT_NODE) {
- Node next = theNode.getNextSibling();
- if (next != null && formattingUtil.isInline(next) || theParentElement.getChildNodes().getLength() <= 1) {
- result = false;
- }
- }
- return result;
- }
-
- public boolean allowNewlineBefore(Node theNode) {
- if (theNode.getNodeType() != Node.TEXT_NODE &&
- theNode.getNodeType() != Node.ELEMENT_NODE) return false;
- return (formattingUtil.isInline(theNode.getParentNode()) ||
- formattingUtil.isInline(theNode.getPreviousSibling()));
- }
-
- public boolean allowNewlineBefore(Node theNode, Element theParentElement) {
- boolean result = true;
- /* The calling method canInsertBreakBefore is checking if you can
- * insert a line break after the text node in the parentElement. We
- * need to check for the case with inline element because we don't want to
- * break before the closing </tag> */
- if (isInlinableTextNode(theNode, theParentElement)) {
- result = false;
- /* Check to see if we need to not break the line because we are
- * a child of a inline element or a next sibling to an inline element*/
- } else if (allowNewlineBefore(theNode)) {
- result = false;
- } else if (theNode.getNodeType() == Node.TEXT_NODE && theParentElement.getChildNodes().getLength() <= 1) {
- result = false;
- }
- return result;
- }
-
- public boolean isIdentable(Node theNode, Node theParent) {
- boolean result = true;
- /* The first 2 cases where we don't want to break/indent or if the
- * node is a inlineText ELement or if we should skip it before its parent
- * is an inlineText element.
- * The last check is to make sure that the parent is actually the parent
- * of the node. This method is called when the formatter is formatting
- * the startTag and the wrap margin causes attributes to be indents on
- * mulitple lines. In this case where the parentNode doesn't match
- * theParent argument, we can allow the indent. */
- if (formattingUtil.isInline(theNode) &&
- formattingUtil.shouldSkipIndentForNode(theNode) &&
- theParent == theNode.getParentNode()) {
- result = false;
- }
- return result;
- }
-} \ No newline at end of file
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatterFactory.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatterFactory.java
deleted file mode 100644
index d34701a590..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatterFactory.java
+++ /dev/null
@@ -1,109 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-import org.eclipse.core.runtime.Preferences;
-import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
-import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
-import org.eclipse.wst.sse.core.internal.format.IStructuredFormatPreferences;
-import org.eclipse.wst.sse.core.internal.format.IStructuredFormatter;
-import org.eclipse.wst.xml.core.internal.provisional.format.StructuredFormatPreferencesXML;
-import org.w3c.dom.Node;
-
-// nakamori_TODO: check and remove CSS formatting
-
-class HTMLFormatterFactory {
- private static HTMLFormatterFactory fInstance = null;
- protected StructuredFormatPreferencesXML fFormatPreferences = null;
-
- static synchronized HTMLFormatterFactory getInstance() {
- if (fInstance == null) {
- fInstance = new HTMLFormatterFactory();
- }
- return fInstance;
- }
-
- protected IStructuredFormatter createFormatter(Node node, IStructuredFormatPreferences formatPreferences) {
- IStructuredFormatter formatter = null;
-
- switch (node.getNodeType()) {
- case Node.ELEMENT_NODE :
- formatter = new HTMLElementFormatter();
- break;
- case Node.TEXT_NODE :
- if (isEmbeddedCSS(node)) {
- formatter = new EmbeddedCSSFormatter();
- }
- else {
- formatter = new HTMLTextFormatter();
- }
- break;
- default :
- formatter = new HTMLFormatter();
- break;
- }
-
- // init FormatPreferences
- formatter.setFormatPreferences(formatPreferences);
-
- return formatter;
- }
-
- /**
- */
- private boolean isEmbeddedCSS(Node node) {
- if (node == null)
- return false;
- Node parent = node.getParentNode();
- if (parent == null)
- return false;
- if (parent.getNodeType() != Node.ELEMENT_NODE)
- return false;
- String name = parent.getNodeName();
- if (name == null)
- return false;
- return name.equalsIgnoreCase("STYLE");//$NON-NLS-1$
- }
-
-
- private HTMLFormatterFactory() {
- super();
- }
-
- protected StructuredFormatPreferencesXML getFormatPreferences() {
- if (fFormatPreferences == null) {
- fFormatPreferences = new StructuredFormatPreferencesXML();
-
- Preferences preferences = HTMLCorePlugin.getDefault().getPluginPreferences();
- if (preferences != null) {
- fFormatPreferences.setLineWidth(preferences.getInt(HTMLCorePreferenceNames.LINE_WIDTH));
- fFormatPreferences.setSplitMultiAttrs(preferences.getBoolean(HTMLCorePreferenceNames.SPLIT_MULTI_ATTRS));
- fFormatPreferences.setAlignEndBracket(preferences.getBoolean(HTMLCorePreferenceNames.ALIGN_END_BRACKET));
- fFormatPreferences.setClearAllBlankLines(preferences.getBoolean(HTMLCorePreferenceNames.CLEAR_ALL_BLANK_LINES));
-
- char indentChar = ' ';
- String indentCharPref = preferences.getString(HTMLCorePreferenceNames.INDENTATION_CHAR);
- if (HTMLCorePreferenceNames.TAB.equals(indentCharPref)) {
- indentChar = '\t';
- }
- int indentationWidth = preferences.getInt(HTMLCorePreferenceNames.INDENTATION_SIZE);
-
- StringBuffer indent = new StringBuffer();
- for (int i = 0; i < indentationWidth; i++) {
- indent.append(indentChar);
- }
- fFormatPreferences.setIndent(indent.toString());
- }
- }
-
- return fFormatPreferences;
- }
-} \ No newline at end of file
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormattingUtil.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormattingUtil.java
deleted file mode 100644
index 9a2b000632..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormattingUtil.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2010 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-import java.util.HashSet;
-import java.util.Locale;
-import java.util.Set;
-
-import org.eclipse.wst.html.core.internal.HTMLCorePlugin;
-import org.eclipse.wst.html.core.internal.preferences.HTMLCorePreferenceNames;
-import org.w3c.dom.Node;
-
-import com.ibm.icu.util.StringTokenizer;
-
-public class HTMLFormattingUtil {
-
- private Set fInlineElements;
-
- public HTMLFormattingUtil() {
- fInlineElements = getInlineSet();
- }
-
- public boolean isInline(Node node) {
- return node != null && fInlineElements.contains(node.getNodeName().toLowerCase(Locale.US));
- }
-
- public boolean shouldSkipIndentForNode(Node node) {
- return isInline(node.getParentNode());
- }
-
- /**
- * Returns an array of the element names considered as "inline" for the purposes of formatting.
- * This list represents those stored in the preference store when invoked.
- *
- * @return An array of element names considered to be "inline"
- */
- public static Object[] getInlineElements() {
- return getInlineSet().toArray();
- }
-
- /**
- * Stores the element names to the preference store to be considered as "inline"
- *
- * @param elements The element names considered to be "inline"
- */
- public static void exportToPreferences(Object[] elements) {
- if (elements != null) {
- StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < elements.length; i++) {
- if (i > 0)
- buffer.append(',');
- buffer.append(elements[i]);
- }
- HTMLCorePlugin.getDefault().getPluginPreferences().setValue(HTMLCorePreferenceNames.INLINE_ELEMENTS, buffer.toString());
- }
- }
-
- /**
- * Returns an array of the default element names considered as "inline" for the purposes of formatting.
- *
- * @return An array of the default element names considered to be "inline"
- */
- public static Object[] getDefaultInlineElements() {
- String inline = HTMLCorePlugin.getDefault().getPluginPreferences().getDefaultString(HTMLCorePreferenceNames.INLINE_ELEMENTS);
- Set defaults = new HashSet();
- StringTokenizer tokenizer = new StringTokenizer(inline, ",");
- while (tokenizer.hasMoreTokens()) {
- defaults.add(tokenizer.nextToken());
- }
- return defaults.toArray();
- }
-
- private static Set getInlineSet() {
- String inline = HTMLCorePlugin.getDefault().getPluginPreferences().getString(HTMLCorePreferenceNames.INLINE_ELEMENTS);
- Set elements = new HashSet();
- StringTokenizer tokenizer = new StringTokenizer(inline, ",");
- while (tokenizer.hasMoreTokens()) {
- elements.add(tokenizer.nextToken());
- }
- return elements;
- }
-}
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLTextFormatter.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLTextFormatter.java
deleted file mode 100644
index 91aefd3c17..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLTextFormatter.java
+++ /dev/null
@@ -1,299 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2005 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-
-
-import org.eclipse.wst.html.core.internal.provisional.HTMLCMProperties;
-import org.eclipse.wst.html.core.internal.provisional.HTMLFormatContraints;
-import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
-import org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
-import org.eclipse.wst.xml.core.internal.provisional.document.IDOMText;
-import org.eclipse.wst.xml.core.internal.regions.DOMRegionContext;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-
-public class HTMLTextFormatter extends HTMLFormatter {
-
- public static int FORMAT_ALL = 0;
- public static int FORMAT_HEAD = 1;
- public static int FORMAT_TAIL = 2;
-
- /**
- */
- protected HTMLTextFormatter() {
- super();
- }
-
- /**
- */
- private boolean canFormatText(IDOMText text) {
- if (text == null)
- return false;
-
- IStructuredDocumentRegion flatNode = text.getFirstStructuredDocumentRegion();
- if (flatNode != null) {
- String type = flatNode.getType();
- if (isUnparsedRegion(type))
- return false;
- }
-
- Node parent = text.getParentNode();
- if (parent != null && parent.getNodeType() == Node.ELEMENT_NODE) {
- IDOMElement element = (IDOMElement) parent;
- if (!element.isGlobalTag() && !text.isElementContentWhitespace())
- return false;
- }
-
- return canFormatChild(parent);
- }
-
- private boolean isUnparsedRegion(String type) {
- boolean result = isNestedScannedRegion(type) || isBlockScannedRegion(type);
- return result;
- }
-
- private boolean isBlockScannedRegion(String type) {
- return type == DOMRegionContext.BLOCK_TEXT;
- }
-
- /**
- * ISSUE: this is a bit of hidden JSP knowledge that was implemented this
- * way for expedency. Should be evolved in future to depend on
- * "nestedContext".
- */
- private boolean isNestedScannedRegion(String type) {
- final String JSP_CONTENT = "JSP_CONTENT"; //$NON-NLS-1$
- return type.equals(JSP_CONTENT);
- }
-
- /**
- */
- private boolean canRemoveHeadingSpaces(IDOMNode node) {
- if (node == null)
- return false;
- if (node.getPreviousSibling() != null)
- return false;
- Node parent = node.getParentNode();
- if (parent == null || parent.getNodeType() != Node.ELEMENT_NODE)
- return false;
-
- CMElementDeclaration decl = getElementDeclaration((Element) parent);
- if (decl == null || (!decl.supports(HTMLCMProperties.LINE_BREAK_HINT)))
- return false;
- String hint = (String) decl.getProperty(HTMLCMProperties.LINE_BREAK_HINT);
- return hint.equals(HTMLCMProperties.Values.BREAK_BEFORE_START_AND_AFTER_END);
- }
-
- /**
- */
- private boolean canRemoveTailingSpaces(IDOMNode node) {
- if (node == null)
- return false;
- if (node.getNextSibling() != null)
- return false;
- Node parent = node.getParentNode();
- if (parent == null || parent.getNodeType() != Node.ELEMENT_NODE)
- return false;
-
- CMElementDeclaration decl = getElementDeclaration((Element) parent);
- if (decl == null || (!decl.supports(HTMLCMProperties.LINE_BREAK_HINT)))
- return false;
- String hint = (String) decl.getProperty(HTMLCMProperties.LINE_BREAK_HINT);
- return hint.equals(HTMLCMProperties.Values.BREAK_BEFORE_START_AND_AFTER_END);
- }
-
- /**
- */
- protected void formatNode(IDOMNode node, HTMLFormatContraints contraints) {
- formatText(node, contraints, FORMAT_ALL); // full format
- }
-
- /**
- */
- protected void formatText(IDOMNode node, HTMLFormatContraints contraints, int mode) {
- if (node == null)
- return;
- Node parent = node.getParentNode();
- if (parent == null)
- return;
-
- IDOMText text = (IDOMText) node;
- String source = text.getSource();
-
- if (!canFormatText(text)) {
- setWidth(contraints, source);
- return;
- }
-
- int offset = text.getStartOffset();
- int length = text.getEndOffset() - offset;
-
- // format adjacent text at once
- if (mode == FORMAT_HEAD) {
- Node next = node.getNextSibling();
- while (next != null && next.getNodeType() == Node.TEXT_NODE) {
- IDOMText nextText = (IDOMText) next;
- length += (nextText.getEndOffset() - nextText.getStartOffset());
- String nextSource = nextText.getSource();
- if (nextSource != null && nextSource.length() > 0) {
- if (source == null)
- source = nextSource;
- else
- source += nextSource;
- }
- next = next.getNextSibling();
- }
- }
- else if (mode == FORMAT_TAIL) {
- Node prev = node.getPreviousSibling();
- while (prev != null && prev.getNodeType() == Node.TEXT_NODE) {
- IDOMText prevText = (IDOMText) prev;
- offset = prevText.getStartOffset();
- length += (prevText.getEndOffset() - offset);
- String prevSource = prevText.getSource();
- if (prevSource != null && prevSource.length() > 0) {
- if (source == null)
- source = prevSource;
- else
- source = prevSource + source;
- }
- prev = prev.getPreviousSibling();
- }
- }
-
- SpaceConverter converter = new SpaceConverter(source, keepBlankLines(contraints));
-
- int wordLength = converter.nextWord();
- if (wordLength == 0) { // only spaces
- if (!converter.hasSpaces())
- return; // empty
- boolean removeSpaces = false;
- if (parent.getNodeType() == Node.ELEMENT_NODE) {
- // check if tags are omitted
- IDOMNode element = (IDOMNode) parent;
- if (node.getPreviousSibling() == null && element.getStartStructuredDocumentRegion() == null) {
- removeSpaces = true;
- }
- else if (node.getNextSibling() == null && element.getEndStructuredDocumentRegion() == null) {
- removeSpaces = true;
- }
- }
- if (removeSpaces) {
- converter.replaceSpaces(null);
- }
- else if (!isWidthAvailable(contraints, 2) || canInsertBreakAfter(node) || canInsertBreakBefore(node)) {
- String spaces = null;
- if (node.getNextSibling() == null) { // last spaces
- // use parent indent for the end tag
- spaces = getBreakSpaces(parent);
- }
- else {
- spaces = getBreakSpaces(node);
- }
- converter.replaceSpaces(spaces);
- setWidth(contraints, spaces);
- }
- else if (canRemoveHeadingSpaces(node) || canRemoveTailingSpaces(node)) {
- converter.replaceSpaces(null);
- }
- else {
- converter.compressSpaces();
- addWidth(contraints, 1);
- }
- }
- else {
- String breakSpaces = null;
-
- // format heading spaces
- boolean hasSpaces = converter.hasSpaces();
- if (mode == FORMAT_TAIL) {
- // keep spaces as is
- addWidth(contraints, converter.getSpaceCount());
- }
- else if ((hasSpaces && !isWidthAvailable(contraints, wordLength + 1)) || canInsertBreakBefore(node)) {
- breakSpaces = getBreakSpaces(node);
- converter.replaceSpaces(breakSpaces);
- setWidth(contraints, breakSpaces);
- }
- else {
- if (hasSpaces) {
- if (canRemoveHeadingSpaces(node)) {
- converter.replaceSpaces(null);
- }
- else {
- converter.compressSpaces();
- addWidth(contraints, 1);
- }
- }
- }
- addWidth(contraints, wordLength);
-
- // format middle
- wordLength = converter.nextWord();
- while (wordLength > 0) {
- if (mode != FORMAT_ALL) {
- // keep spaces as is
- addWidth(contraints, converter.getSpaceCount());
- }
- else if (!isWidthAvailable(contraints, wordLength + 1)) {
- if (breakSpaces == null)
- breakSpaces = getBreakSpaces(node);
- converter.replaceSpaces(breakSpaces);
- setWidth(contraints, breakSpaces);
- }
- else {
- converter.compressSpaces();
- addWidth(contraints, 1);
- }
- addWidth(contraints, wordLength);
- wordLength = converter.nextWord();
- }
-
- // format tailing spaces
- hasSpaces = converter.hasSpaces();
- if (mode == FORMAT_HEAD) {
- // keep spaces as is
- addWidth(contraints, converter.getSpaceCount());
- }
- else if ((hasSpaces && !isWidthAvailable(contraints, 2)) || canInsertBreakAfter(node)) {
- if (node.getNextSibling() == null) { // last test
- // use parent indent for the end tag
- breakSpaces = getBreakSpaces(parent);
- }
- else {
- if (breakSpaces == null)
- breakSpaces = getBreakSpaces(node);
- }
- converter.replaceSpaces(breakSpaces);
- setWidth(contraints, breakSpaces);
- }
- else {
- if (hasSpaces) {
- if (canRemoveTailingSpaces(node)) {
- converter.replaceSpaces(null);
- }
- else {
- converter.compressSpaces();
- addWidth(contraints, 1);
- }
- }
- }
- }
-
- if (converter.isModified()) {
- source = converter.getSource();
- replaceSource(text.getModel(), offset, length, source);
- }
- }
-}
diff --git a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/SpaceConverter.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/SpaceConverter.java
deleted file mode 100644
index 38f9203a85..0000000000
--- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/SpaceConverter.java
+++ /dev/null
@@ -1,235 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2004, 2007 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.wst.html.core.internal.format;
-
-
-
-final class SpaceConverter {
-
- private String source = null;
- private int length = 0;
- private int startOffset = 0;
- private int endOffset = 0;
- private int spaceCount = 0;
- private int wordCount = 0;
- private StringBuffer buffer = null;
- private int lastOffset = 0;
- private boolean keepBlankLines = false;
-
- /**
- */
- public SpaceConverter(String source) {
- super();
-
- if (source == null) {
- this.source = new String();
- }
- else {
- this.source = source;
- this.length = source.length();
- }
- }
-
- /**
- */
- public SpaceConverter(String source, boolean keepBlankLines) {
- super();
-
- if (source == null) {
- this.source = new String();
- }
- else {
- this.source = source;
- this.length = source.length();
- }
- this.keepBlankLines = keepBlankLines;
- }
-
- /**
- */
- public void compressSpaces() {
- if (this.spaceCount == 0)
- return;
- if (this.spaceCount == 1 && this.source.charAt(this.startOffset) == ' ')
- return;
-
- if (this.buffer == null)
- this.buffer = new StringBuffer();
- if (this.startOffset > this.lastOffset) {
- this.buffer.append(this.source.substring(this.lastOffset, this.startOffset));
- }
-
- this.buffer.append(' ');
-
- this.lastOffset = this.startOffset + this.spaceCount;
- }
-
- /**
- */
- public String getSource() {
- if (this.buffer == null)
- this.buffer = new StringBuffer();
- if (this.length > this.lastOffset) {
- this.buffer.append(this.source.substring(this.lastOffset, this.length));
- }
- return this.buffer.toString();
- }
-
- /**
- */
- public int getSpaceCount() {
- return this.spaceCount;
- }
-
- /**
- */
- public boolean hasSpaces() {
- return (this.spaceCount > 0);
- }
-
- /**
- */
- public boolean isModified() {
- return (this.buffer != null);
- }
-
- /**
- * Add number of the old blank lines to new space string
- */
- private static String mergeBlankLines(String newSpaces, String oldSpaces) {
- if (newSpaces == null || newSpaces.length() == 0)
- return newSpaces;
- if (oldSpaces == null)
- return newSpaces;
-
- // count old new lines
- int newLineCount = 0;
- int oldLength = oldSpaces.length();
- for (int i = 0; i < oldLength; i++) {
- char c = oldSpaces.charAt(i);
- if (c == '\r') {
- newLineCount++;
- if (i + 1 < oldLength) {
- c = oldSpaces.charAt(i + 1);
- if (c == '\n')
- i++;
- }
- }
- else {
- if (c == '\n')
- newLineCount++;
- }
- }
- if (newLineCount < 2)
- return newSpaces; // no blank line
-
- // here assuming newSpaces starts with a new line if any
- String delim = null;
- char d = newSpaces.charAt(0);
- if (d == '\r') {
- if (newSpaces.length() > 1 && newSpaces.charAt(1) == '\n')
- delim = "\r\n";//$NON-NLS-1$
- else
- delim = "\r";//$NON-NLS-1$
- }
- else {
- if (d == '\n')
- delim = "\n";//$NON-NLS-1$
- else
- return newSpaces; // no new line
- }
-
- newLineCount--;
- StringBuffer buffer = new StringBuffer(newSpaces.length() + newLineCount * 2);
- while (newLineCount > 0) {
- buffer.append(delim);
- newLineCount--;
- }
- buffer.append(newSpaces);
- return buffer.toString();
- }
-
- /**
- */
- public int nextWord() {
- if (this.endOffset == this.length) {
- this.startOffset = this.endOffset;
- this.spaceCount = 0;
- this.wordCount = 0;
- return 0;
- }
-
- this.startOffset = this.endOffset;
- int i = this.startOffset;
- for (; i < this.length; i++) {
- if (!Character.isWhitespace(this.source.charAt(i)))
- break;
- }
- this.spaceCount = i - this.startOffset;
-
- int wordOffset = i;
- for (; i < this.length; i++) {
- if (Character.isWhitespace(this.source.charAt(i)))
- break;
- }
- this.wordCount = i - wordOffset;
- this.endOffset = i;
-
- return this.wordCount;
- }
-
- /**
- */
- public void replaceSpaces(String spaces) {
- int spaceLength = (spaces != null ? spaces.length() : 0);
- String oldSpaces = null;
- if (spaceLength == this.spaceCount) {
- if (spaceLength == 0)
- return;
- if (this.startOffset == 0) {
- if (this.source.startsWith(spaces))
- return;
- }
- else if (this.endOffset == this.length) {
- if (this.source.endsWith(spaces))
- return;
- }
- else {
- int textOffset = this.startOffset + this.spaceCount;
- oldSpaces = this.source.substring(this.startOffset, textOffset);
- if (oldSpaces.equals(spaces))
- return;
- }
- }
- if (this.keepBlankLines && this.spaceCount > 0) {
- if (oldSpaces == null) {
- int textOffset = this.startOffset + this.spaceCount;
- oldSpaces = this.source.substring(this.startOffset, textOffset);
- }
- if (oldSpaces != null) {
- spaces = mergeBlankLines(spaces, oldSpaces);
- if (oldSpaces.equals(spaces))
- return;
- }
- }
-
- if (this.buffer == null)
- this.buffer = new StringBuffer();
- if (this.startOffset > this.lastOffset) {
- this.buffer.append(this.source.substring(this.lastOffset, this.startOffset));
- }
-
- if (spaceLength > 0)
- this.buffer.append(spaces);
-
- this.lastOffset = this.startOffset + this.spaceCount;
- }
-} \ No newline at end of file

Back to the top