diff options
author | nitind | 2007-03-28 07:12:54 +0000 |
---|---|---|
committer | nitind | 2007-03-28 07:12:54 +0000 |
commit | eb89b7a2e789b1aa1a16cbc9577fd868461a163f (patch) | |
tree | 28d2423dac811d301aa174b9c4363ef9b6f7c4dd /bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format | |
parent | d21d89ebd13b80a61b432dd5d27476d3117fdb5e (diff) | |
parent | 547e8e108a7a28cccdaf93628c969716776452e8 (diff) | |
download | webtools.sourceediting-200703280735.tar.gz webtools.sourceediting-200703280735.tar.xz webtools.sourceediting-200703280735.zip |
This commit was manufactured by cvs2svn to create tag 'v200703280735'.v200703280735
Diffstat (limited to 'bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format')
8 files changed, 0 insertions, 1858 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 294906cb59..0000000000 --- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/EmbeddedCSSFormatter.java +++ /dev/null @@ -1,90 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004 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.format.CSSSourceFormatter; -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; - INodeAdapter adapter = notifier.getAdapterFor(CSSSourceFormatter.class); - if (adapter == null) - return null; - CSSSourceFormatter formatter = (CSSSourceFormatter) adapter; - 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 9264c34c47..0000000000 --- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLElementFormatter.java +++ /dev/null @@ -1,376 +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 java.util.Iterator; - -import org.eclipse.wst.css.core.internal.format.CSSSourceFormatter; -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.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(); - - if (insertBreak) { - NamedNodeMap attributes = element.getAttributes(); - if (attributes == null || attributes.getLength() < 2) - insertBreak = false; - } - String breakSpaces = getBreakSpaces(element); - 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); - } - 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); - } - 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()); - } - } - - /** - * 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; - 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; - INodeAdapter adapter = notifier.getAdapterFor(CSSSourceFormatter.class); - if (adapter == null) - return null; - CSSSourceFormatter formatter = (CSSSourceFormatter) adapter; - 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 c650384853..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 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; - } -}
\ No newline at end of file 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 8235e46744..0000000000 --- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatProcessorImpl.java +++ /dev/null @@ -1,60 +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)); - 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 72e991644c..0000000000 --- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatter.java +++ /dev/null @@ -1,650 +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.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; - -public class HTMLFormatter implements IStructuredFormatter { - - private static final String HTML_NAME = "html";//$NON-NLS-1$ - private static final String BODY_NAME = "BODY";//$NON-NLS-1$ - - /** - */ - 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(); - - 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; - 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(); - - 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) { - if (decl.getContentType() == CMElementDeclaration.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) { - CMElementDeclaration decl = getElementDeclaration((Element) node); - if (canInsertBreakBefore(decl)) - 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; - } - - CMElementDeclaration decl = getElementDeclaration(element); - if (decl != null && decl.supports(HTMLCMProperties.SHOULD_INDENT_CHILD_SOURCE)) { - boolean shouldIndent = ((Boolean) decl.getProperty(HTMLCMProperties.SHOULD_INDENT_CHILD_SOURCE)).booleanValue(); - 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; - 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)); - 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; - } -}
\ 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 ba003c8ce8..0000000000 --- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLFormatterFactory.java +++ /dev/null @@ -1,108 +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.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/HTMLTextFormatter.java b/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/HTMLTextFormatter.java deleted file mode 100644 index a152f8241e..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 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); - } - } -}
\ No newline at end of file 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 a7b6f66bf9..0000000000 --- a/bundles/org.eclipse.wst.html.core/src/org/eclipse/wst/html/core/internal/format/SpaceConverter.java +++ /dev/null @@ -1,233 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2004 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 (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 |