diff options
Diffstat (limited to 'bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format')
8 files changed, 0 insertions, 1625 deletions
diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/CommentNodeFormatter.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/CommentNodeFormatter.java deleted file mode 100644 index f962f15db6..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/CommentNodeFormatter.java +++ /dev/null @@ -1,99 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import org.eclipse.wst.sse.core.format.IStructuredFormatContraints; -import org.eclipse.wst.sse.core.util.StringUtils; -import org.eclipse.wst.xml.core.document.XMLNode; -import org.w3c.dom.Node; - - -public class CommentNodeFormatter extends NodeFormatter { - - protected String adjustIndentations(String aString, String lineIndent, String singleIndent) { - String result = new String(); - - int indexOfLineDelimiter = StringUtils.indexOfLineDelimiter(aString); - result = aString.substring(0, indexOfLineDelimiter); - while (indexOfLineDelimiter != -1) { - // Before find the next LineDelimiter, we have to figure out the - // size of the current LineDelimiter - // so we can figure out how many bytes to skip before finding the - // next LineDelimiter. - // Otherwise, we may treat the LF in CRLF as the next - // LineDelimiter. - int lineDelimiterSize = 1; - if (aString.length() >= indexOfLineDelimiter + 2 && aString.substring(indexOfLineDelimiter, indexOfLineDelimiter + 1).compareTo(CR) == 0 && aString.substring(indexOfLineDelimiter + 1, indexOfLineDelimiter + 2).compareTo(LF) == 0) - lineDelimiterSize = 2; - - int indexOfNextLineDelimiter = StringUtils.indexOfLineDelimiter(aString, indexOfLineDelimiter + lineDelimiterSize); - int indexOfNonblank = StringUtils.indexOfNonblank(aString, indexOfLineDelimiter); - - if (indexOfNonblank != -1) { - if (indexOfNextLineDelimiter == -1) { - // last line; copy till the end - result += lineIndent + singleIndent + aString.substring(indexOfNonblank); - } else if (indexOfNextLineDelimiter != -1 && indexOfNextLineDelimiter < indexOfNonblank) { - // blank line; just add a indent - result += lineIndent + singleIndent; - } else { - // copy all text between indexOfNonblank and - // indexOfNextLineDelimiter - result += lineIndent + singleIndent + aString.substring(indexOfNonblank, indexOfNextLineDelimiter); - } - - indexOfLineDelimiter = indexOfNextLineDelimiter; - } else { - if (indexOfNextLineDelimiter == -1) { - result += lineIndent; - } else { - // blank line; just add a indent - result += lineIndent + singleIndent; - } - - indexOfLineDelimiter = indexOfNextLineDelimiter; - } - } - - return result; - } - - protected void formatNode(XMLNode node, IStructuredFormatContraints formatContraints) { - if (node != null) { - // lineDelimiterFound means multi line comment - String nodeValue = node.getNodeValue(); - boolean lineDelimiterFoundInComment = StringUtils.containsLineDelimiter(nodeValue); - - if (lineDelimiterFoundInComment) { - // format indentation before node - formatIndentationBeforeNode(node, formatContraints); - - // adjust indentations in multi line comment - String lineDelimiter = node.getModel().getStructuredDocument().getLineDelimiter(); - String lineIndent = formatContraints.getCurrentIndent(); - String singleIndent = getFormatPreferences().getIndent(); - String newNodevalue = adjustIndentations(nodeValue, lineDelimiter + lineIndent, singleIndent); - if (nodeValue.compareTo(newNodevalue) != 0) - node.setNodeValue(newNodevalue); - } - - if (!nodeHasSiblings(node) || (node.getPreviousSibling() != null && node.getPreviousSibling().getNodeType() == Node.TEXT_NODE && !StringUtils.containsLineDelimiter(node.getPreviousSibling().getNodeValue()) && node.getNextSibling() == null)) { - // single child - // or inline comment after text - // do nothing - } else - // format indentation after node - formatIndentationAfterNode(node, formatContraints); - } - } -} diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/DocumentNodeFormatter.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/DocumentNodeFormatter.java deleted file mode 100644 index ee671a9925..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/DocumentNodeFormatter.java +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import org.eclipse.wst.sse.core.format.IStructuredFormatContraints; -import org.eclipse.wst.sse.core.format.IStructuredFormatter; -import org.eclipse.wst.xml.core.document.XMLNode; - - -public class DocumentNodeFormatter extends NodeFormatter { - protected void formatChildren(XMLNode node, IStructuredFormatContraints formatContraints) { - String singleIndent = getFormatPreferences().getIndent(); - String lineIndent = formatContraints.getCurrentIndent(); - - if (node != null && (fProgressMonitor == null || !fProgressMonitor.isCanceled())) { - // normalize node first to combine adjacent text nodes - node.normalize(); - - XMLNode nextChild = (XMLNode) node.getFirstChild(); - while (nextChild != null) { - XMLNode eachChildNode = nextChild; - nextChild = (XMLNode) eachChildNode.getNextSibling(); - IStructuredFormatter formatter = getFormatter(eachChildNode); - IStructuredFormatContraints childFormatContraints = formatter.getFormatContraints(); - String childIndent = lineIndent + singleIndent; - childFormatContraints.setCurrentIndent(childIndent); - childFormatContraints.setClearAllBlankLines(formatContraints.getClearAllBlankLines()); - - // format each child - formatter.format(eachChildNode, childFormatContraints); - - if (nextChild != null && nextChild.getParentNode() == null) - // nextNode is deleted during format - nextChild = (XMLNode) eachChildNode.getNextSibling(); - } - } - } - - protected void formatNode(XMLNode node, IStructuredFormatContraints formatContraints) { - if (node != null) - formatChildren(node, formatContraints); - } -} diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/ElementNodeFormatter.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/ElementNodeFormatter.java deleted file mode 100644 index ad5e83fef8..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/ElementNodeFormatter.java +++ /dev/null @@ -1,354 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.wst.common.contentmodel.CMAttributeDeclaration; -import org.eclipse.wst.common.contentmodel.CMElementDeclaration; -import org.eclipse.wst.sse.core.exceptions.SourceEditingRuntimeException; -import org.eclipse.wst.sse.core.format.IStructuredFormatContraints; -import org.eclipse.wst.sse.core.modelquery.ModelQueryAdapter; -import org.eclipse.wst.sse.core.text.IStructuredDocument; -import org.eclipse.wst.sse.core.text.IStructuredDocumentRegion; -import org.eclipse.wst.sse.core.text.ITextRegion; -import org.eclipse.wst.sse.core.text.ITextRegionList; -import org.eclipse.wst.sse.core.util.StringUtils; -import org.eclipse.wst.xml.core.document.XMLDocument; -import org.eclipse.wst.xml.core.document.XMLGenerator; -import org.eclipse.wst.xml.core.document.XMLModel; -import org.eclipse.wst.xml.core.document.XMLNode; -import org.eclipse.wst.xml.core.internal.document.AttrImpl; -import org.eclipse.wst.xml.core.parser.XMLRegionContext; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; - - -public class ElementNodeFormatter extends DocumentNodeFormatter { - static protected final char DOUBLE_QUOTE = '"';//$NON-NLS-1$ - static protected final String DOUBLE_QUOTES = "\"\"";//$NON-NLS-1$ - static protected final char EQUAL_CHAR = '='; // equal sign$NON-NLS-1$ - static protected final String PRESERVE = "preserve";//$NON-NLS-1$ - static protected final String PRESERVE_QUOTED = "\"preserve\"";//$NON-NLS-1$ - static protected final char SINGLE_QUOTE = '\'';//$NON-NLS-1$ - static protected final String XML_SPACE = "xml:space";//$NON-NLS-1$ - - protected void formatEndTag(XMLNode node, IStructuredFormatContraints formatContraints) { - if (!isEndTagMissing(node)) { - // end tag exists - - IStructuredDocument structuredDocument = node.getModel().getStructuredDocument(); - String lineDelimiter = structuredDocument.getLineDelimiter(); - String nodeIndentation = getNodeIndent(node); - XMLNode lastChild = (XMLNode) node.getLastChild(); - if (lastChild != null && lastChild.getNodeType() != Node.TEXT_NODE) { - if (isEndTagMissing(lastChild)) { - // find deepest child - XMLNode deepestChild = (XMLNode) lastChild.getLastChild(); - while (deepestChild != null && deepestChild.getLastChild() != null && isEndTagMissing(deepestChild)) { - lastChild = deepestChild; - deepestChild = (XMLNode) deepestChild.getLastChild(); - } - - if (deepestChild != null) { - if (deepestChild.getNodeType() == Node.TEXT_NODE) { - // Special indentation handling if lastChild's end - // tag is missing and deepestChild is a text node. - String nodeText = deepestChild.getNodeValue(); - - if (!nodeText.endsWith(lineDelimiter + nodeIndentation)) { - nodeText = StringUtils.appendIfNotEndWith(nodeText, lineDelimiter); - nodeText = StringUtils.appendIfNotEndWith(nodeText, nodeIndentation); - } - - replaceNodeValue(deepestChild, nodeText); - } else - insertAfterNode(lastChild, lineDelimiter + nodeIndentation); - } - } else - // indent end tag - insertAfterNode(lastChild, lineDelimiter + nodeIndentation); - } else if (lastChild == null && firstStructuredDocumentRegionContainsLineDelimiters(node)) { - // indent end tag - replace(structuredDocument, node.getFirstStructuredDocumentRegion().getEndOffset(), 0, lineDelimiter + nodeIndentation); - } - - // format end tag name - IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getLastStructuredDocumentRegion(); - if (endTagStructuredDocumentRegion.getRegions().size() >= 3) { - ITextRegion endTagNameRegion = endTagStructuredDocumentRegion.getRegions().get(1); - removeRegionSpaces(node, endTagStructuredDocumentRegion, endTagNameRegion); - } - } - } - - protected void formatNode(XMLNode node, IStructuredFormatContraints formatContraints) { - if (node != null) { - // format indentation before node - formatIndentationBeforeNode(node, formatContraints); - - // format start tag - XMLNode newNode = node; - int startTagStartOffset = node.getStartOffset(); - XMLModel structuredModel = node.getModel(); - - formatStartTag(node, formatContraints); - // save new node - newNode = (XMLNode) structuredModel.getIndexedRegion(startTagStartOffset); - - IStructuredDocumentRegion flatNode = newNode.getFirstStructuredDocumentRegion(); - if (flatNode != null) { - ITextRegionList regions = flatNode.getRegions(); - ITextRegion lastRegion = regions.get(regions.size() - 1); - // format children and end tag if not empty start tag - if (lastRegion.getType() != XMLRegionContext.XML_EMPTY_TAG_CLOSE) { - // format children - formatChildren(newNode, formatContraints); - - // save new node - newNode = (XMLNode) structuredModel.getIndexedRegion(startTagStartOffset); - - // format end tag - formatEndTag(newNode, formatContraints); - } - } - - // format indentation after node - formatIndentationAfterNode(newNode, formatContraints); - } - } - - /** - * This method formats the start tag name, and formats the attributes if - * available. - */ - protected void formatStartTag(XMLNode node, IStructuredFormatContraints formatContraints) { - String singleIndent = getFormatPreferences().getIndent(); - String lineIndent = formatContraints.getCurrentIndent(); - String attrIndent = lineIndent + singleIndent; - boolean splitMultiAttrs = ((IStructuredFormatPreferencesXML) fFormatPreferences).getSplitMultiAttrs(); - IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion(); - NamedNodeMap attributes = node.getAttributes(); - - // Note: attributes should not be null even if the node has no - // attributes. However, attributes.getLength() will be 0. But, check - // for null just in case. - if (attributes != null) { - // compute current available line width - int currentAvailableLineWidth = 0; - try { - // 1 is for "<" - int nodeNameOffset = node.getStartOffset() + 1 + node.getNodeName().length(); - int lineOffset = node.getStructuredDocument().getLineInformationOfOffset(nodeNameOffset).getOffset(); - String text = node.getStructuredDocument().get(lineOffset, nodeNameOffset - lineOffset); - int usedWidth = getIndentationLength(text); - currentAvailableLineWidth = getFormatPreferences().getLineWidth() - usedWidth; - } catch (BadLocationException exception) { - throw new SourceEditingRuntimeException(exception); - } - - StringBuffer stringBuffer = new StringBuffer(); - String lineDelimiter = node.getModel().getStructuredDocument().getLineDelimiter(); - int attrLength = attributes.getLength(); - int lastUndefinedRegionOffset = 0; - for (int i = 0; i < attrLength; i++) { - AttrImpl attr = (AttrImpl) attributes.item(i); - ITextRegion nameRegion = attr.getNameRegion(); - ITextRegion equalRegion = attr.getEqualRegion(); - ITextRegion valueRegion = attr.getValueRegion(); - - // append undefined regions - String undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, attr.getStartOffset() - lastUndefinedRegionOffset); - stringBuffer.append(undefinedRegion); - lastUndefinedRegionOffset = attr.getStartOffset(); - - // check for xml:space attribute - if (flatNode.getText(nameRegion).compareTo(XML_SPACE) == 0) { - if (valueRegion == null) { - ModelQueryAdapter adapter = (ModelQueryAdapter) ((XMLDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class); - CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node); - if (elementDeclaration == null) - // CMElementDeclaration not found, default to - // PRESERVE - formatContraints.setClearAllBlankLines(false); - else { - CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) elementDeclaration.getAttributes().getNamedItem(XML_SPACE); - if (attributeDeclaration == null) - // CMAttributeDeclaration not found, default - // to PRESERVE - formatContraints.setClearAllBlankLines(false); - else { - String defaultValue = attributeDeclaration.getAttrType().getImpliedValue(); - - if (defaultValue.compareTo(PRESERVE) == 0) - formatContraints.setClearAllBlankLines(false); - else - formatContraints.setClearAllBlankLines(getFormatPreferences().getClearAllBlankLines()); - } - } - } else { - XMLGenerator generator = node.getModel().getGenerator(); - String newAttrValue = generator.generateAttrValue(attr); - - // There is a problem in - // StructuredDocumentRegionUtil.getAttrValue(ITextRegion) - // when the region is instanceof ContextRegion. - // Workaround for now. - if (flatNode.getText(valueRegion).length() == 1) { - char firstChar = flatNode.getText(valueRegion).charAt(0); - if ((firstChar == DOUBLE_QUOTE) || (firstChar == SINGLE_QUOTE)) - newAttrValue = DOUBLE_QUOTES; - } - - if (newAttrValue.compareTo(PRESERVE_QUOTED) == 0) - formatContraints.setClearAllBlankLines(false); - else - formatContraints.setClearAllBlankLines(getFormatPreferences().getClearAllBlankLines()); - } - } - - if (splitMultiAttrs && attrLength > 1) { - stringBuffer.append(lineDelimiter + attrIndent); - stringBuffer.append(flatNode.getText(nameRegion)); - if (valueRegion != null) { - // append undefined regions - undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(equalRegion) - lastUndefinedRegionOffset); - stringBuffer.append(undefinedRegion); - lastUndefinedRegionOffset = flatNode.getStartOffset(equalRegion); - - stringBuffer.append(EQUAL_CHAR); - - // append undefined regions - undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(valueRegion) - lastUndefinedRegionOffset); - stringBuffer.append(undefinedRegion); - lastUndefinedRegionOffset = flatNode.getStartOffset(valueRegion); - - // Note: trim() should not be needed for - // valueRegion.getText(). Just a workaround for a - // problem found in valueRegion for now. - stringBuffer.append(flatNode.getText(valueRegion).trim()); - } - } else { - if (valueRegion != null) { - int textLength = 1 + flatNode.getText(nameRegion).length() + 1 + flatNode.getText(valueRegion).length(); - if (i == attrLength - 1) { - if (flatNode != null) { - ITextRegionList regions = flatNode.getRegions(); - ITextRegion lastRegion = regions.get(regions.size() - 1); - if (lastRegion.getType() != XMLRegionContext.XML_EMPTY_TAG_CLOSE) - // 3 is for " />" - textLength += 3; - else - // 1 is for ">" - textLength++; - } - } - - if (currentAvailableLineWidth >= textLength) { - stringBuffer.append(SPACE_CHAR); - currentAvailableLineWidth--; - } else { - stringBuffer.append(lineDelimiter + attrIndent); - currentAvailableLineWidth = getFormatPreferences().getLineWidth() - attrIndent.length(); - } - - stringBuffer.append(flatNode.getText(nameRegion)); - - // append undefined regions - undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(equalRegion) - lastUndefinedRegionOffset); - stringBuffer.append(undefinedRegion); - lastUndefinedRegionOffset = flatNode.getStartOffset(equalRegion); - - stringBuffer.append(EQUAL_CHAR); - - // append undefined regions - undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, flatNode.getStartOffset(valueRegion) - lastUndefinedRegionOffset); - stringBuffer.append(undefinedRegion); - lastUndefinedRegionOffset = flatNode.getStartOffset(valueRegion); - - // Note: trim() should not be needed for - // valueRegion.getText(). Just a workaround for a - // problem found in valueRegion for now. - stringBuffer.append(flatNode.getText(valueRegion).trim()); - - currentAvailableLineWidth -= flatNode.getText(nameRegion).length(); - currentAvailableLineWidth--; - currentAvailableLineWidth -= flatNode.getText(valueRegion).trim().length(); - } else { - if (currentAvailableLineWidth >= 1 + flatNode.getText(nameRegion).length()) { - stringBuffer.append(SPACE_CHAR); - currentAvailableLineWidth--; - } else { - stringBuffer.append(lineDelimiter + attrIndent); - currentAvailableLineWidth = getFormatPreferences().getLineWidth() - attrIndent.length(); - } - - stringBuffer.append(flatNode.getText(nameRegion)); - - currentAvailableLineWidth -= flatNode.getText(nameRegion).length(); - } - } - } - - // append undefined regions - String undefinedRegion = getUndefinedRegions(node, lastUndefinedRegionOffset, node.getEndOffset() - lastUndefinedRegionOffset); - stringBuffer.append(undefinedRegion); - - XMLModel structuredModel = node.getModel(); - IStructuredDocument structuredDocument = structuredModel.getStructuredDocument(); - // 1 is for "<" - int offset = node.getStartOffset() + 1 + node.getNodeName().length(); - // 1 is for "<" - int length = node.getFirstStructuredDocumentRegion().getTextLength() - 1 - node.getNodeName().length(); - - if (flatNode != null) { - ITextRegionList regions = flatNode.getRegions(); - ITextRegion firstRegion = regions.get(0); - ITextRegion lastRegion = regions.get(regions.size() - 1); - - if (firstRegion.getType() == XMLRegionContext.XML_END_TAG_OPEN) - // skip formatting for end tags in this format: </tagName> - return; - else { - if (lastRegion.getType() == XMLRegionContext.XML_TAG_CLOSE || lastRegion.getType() == XMLRegionContext.XML_EMPTY_TAG_CLOSE) - length = length - lastRegion.getLength(); - - if (lastRegion.getType() == XMLRegionContext.XML_EMPTY_TAG_CLOSE) - // leave space before XML_EMPTY_TAG_CLOSE: <tagName /> - stringBuffer.append(SPACE_CHAR); - } - } - - replace(structuredDocument, offset, length, stringBuffer.toString()); - } - } - - protected String getUndefinedRegions(XMLNode node, int startOffset, int length) { - String result = new String(); - - IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion(); - ITextRegionList regions = flatNode.getRegions(); - for (int i = 0; i < regions.size(); i++) { - ITextRegion region = regions.get(i); - String regionType = region.getType(); - int regionStartOffset = flatNode.getStartOffset(region); - - if (regionType.compareTo(XMLRegionContext.UNDEFINED) == 0 && regionStartOffset >= startOffset && regionStartOffset < startOffset + length) - result = result + flatNode.getFullText(region); - } - - if (result.length() > 0) - return SPACE + result.trim(); - else - return result; - } -} diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/FormatProcessorXML.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/FormatProcessorXML.java deleted file mode 100644 index 292bde58c4..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/FormatProcessorXML.java +++ /dev/null @@ -1,108 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import org.eclipse.core.runtime.Preferences; -import org.eclipse.wst.sse.core.format.AbstractStructuredFormatProcessor; -import org.eclipse.wst.sse.core.format.IStructuredFormatPreferences; -import org.eclipse.wst.sse.core.format.IStructuredFormatter; -import org.eclipse.wst.sse.core.internal.SSECorePlugin; -import org.eclipse.wst.sse.core.preferences.CommonModelPreferenceNames; -import org.eclipse.wst.xml.core.internal.XMLCorePlugin; -import org.eclipse.wst.xml.core.internal.document.CDATASectionImpl; -import org.w3c.dom.Node; - -public class FormatProcessorXML extends AbstractStructuredFormatProcessor { - protected IStructuredFormatPreferences fFormatPreferences = null; - - protected String getFileExtension() { - return "xml"; //$NON-NLS-1$ - } - - public IStructuredFormatPreferences getFormatPreferences() { - if (fFormatPreferences == null) { - fFormatPreferences = new StructuredFormatPreferencesXML(); - - Preferences preferences = getModelPreferences(); - if (preferences != null) { - fFormatPreferences.setLineWidth(preferences.getInt(CommonModelPreferenceNames.LINE_WIDTH)); - ((IStructuredFormatPreferencesXML) fFormatPreferences).setSplitMultiAttrs(preferences.getBoolean(CommonModelPreferenceNames.SPLIT_MULTI_ATTRS)); - fFormatPreferences.setClearAllBlankLines(preferences.getBoolean(CommonModelPreferenceNames.CLEAR_ALL_BLANK_LINES)); - - if (preferences.getBoolean(CommonModelPreferenceNames.INDENT_USING_TABS)) - fFormatPreferences.setIndent("\t"); //$NON-NLS-1$ - else { - int tabWidth = SSECorePlugin.getDefault().getPluginPreferences().getInt(CommonModelPreferenceNames.TAB_WIDTH); - String indent = ""; //$NON-NLS-1$ - for (int i = 0; i < tabWidth; i++) { - indent += " "; //$NON-NLS-1$ - } - fFormatPreferences.setIndent(indent); - } - } - } - - return fFormatPreferences; - } - - protected IStructuredFormatter getFormatter(Node node) { - // 262135 - NPE during format of empty document - if (node == null) - return null; - - short nodeType = node.getNodeType(); - IStructuredFormatter formatter = null; - switch (nodeType) { - case Node.ELEMENT_NODE : { - formatter = new ElementNodeFormatter(); - break; - } - case Node.TEXT_NODE : { - if (node instanceof CDATASectionImpl) - formatter = new NodeFormatter(); - else - formatter = new TextNodeFormatter(); - break; - } - case Node.COMMENT_NODE : { - formatter = new CommentNodeFormatter(); - break; - } - case Node.PROCESSING_INSTRUCTION_NODE : { - formatter = new NodeFormatter(); - break; - } - case Node.DOCUMENT_NODE : { - formatter = new DocumentNodeFormatter(); - break; - } - default : { - formatter = new NodeFormatter(); - } - } - - // init fomatter - formatter.setFormatPreferences(getFormatPreferences()); - formatter.setProgressMonitor(fProgressMonitor); - - return formatter; - } - - protected Preferences getModelPreferences() { - return XMLCorePlugin.getDefault().getPluginPreferences(); - } - - protected void refreshFormatPreferences() { - fFormatPreferences = null; - } -} diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/IStructuredFormatPreferencesXML.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/IStructuredFormatPreferencesXML.java deleted file mode 100644 index bc3faccf25..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/IStructuredFormatPreferencesXML.java +++ /dev/null @@ -1,21 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import org.eclipse.wst.sse.core.format.IStructuredFormatPreferences; - -public interface IStructuredFormatPreferencesXML extends IStructuredFormatPreferences { - boolean getSplitMultiAttrs(); - - void setSplitMultiAttrs(boolean splitMultiAttrs); -} diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/NodeFormatter.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/NodeFormatter.java deleted file mode 100644 index 73d7afcf91..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/NodeFormatter.java +++ /dev/null @@ -1,765 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.Preferences; -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.wst.sse.core.exceptions.SourceEditingRuntimeException; -import org.eclipse.wst.sse.core.format.IStructuredFormatContraints; -import org.eclipse.wst.sse.core.format.IStructuredFormatPreferences; -import org.eclipse.wst.sse.core.format.IStructuredFormatter; -import org.eclipse.wst.sse.core.format.StructuredFormatContraints; -import org.eclipse.wst.sse.core.internal.SSECorePlugin; -import org.eclipse.wst.sse.core.internal.parser.ContextRegion; -import org.eclipse.wst.sse.core.preferences.CommonModelPreferenceNames; -import org.eclipse.wst.sse.core.text.IStructuredDocument; -import org.eclipse.wst.sse.core.text.IStructuredDocumentRegion; -import org.eclipse.wst.sse.core.text.ITextRegion; -import org.eclipse.wst.sse.core.text.ITextRegionList; -import org.eclipse.wst.sse.core.util.StringUtils; -import org.eclipse.wst.xml.core.document.XMLModel; -import org.eclipse.wst.xml.core.document.XMLNode; -import org.eclipse.wst.xml.core.internal.XMLCorePlugin; -import org.eclipse.wst.xml.core.internal.document.CDATASectionImpl; -import org.eclipse.wst.xml.core.internal.parser.regions.TagNameRegion; -import org.eclipse.wst.xml.core.jsp.model.parser.temp.XMLJSPRegionContexts; -import org.eclipse.wst.xml.core.parser.XMLRegionContext; -import org.w3c.dom.Node; - -public class NodeFormatter implements IStructuredFormatter { - static protected final String CR = "\r"; //$NON-NLS-1$ - static protected final String CRLF = "\r\n"; //$NON-NLS-1$ - static protected final String DELIMITERS = " \t\n\r\f"; //$NON-NLS-1$ - static protected final String EMPTY_STRING = ""; //$NON-NLS-1$ - static protected final String FF = "\f"; //$NON-NLS-1$ - static protected final String LF = "\n"; //$NON-NLS-1$ - static protected final String SPACE = " "; //$NON-NLS-1$ - static protected final char SPACE_CHAR = ' '; //$NON-NLS-1$ - static protected final String TAB = "\t"; //$NON-NLS-1$ - static protected final char TAB_CHAR = '\t'; //$NON-NLS-1$ - protected IStructuredFormatContraints fFormatContraints = null; - protected IStructuredFormatPreferences fFormatPreferences = null; - protected IProgressMonitor fProgressMonitor = null; - - protected String compressSpaces(String string, IStructuredFormatContraints formatContraints) { - /* - * Note that the StructuredTextEditor supports mixed new line - * characters (CR, LF, CRLF) in one file. We have to handle that when - * we try to preserve blank lines. - */ - String[] stringArray = null; - boolean clearAllBlankLines = formatContraints.getClearAllBlankLines(); - - if (clearAllBlankLines) - stringArray = StringUtils.asArray(string); - else - stringArray = StringUtils.asArray(string, DELIMITERS, true); - - StringBuffer compressedString = new StringBuffer(); - if (stringArray.length > 0) { - boolean cr = false, lf = false, cr2 = false, nonSpace = true; - - if (stringArray[0].compareTo(CR) == 0) - cr = true; - else if (stringArray[0].compareTo(LF) == 0) - lf = true; - else if ((stringArray[0].compareTo(SPACE) != 0) && (stringArray[0].compareTo(TAB) != 0) && (stringArray[0].compareTo(FF) != 0)) { - compressedString.append(stringArray[0]); - nonSpace = true; - } - - for (int i = 1; i < stringArray.length; i++) { - if (stringArray[i].compareTo(CR) == 0) { - if (cr && lf) { - if (nonSpace) { - compressedString.append(CR + LF); - nonSpace = false; - } - compressedString.append(stringArray[i]); - cr2 = true; - } else if (cr) { - if (nonSpace) { - compressedString.append(CR); - nonSpace = false; - } - compressedString.append(stringArray[i]); - cr2 = true; - } else - cr = true; - } else if (stringArray[i].compareTo(LF) == 0) { - if (cr && lf && cr2) { - compressedString.append(stringArray[i]); - } else if (lf) { - if (nonSpace) { - compressedString.append(LF); - nonSpace = false; - } - compressedString.append(stringArray[i]); - } else - lf = true; - } else if ((stringArray[i].compareTo(SPACE) != 0) && (stringArray[i].compareTo(TAB) != 0) && (stringArray[i].compareTo(FF) != 0)) { - if (compressedString.length() > 0) - compressedString.append(SPACE); - compressedString.append(stringArray[i]); - - cr = false; - lf = false; - cr2 = false; - nonSpace = true; - } - } - } - - return compressedString.toString(); - } - - protected boolean firstStructuredDocumentRegionContainsLineDelimiters(XMLNode node) { - boolean result = false; - - if (node != null) { - IStructuredDocumentRegion firstStructuredDocumentRegion = node.getFirstStructuredDocumentRegion(); - if (firstStructuredDocumentRegion != null && firstStructuredDocumentRegion.getText() != null) { - String firstStructuredDocumentRegionText = firstStructuredDocumentRegion.getText(); - result = StringUtils.containsLineDelimiter(firstStructuredDocumentRegionText); - } - } - - return result; - } - - public void format(Node node) { - IStructuredFormatContraints formatContraints = getFormatContraints(); - - format(node, formatContraints); - } - - public void format(Node node, IStructuredFormatContraints formatContraints) { - if (formatContraints.getFormatWithSiblingIndent()) - formatContraints.setCurrentIndent(getSiblingIndent(node)); - - if (node instanceof XMLNode) - formatNode((XMLNode) node, formatContraints); - } - - protected void formatIndentationAfterNode(XMLNode node, IStructuredFormatContraints formatContraints) { - if (node != null) { - XMLNode nextSibling = (XMLNode) node.getNextSibling(); - IStructuredDocument doc = node.getModel().getStructuredDocument(); - int line = doc.getLineOfOffset(node.getEndOffset()); - String lineDelimiter = doc.getLineDelimiter(); - try { - lineDelimiter = doc.getLineDelimiter(line); - if (lineDelimiter == null) - lineDelimiter = ""; //$NON-NLS-1$ - } catch (BadLocationException exception) { - throw new SourceEditingRuntimeException(exception); - } - - if (node.getParentNode() != null) { - if (node.getParentNode().getNodeType() == Node.DOCUMENT_NODE) - if (nextSibling != null) - if (nextSibling.getNodeType() == Node.TEXT_NODE) - getFormatter(nextSibling).format(nextSibling, formatContraints); - else if (nextSibling.getNodeType() == Node.COMMENT_NODE) { - // do nothing - } else { - String lineIndent = formatContraints.getCurrentIndent(); - insertAfterNode(node, lineDelimiter + lineIndent); - } - else { - } - - else if (nextSibling != null) - if (nextSibling.getNodeType() == Node.TEXT_NODE) - getFormatter(nextSibling).format(nextSibling, formatContraints); - else if (nextSibling.getNodeType() == Node.COMMENT_NODE) { - // do nothing - } else { - String lineIndent = formatContraints.getCurrentIndent(); - insertAfterNode(node, lineDelimiter + lineIndent); - } - else { - XMLNode indentNode = getParentIndentNode(node); - String lineIndent = getNodeIndent(indentNode); - XMLNode lastChild = getDeepestChildNode(node); - boolean clearAllBlankLines = formatContraints.getClearAllBlankLines(); - - if (lastChild != null) { - if ((lastChild.getNodeType() == Node.TEXT_NODE) && (lastChild.getNodeValue().endsWith(lineDelimiter + lineIndent))) { - // this text node already ends with the requested - // indentation - } - - else if ((lastChild.getNodeType() == Node.TEXT_NODE) && (lastChild.getNodeValue() != null && lastChild.getNodeValue().endsWith(lineDelimiter))) - if (clearAllBlankLines) { - replaceNodeValue(lastChild, lineDelimiter + lineIndent); - } else { - // append indentation - insertAfterNode(lastChild, lineIndent); - } - else if (lastChild.getNodeType() == Node.TEXT_NODE) - if (lastChild.getNodeValue().length() == 0) { - // replace - replaceNodeValue(lastChild, lineDelimiter + lineIndent); - } else { - // append indentation - insertAfterNode(lastChild, lineDelimiter + lineIndent); - } - else { - // append indentation - insertAfterNode(lastChild, lineDelimiter + lineIndent); - } - } - } - } - } - } - - protected void formatIndentationBeforeNode(XMLNode node, IStructuredFormatContraints formatContraints) { - if (node != null) { - XMLNode previousSibling = (XMLNode) node.getPreviousSibling(); - IStructuredDocument doc = node.getModel().getStructuredDocument(); - int line = doc.getLineOfOffset(node.getStartOffset()); - String lineDelimiter = doc.getLineDelimiter(); - try { - if (line > 0) { - lineDelimiter = doc.getLineDelimiter(line - 1); - if (lineDelimiter == null) - lineDelimiter = ""; //$NON-NLS-1$ - } - } catch (BadLocationException exception) { - throw new SourceEditingRuntimeException(exception); - } - String lineIndent = formatContraints.getCurrentIndent(); - - if (node.getParentNode() != null) { - if (node.getParentNode().getNodeType() == Node.DOCUMENT_NODE) { - if (previousSibling != null) - if (previousSibling.getNodeType() == Node.TEXT_NODE) - getFormatter(previousSibling).format(previousSibling, formatContraints); - else { - insertBeforeNode(node, lineDelimiter + lineIndent); - } - } else { - if (previousSibling == null || previousSibling.getNodeType() != Node.TEXT_NODE) { - // 261968 - formatting tag without closing bracket: - // <t1><t1 - // 265673 - Null ptr in formatIndentationBeforeNode - int prevEndNodeOffset = -1; - int prevEndRegionOffset = -1; - if (previousSibling != null) { - prevEndNodeOffset = previousSibling.getEndOffset(); - IStructuredDocumentRegion endRegion = previousSibling.getEndStructuredDocumentRegion(); - if (endRegion != null) { - prevEndRegionOffset = endRegion.getTextEndOffset(); - } - } - if ((previousSibling == null) || (prevEndNodeOffset != -1 && prevEndNodeOffset == prevEndRegionOffset)) { - insertBeforeNode(node, lineDelimiter + lineIndent); - } - - } else { - if (previousSibling.getNodeValue().length() == 0) { - // replace - replaceNodeValue(previousSibling, lineDelimiter + lineIndent); - } else { - // append indentation - if (!previousSibling.getNodeValue().endsWith(lineDelimiter + lineIndent)) { - if (previousSibling.getNodeValue().endsWith(lineDelimiter)) { - insertAfterNode(previousSibling, lineIndent); - } else - getFormatter(previousSibling).format(previousSibling, formatContraints); - } - } - } - } - } - } - } - - protected void formatNode(XMLNode node, IStructuredFormatContraints formatContraints) { - if (node != null && (fProgressMonitor == null || !fProgressMonitor.isCanceled())) { - // format indentation before node - formatIndentationBeforeNode(node, formatContraints); - - // format indentation after node - formatIndentationAfterNode(node, formatContraints); - } - } - - /** - * This method will compute the correct indentation after this node - * depending on the indentations of its sibling nodes and parent node. Not - * needed anymore? - */ - protected void formatTrailingText(XMLNode node, IStructuredFormatContraints formatContraints) { - String lineDelimiter = node.getModel().getStructuredDocument().getLineDelimiter(); - String lineIndent = formatContraints.getCurrentIndent(); - String parentLineIndent = getNodeIndent(node.getParentNode()); - boolean clearAllBlankLines = formatContraints.getClearAllBlankLines(); - - if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE)) { - XMLNode nextSibling = (XMLNode) node.getNextSibling(); - if ((nextSibling != null) && (nextSibling.getNodeType() == Node.TEXT_NODE)) { - String nextSiblingText = nextSibling.getNodeValue(); - if (nextSibling.getNextSibling() == null) - if ((nextSibling.getParentNode().getNodeType() == Node.DOCUMENT_NODE) && (nextSiblingText.trim().length() == 0)) - // delete spaces at the end of the document - replaceNodeValue(nextSibling, EMPTY_STRING); - else - // replace the text node with parent indentation - replaceNodeValue(nextSibling, lineDelimiter + parentLineIndent); - else - // replace the text node with indentation - replaceNodeValue(nextSibling, lineDelimiter + lineIndent); - } else { - if (nextSibling == null) { - lineIndent = parentLineIndent; - - if (node.getParentNode().getNodeType() != Node.DOCUMENT_NODE) - if ((node.getNodeType() == Node.TEXT_NODE) && (node.getNodeValue().endsWith(lineDelimiter + lineIndent))) { - // this text node already ends with the requested - // indentation - } - - else if ((node.getNodeType() == Node.TEXT_NODE) && (node.getNodeValue().endsWith(lineDelimiter))) - if (clearAllBlankLines) - replaceNodeValue(node, lineDelimiter + lineIndent); - else - // append indentation - insertAfterNode(node, lineIndent); - else if (node.getNodeType() == Node.TEXT_NODE) - if (node.getNodeValue().length() == 0) - // replace - replaceNodeValue(node, lineDelimiter + lineIndent); - else - // append indentation - if (!node.getNodeValue().endsWith(lineDelimiter + lineIndent)) - if (node.getNodeValue().endsWith(lineDelimiter)) - insertAfterNode(node, lineIndent); - else - insertAfterNode(node, lineDelimiter + lineIndent); - else - replaceNodeValue(node, lineDelimiter + lineIndent); - } else { - if ((node.getNodeType() == Node.TEXT_NODE) && (node.getNodeValue().endsWith(lineDelimiter + lineIndent))) { - // this text node already ends with the requested - // indentation - } - - else if ((node.getNodeType() == Node.TEXT_NODE) && (node.getNodeValue().endsWith(lineDelimiter))) - if (clearAllBlankLines) - replaceNodeValue(node, lineDelimiter + lineIndent); - else - // append indentation - insertAfterNode(node, lineIndent); - else if (node.getNodeType() == Node.TEXT_NODE) - if (node.getNodeValue().length() == 0) - // replace - replaceNodeValue(node, lineDelimiter + lineIndent); - else - // append indentation - insertAfterNode(node, lineDelimiter + lineIndent); - else - // append indentation - insertAfterNode(node, lineDelimiter + lineIndent); - } - } - } - } - - protected String getCompressedNodeText(XMLNode node, IStructuredFormatContraints formatContraints) { - return compressSpaces(getNodeText(node), formatContraints); - } - - protected XMLNode getDeepestChildNode(XMLNode node) { - XMLNode result = null; - XMLNode lastChild = (XMLNode) node.getLastChild(); - - if (lastChild == null) - result = node; - else { - result = getDeepestChildNode(lastChild); - - if ((result.getNodeType() == Node.TEXT_NODE || result.getNodeType() == Node.COMMENT_NODE) && !isEndTagMissing(node)) - result = node; - } - - return result; - } - - public IStructuredFormatContraints getFormatContraints() { - if (fFormatContraints == null) { - fFormatContraints = new StructuredFormatContraints(); - - fFormatContraints.setClearAllBlankLines(getFormatPreferences().getClearAllBlankLines()); - } - - return fFormatContraints; - } - - public IStructuredFormatPreferences getFormatPreferences() { - if (fFormatPreferences == null) { - fFormatPreferences = new StructuredFormatPreferencesXML(); - - Preferences preferences = getModelPreferences(); - if (preferences != null) { - fFormatPreferences.setLineWidth(preferences.getInt(CommonModelPreferenceNames.LINE_WIDTH)); - ((IStructuredFormatPreferencesXML) fFormatPreferences).setSplitMultiAttrs(preferences.getBoolean(CommonModelPreferenceNames.SPLIT_MULTI_ATTRS)); - fFormatPreferences.setClearAllBlankLines(preferences.getBoolean(CommonModelPreferenceNames.CLEAR_ALL_BLANK_LINES)); - - if (preferences.getBoolean(CommonModelPreferenceNames.INDENT_USING_TABS)) - fFormatPreferences.setIndent("\t"); //$NON-NLS-1$ - else { - int tabWidth = SSECorePlugin.getDefault().getPluginPreferences().getInt(CommonModelPreferenceNames.TAB_WIDTH); - String indent = ""; //$NON-NLS-1$ - for (int i = 0; i < tabWidth; i++) { - indent += " "; //$NON-NLS-1$ - } - fFormatPreferences.setIndent(indent); - } - } - } - - return fFormatPreferences; - } - - protected IStructuredFormatter getFormatter(XMLNode node) { - // 262135 - NPE during format of empty document - if (node == null) - return null; - - short nodeType = ((Node) node).getNodeType(); - IStructuredFormatter formatter = null; - switch (nodeType) { - case Node.ELEMENT_NODE : { - formatter = new ElementNodeFormatter(); - break; - } - case Node.TEXT_NODE : { - if (node instanceof CDATASectionImpl) - formatter = new NodeFormatter(); - else - formatter = new TextNodeFormatter(); - break; - } - case Node.COMMENT_NODE : { - formatter = new CommentNodeFormatter(); - break; - } - case Node.PROCESSING_INSTRUCTION_NODE : { - formatter = new NodeFormatter(); - break; - } - case Node.DOCUMENT_NODE : { - formatter = new DocumentNodeFormatter(); - break; - } - default : { - formatter = new NodeFormatter(); - } - } - - // init fomatter - formatter.setFormatPreferences(getFormatPreferences()); - formatter.setProgressMonitor(fProgressMonitor); - - return formatter; - } - - protected int getIndentationLength(String indent) { - // TODO Kit : The calculation of IndentationLength is not correct - // here. - // nodeIndentation may contain tabs. Multiply by 4 temporarily to get - // approx. width. - // Need to re-work. - - int indentationLength = 0; - - for (int i = 0; i < indent.length(); i++) { - if (indent.substring(i, i + 1).compareTo(TAB) == 0) - indentationLength += 4; - else - indentationLength++; - } - - return indentationLength; - } - - protected Preferences getModelPreferences() { - return XMLCorePlugin.getDefault().getPluginPreferences(); - } - - /** - * This method will find the indentation for this node. It will search - * backwards starting from the beginning of the node until a character - * other than a space or a tab is found. If this node is null or it's a - * document node or it's a first level node (node's parent is a document - * node) the default empty string will be returned as the indentation. - */ - protected String getNodeIndent(Node node) { - String result = EMPTY_STRING; - - if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE) && (node.getParentNode() != null) && (node.getParentNode().getNodeType() != Node.DOCUMENT_NODE)) { - XMLNode siblingTextNode = (XMLNode) node.getPreviousSibling(); - if ((siblingTextNode != null) && (siblingTextNode.getNodeType() == Node.TEXT_NODE)) { - // find the indentation - String siblingText = siblingTextNode.getNodeValue(); - int siblingTextLength = siblingText.length(); - if ((siblingText != null) && (siblingTextLength > 0) && ((siblingText.charAt(siblingTextLength - 1) == SPACE_CHAR) || (siblingText.charAt(siblingTextLength - 1) == TAB_CHAR))) { - int searchIndex = siblingTextLength - 1; - while ((searchIndex >= 0) && ((siblingText.charAt(searchIndex) == SPACE_CHAR) || (siblingText.charAt(searchIndex) == TAB_CHAR))) - searchIndex--; - - if (searchIndex < siblingTextLength) - result = siblingText.substring(searchIndex + 1, siblingTextLength); - } - } - } - - return result; - } - - protected String getNodeName(XMLNode node) { - return node.getNodeName(); - } - - protected String getNodeText(XMLNode node) { - String text = null; - - if ((node instanceof org.eclipse.wst.xml.core.internal.document.CharacterDataImpl) && !(node instanceof org.eclipse.wst.xml.core.internal.document.CommentImpl) && !(node instanceof org.eclipse.wst.xml.core.internal.document.CDATASectionImpl) && !isJSPTag(node)) - text = ((org.eclipse.wst.xml.core.internal.document.CharacterDataImpl) node).getSource(); - else - text = node.getFirstStructuredDocumentRegion().getText(); - - return text; - } - - protected XMLNode getParentIndentNode(XMLNode node) { - XMLNode result = null; - XMLNode parentNode = (XMLNode) node.getParentNode(); - - if (parentNode.getNodeType() == Node.DOCUMENT_NODE) - result = parentNode; - else { - ITextRegion region = parentNode.getLastStructuredDocumentRegion().getFirstRegion(); - if (region.getType() == XMLRegionContext.XML_END_TAG_OPEN) - result = parentNode; - else - result = getParentIndentNode(parentNode); - } - - return result; - } - - /** - * This method will find the indentation for a node sibling to this node. - * It will try to find a sibling node before this node first. If there is - * no sibling node before this node, it will try to find a sibling node - * after this node. If still not found, we will check if this node is - * already indented from its parent. If yes, this node's indentation will - * be used. Otherwise, the parent node's indentation plus one indentation - * will be used. If this node is null or it's a document node or it's a - * first level node (node's parent is a document node) the default empty - * string will be returned as the indentation. - */ - protected String getSiblingIndent(Node node) { - String result = EMPTY_STRING; - - if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE) && (node.getParentNode() != null) && (node.getParentNode().getNodeType() != Node.DOCUMENT_NODE)) { - // find the text node before the previous non-text sibling - // if that's not found, we will try the text node before the next - // non-text sibling - XMLNode sibling = (XMLNode) node.getPreviousSibling(); - while ((sibling != null) && (sibling.getNodeType() == Node.TEXT_NODE || sibling.getNodeType() == Node.COMMENT_NODE)) { - if (sibling.getNodeType() == Node.COMMENT_NODE && sibling.getPreviousSibling() != null && sibling.getPreviousSibling().getNodeType() == Node.TEXT_NODE && StringUtils.containsLineDelimiter(sibling.getPreviousSibling().getNodeValue())) - break; - sibling = (XMLNode) sibling.getPreviousSibling(); - } - if (sibling == null) { - sibling = (XMLNode) node.getNextSibling(); - while ((sibling != null) && (sibling.getNodeType() == Node.TEXT_NODE)) - sibling = (XMLNode) sibling.getNextSibling(); - } - String singleIndent = getFormatPreferences().getIndent(); - String parentLineIndent = getNodeIndent(node.getParentNode()); - - if (sibling != null) { - String siblingIndent = getNodeIndent(sibling); - if (siblingIndent.length() > 0) - result = siblingIndent; - else { - String nodeIndent = getNodeIndent(node); - if (nodeIndent.length() > parentLineIndent.length()) - // this node is indented from its parent, its - // indentation will be used - result = nodeIndent; - else - result = parentLineIndent + singleIndent; - } - } else { - String nodeIndent = getNodeIndent(node); - if (nodeIndent.length() > parentLineIndent.length()) - // this node is indented from its parent, its indentation - // will be used - result = nodeIndent; - else - result = parentLineIndent + singleIndent; - } - } - - return result; - } - - protected void insertAfterNode(XMLNode node, String string) { - XMLModel structuredModel = node.getModel(); - IStructuredDocument structuredDocument = structuredModel.getStructuredDocument(); - - int offset = node.getEndOffset(); - int length = 0; - - // 261968 - formatting tag without closing bracket: <t1><t1 - if (node.getEndStructuredDocumentRegion() != null) { - offset = node.getEndStructuredDocumentRegion().getTextEndOffset(); - length = node.getEndOffset() - offset; - } - replace(structuredDocument, offset, length, string); - } - - protected void insertBeforeNode(XMLNode node, String string) { - XMLModel structuredModel = node.getModel(); - IStructuredDocument structuredDocument = structuredModel.getStructuredDocument(); - - replace(structuredDocument, node.getStartOffset(), 0, string); - } - - /** - * Allowing the INodeAdapter to compare itself against the type allows it - * to return true in more than one case. - */ - public boolean isAdapterForType(Object type) { - return type.equals(IStructuredFormatter.class); - } - - protected boolean isEndTagMissing(XMLNode node) { - boolean result = false; - - if ((node != null) && (node.getNodeType() != Node.DOCUMENT_NODE) && !isJSPTag(node)) { - IStructuredDocumentRegion startTagStructuredDocumentRegion = node.getFirstStructuredDocumentRegion(); - IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getLastStructuredDocumentRegion(); - - ITextRegion startTagNameRegion = null; - if (startTagStructuredDocumentRegion.getRegions().size() > 1) - startTagNameRegion = startTagStructuredDocumentRegion.getRegions().get(1); - ITextRegion endTagNameRegion = null; - if (endTagStructuredDocumentRegion.getRegions().size() > 1) - endTagNameRegion = endTagStructuredDocumentRegion.getRegions().get(1); - - ITextRegionList startTagRegions = startTagStructuredDocumentRegion.getRegions(); - if (startTagNameRegion == endTagNameRegion && startTagNameRegion != null && (startTagRegions.get(0)).getType() != XMLRegionContext.XML_END_TAG_OPEN && (startTagRegions.get(startTagRegions.size() - 1).getType()) != XMLRegionContext.XML_EMPTY_TAG_CLOSE) - // end tag missing - result = true; - } - - return result; - } - - protected boolean isJSPTag(XMLNode node) { - boolean result = false; - - IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion(); - // in some cases, the nodes exists, but hasn't been associated with - // a flatnode yet (the screen updates can be initiated on a different - // thread, - // so the request for a flatnode can come in before the node is fully - // formed. - // if the flatnode is null, we'll just allow the defaults to apply. - // (html adapter in this case). - if (flatNode != null) { - String flatNodeType = flatNode.getType(); - if ((flatNodeType == XMLJSPRegionContexts.JSP_CONTENT) || (flatNodeType == XMLJSPRegionContexts.JSP_EXPRESSION_OPEN) || (flatNodeType == XMLJSPRegionContexts.JSP_SCRIPTLET_OPEN) || (flatNodeType == XMLJSPRegionContexts.JSP_DECLARATION_OPEN) || (flatNodeType == XMLJSPRegionContexts.JSP_DIRECTIVE_CLOSE) || (flatNodeType == XMLJSPRegionContexts.JSP_DIRECTIVE_NAME) || (flatNodeType == XMLJSPRegionContexts.JSP_DIRECTIVE_OPEN) || (flatNodeType == XMLJSPRegionContexts.JSP_CLOSE)) { - result = true; - } - } - - return result; - } - - protected boolean nodeHasSiblings(XMLNode node) { - return (node.getPreviousSibling() != null) || (node.getNextSibling() != null); - } - - /** - * Node changed. No format should be performed automatically. - */ - public void notifyChanged(org.eclipse.wst.sse.core.INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) { - } - - protected void removeRegionSpaces(XMLNode node, IStructuredDocumentRegion flatNode, ITextRegion region) { - if ((region != null) && (region instanceof ContextRegion || region instanceof TagNameRegion) && (flatNode.getEndOffset(region) > flatNode.getTextEndOffset(region))) { - XMLModel structuredModel = node.getModel(); - IStructuredDocument structuredDocument = structuredModel.getStructuredDocument(); - - replace(structuredDocument, flatNode.getTextEndOffset(region), flatNode.getEndOffset(region) - flatNode.getTextEndOffset(region), EMPTY_STRING); - } - } - - /** - * This method will replace the string at offset and length with a new - * string. If the string to be replaced is the same as the new string, the - * string will not be replaced. - */ - protected void replace(IStructuredDocument structuredDocument, int offset, int length, String string) { - try { - String structuredDocumentString = structuredDocument.get(offset, length); - if (structuredDocumentString.compareTo(string) != 0) - structuredDocument.replaceText(structuredDocument, offset, length, string); - } catch (BadLocationException exception) { - throw new SourceEditingRuntimeException(exception); - } - } - - /** - * This method will replace the node value with a new string. If the node - * value to be replaced is the same as the new string, the node value will - * not be replaced. - */ - protected void replaceNodeValue(XMLNode node, String string) { - XMLModel structuredModel = node.getModel(); - IStructuredDocument structuredDocument = structuredModel.getStructuredDocument(); - int offset = node.getStartOffset(); - int length = node.getEndOffset() - node.getStartOffset(); - - try { - String structuredDocumentString = structuredDocument.get(offset, length); - if (structuredDocumentString.compareTo(string) != 0) - replace(structuredDocument, offset, length, string); - } catch (BadLocationException exception) { - throw new SourceEditingRuntimeException(exception); - } - } - - public void setFormatPreferences(IStructuredFormatPreferences formatPreferences) { - fFormatPreferences = formatPreferences; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.wst.sse.core.format.IStructuredFormatter#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) - */ - public void setProgressMonitor(IProgressMonitor monitor) { - fProgressMonitor = monitor; - } -} diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/StructuredFormatPreferencesXML.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/StructuredFormatPreferencesXML.java deleted file mode 100644 index 079ae41c29..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/StructuredFormatPreferencesXML.java +++ /dev/null @@ -1,27 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import org.eclipse.wst.sse.core.format.StructuredFormatPreferences; - -public class StructuredFormatPreferencesXML extends StructuredFormatPreferences implements IStructuredFormatPreferencesXML { - private boolean fSplitMultiAttrs; - - public boolean getSplitMultiAttrs() { - return fSplitMultiAttrs; - } - - public void setSplitMultiAttrs(boolean splitMultiAttrs) { - fSplitMultiAttrs = splitMultiAttrs; - } -} diff --git a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/TextNodeFormatter.java b/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/TextNodeFormatter.java deleted file mode 100644 index 9bc4fe1d56..0000000000 --- a/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/format/TextNodeFormatter.java +++ /dev/null @@ -1,198 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2001, 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 - * Jens Lukowski/Innoopract - initial renaming/restructuring - * - *******************************************************************************/ -package org.eclipse.wst.xml.core.format; - -import java.util.List; -import java.util.Vector; - -import org.eclipse.jface.text.BadLocationException; -import org.eclipse.wst.sse.core.exceptions.SourceEditingRuntimeException; -import org.eclipse.wst.sse.core.format.IStructuredFormatContraints; -import org.eclipse.wst.sse.core.text.IStructuredDocument; -import org.eclipse.wst.sse.core.util.StringUtils; -import org.eclipse.wst.xml.core.document.XMLNode; -import org.w3c.dom.Node; - - -public class TextNodeFormatter extends NodeFormatter { - protected void formatNode(XMLNode node, IStructuredFormatContraints formatContraints) { - if (node != null) { - IStructuredDocument doc = node.getModel().getStructuredDocument(); - int line = doc.getLineOfOffset(node.getStartOffset()); - String lineDelimiter = doc.getLineDelimiter(); - try { - lineDelimiter = doc.getLineDelimiter(line); - if (lineDelimiter == null) - lineDelimiter = ""; //$NON-NLS-1$ - } catch (BadLocationException exception) { - throw new SourceEditingRuntimeException(exception); - } - int lineWidth = getFormatPreferences().getLineWidth(); - XMLNode parentNode = (XMLNode) node.getParentNode(); - String nodeIndentation = formatContraints.getCurrentIndent(); - - // compute current available line width - int currentAvailableLineWidth = 0; - try { - int nodeNameOffset = node.getStartOffset(); - int lineOffset = node.getStructuredDocument().getLineInformationOfOffset(nodeNameOffset).getOffset(); - String text = node.getStructuredDocument().get(lineOffset, nodeNameOffset - lineOffset); - int usedWidth = getIndentationLength(text); - currentAvailableLineWidth = getFormatPreferences().getLineWidth() - usedWidth; - } catch (BadLocationException exception) { - throw new SourceEditingRuntimeException(exception); - } - - String compressedText = getCompressedNodeText(node, formatContraints); - - if (((compressedText.length() <= (currentAvailableLineWidth - node.getParentNode().getNodeName().length() - 3) && !StringUtils.containsLineDelimiter(compressedText)) && (!nodeHasSiblings(node) || (!StringUtils.containsLineDelimiter(node.getNodeValue()) && node.getNextSibling() != null && node.getNextSibling().getNodeType() == Node.COMMENT_NODE && !StringUtils.containsLineDelimiter(node.getNextSibling().getNodeValue()))) && !firstStructuredDocumentRegionContainsLineDelimiters((XMLNode) node.getParentNode())) || node.getStartStructuredDocumentRegion().getStartOffset() == 0) { - // enough space - // and text has no line delimiters - // and (node has no siblings or followed by inline comment) - // and - // parentFirstStructuredDocumentRegionContainsLineDelimiters - - if (isEndTagMissing(parentNode)) { - parentNode = (XMLNode) parentNode.getParentNode(); - while (isEndTagMissing(parentNode)) - parentNode = (XMLNode) parentNode.getParentNode(); - - // add parent's indentation to end - nodeIndentation = getNodeIndent(parentNode); - - if (!compressedText.endsWith(lineDelimiter + nodeIndentation)) { - compressedText = StringUtils.appendIfNotEndWith(compressedText, lineDelimiter); - compressedText = StringUtils.appendIfNotEndWith(compressedText, nodeIndentation); - } - } - - if ((parentNode != null) && (parentNode.getNodeType() == Node.DOCUMENT_NODE) && (node.getNodeValue().length() > 0) && (node.getNodeValue().trim().length() == 0) && ((node.getPreviousSibling() == null) || (node.getNextSibling() == null))) - // delete spaces at the beginning or end of the document - compressedText = EMPTY_STRING; - - replaceNodeValue(node, compressedText); - } else { - // not enough space, need to reflow text - - currentAvailableLineWidth = lineWidth - getIndentationLength(nodeIndentation); - List vector = reflowText(compressedText, currentAvailableLineWidth); - int vectorSize = vector.size(); - String reflowedText = new String(); - - for (int i = 0; i < vectorSize; i++) { - if (((String) vector.get(i)).trim().length() > 0) - reflowedText = reflowedText + lineDelimiter + nodeIndentation + (String) vector.get(i); - else - reflowedText = reflowedText + lineDelimiter; - } - - if (node.getNextSibling() == null) { - if (isEndTagMissing(parentNode)) { - // don't add indentation to end if parent end tag is - // missing - } - - else { - // add parent's indentation to end - nodeIndentation = getNodeIndent(parentNode); - - if (!reflowedText.endsWith(lineDelimiter + nodeIndentation)) { - reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter); - reflowedText = StringUtils.appendIfNotEndWith(reflowedText, nodeIndentation); - } - } - } else { - if (!reflowedText.endsWith(lineDelimiter + nodeIndentation)) { - // not already ended with the expected indentation - - if (node.getNextSibling().getNodeType() == Node.COMMENT_NODE) { - // add indentation to end if - // currentTextEndsWithLineDelimiter - // or followed by multiLineComment - - String nodeText = getNodeText(node); - int indexOfLastLineDelimiter = StringUtils.indexOfLastLineDelimiter(nodeText); - boolean currentTextEndsWithLineDelimiter = indexOfLastLineDelimiter != -1; - if (currentTextEndsWithLineDelimiter) { - // no more non blank character after the last - // line delimiter - currentTextEndsWithLineDelimiter = StringUtils.indexOfNonblank(nodeText, indexOfLastLineDelimiter) == -1; - } - - String nodeValue = node.getNextSibling().getNodeValue(); - boolean multiLineComment = StringUtils.containsLineDelimiter(nodeValue); - - if (currentTextEndsWithLineDelimiter || multiLineComment) { - reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter); - reflowedText = StringUtils.appendIfNotEndWith(reflowedText, nodeIndentation); - } - } else { - // not a comment, just add add indentation to end - reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter); - reflowedText = StringUtils.appendIfNotEndWith(reflowedText, nodeIndentation); - } - } - } - - replaceNodeValue(node, reflowedText); - } - - } - } - - protected Vector reflowText(String text, int availableWidth) { - String[] stringArray = null; - boolean clearAllBlankLines = getFormatPreferences().getClearAllBlankLines(); - - if (clearAllBlankLines) - stringArray = StringUtils.asArray(text); - else - stringArray = StringUtils.asArray(text, DELIMITERS, true); - - Vector output = new Vector(); - if ((stringArray != null) && (stringArray.length > 0)) { - StringBuffer buffer = new StringBuffer(); - if (stringArray[0].compareTo(CR) != 0) - buffer.append(stringArray[0]); - int bufferLength = stringArray[0].toString().length(); - boolean cr = stringArray[0].compareTo(CR) == 0; - - for (int i = 1; i < stringArray.length; i++) { - String eachString = stringArray[i]; - if ((eachString.compareTo(SPACE) != 0) && (eachString.compareTo(TAB) != 0) && (eachString.compareTo(FF) != 0)) { - if ((bufferLength + 1 + eachString.length() > availableWidth) || (eachString.compareTo(CR) == 0) || (eachString.compareTo(LF) == 0)) { - if ((eachString.compareTo(LF) == 0) && cr) { - // do nothing - } else { - output.add(buffer.toString()); - buffer = new StringBuffer(); - bufferLength = 0; - } - cr = eachString.compareTo(CR) == 0; - } else if (buffer.toString().trim().length() > 0) { - buffer.append(SPACE); - bufferLength++; - } - if ((eachString.compareTo(CR) != 0) && (eachString.compareTo(LF) != 0)) { - buffer.append(eachString); - bufferLength = bufferLength + eachString.length(); - } - } - } - output.add(buffer.toString()); - } else - output.add(text); - - return output; - } -} |