use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class XMLModelParser method insertCDATASection.
/**
* insertCDATASection method
*/
private void insertCDATASection(IStructuredDocumentRegion flatNode) {
ITextRegionList regions = flatNode.getRegions();
if (regions == null)
return;
CDATASectionImpl cdata = null;
try {
cdata = (CDATASectionImpl) this.model.getDocument().createCDATASection(null);
} catch (DOMException ex) {
}
if (cdata == null) {
// CDATA section might not be supported
// regard as invalid decl
insertInvalidDecl(flatNode);
return;
}
cdata.setStructuredDocumentRegion(flatNode);
insertNode(cdata);
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class XMLModelParser method insertPI.
/**
* insertPI method
*/
private void insertPI(IStructuredDocumentRegion flatNode) {
ITextRegionList regions = flatNode.getRegions();
if (regions == null)
return;
String target = null;
Iterator e = regions.iterator();
while (e.hasNext()) {
ITextRegion region = (ITextRegion) e.next();
String regionType = region.getType();
if (regionType == DOMRegionContext.XML_PI_OPEN || regionType == DOMRegionContext.XML_PI_CLOSE)
continue;
if (target == null)
target = flatNode.getText(region);
}
ProcessingInstructionImpl pi = (ProcessingInstructionImpl) this.model.getDocument().createProcessingInstruction(target, null);
if (pi == null)
return;
pi.setStructuredDocumentRegion(flatNode);
insertNode(pi);
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method formatStartTag.
/**
* Formats a start tag
*
* @param textEdit
* @param currentRegion
* @param textRegions
*/
private DOMRegion formatStartTag(TextEdit textEdit, Position formatRange, XMLFormattingConstraints parentConstraints, DOMRegion currentDOMRegion, IStructuredDocumentRegion previousDocumentRegion) {
// determine proper indent by referring to parent constraints,
// previous node, and current node
IStructuredDocumentRegion currentDocumentRegion = currentDOMRegion.documentRegion;
IDOMNode currentDOMNode = currentDOMRegion.domNode;
// create a constraint for this tag
XMLFormattingConstraints thisConstraints = new XMLFormattingConstraints();
XMLFormattingConstraints childrenConstraints = new XMLFormattingConstraints();
updateFormattingConstraints(parentConstraints, thisConstraints, childrenConstraints, currentDOMRegion);
if (XMLFormattingConstraints.DEFAULT.equals(childrenConstraints.getWhitespaceStrategy()))
childrenConstraints.setWhitespaceStrategy((new XMLFormattingPreferences()).getElementWhitespaceStrategy());
String whitespaceStrategy = thisConstraints.getWhitespaceStrategy();
String indentStrategy = thisConstraints.getIndentStrategy();
int availableLineWidth = thisConstraints.getAvailableLineWidth();
// do not format space before start tag if preserving spaces
if (!XMLFormattingConstraints.PRESERVE.equals(whitespaceStrategy)) {
// format like indent strategy says
if (XMLFormattingConstraints.INDENT.equals(indentStrategy) || XMLFormattingConstraints.NEW_LINE.equals(indentStrategy)) {
availableLineWidth = indentIfPossible(textEdit, thisConstraints, currentDocumentRegion, previousDocumentRegion, whitespaceStrategy, indentStrategy, true, true);
if (availableLineWidth > 0)
thisConstraints.setAvailableLineWidth(availableLineWidth);
}
}
// format the start tag itself
boolean tagEnded = formatWithinTag(textEdit, thisConstraints, currentDocumentRegion, previousDocumentRegion);
// format children
if (!tagEnded) {
// update childConstraints with thisConstraint's indentLevel &
// availableLineWidth
childrenConstraints.setIndentLevel(thisConstraints.getIndentLevel());
childrenConstraints.setAvailableLineWidth(thisConstraints.getAvailableLineWidth());
previousDocumentRegion = currentDocumentRegion;
IDOMNode childDOMNode = (IDOMNode) currentDOMNode.getFirstChild();
IStructuredDocumentRegion nextRegion = currentDocumentRegion.getNext();
boolean passedFormatRange = false;
// as long as there is one child
if (childDOMNode != null && nextRegion != null) {
while (childDOMNode != null && nextRegion != null && !passedFormatRange && (fProgressMonitor == null || !fProgressMonitor.isCanceled())) {
DOMRegion childDOMRegion = new DOMRegion();
childDOMRegion.documentRegion = nextRegion;
childDOMRegion.domNode = childDOMNode;
if (nextRegion.equals(childDOMNode.getFirstStructuredDocumentRegion())) {
// format children. pass in child constraints
childDOMRegion = formatRegion(textEdit, formatRange, childrenConstraints, childDOMRegion, previousDocumentRegion);
} else {
// TODO: what happens if they dont match up?
}
// update childDOMRegion with next dom/region node
if (childDOMRegion.domNode != null) {
childDOMNode = (IDOMNode) childDOMRegion.domNode.getNextSibling();
} else {
childDOMNode = null;
}
previousDocumentRegion = childDOMRegion.documentRegion;
nextRegion = previousDocumentRegion.getNext();
if (nextRegion != null)
passedFormatRange = !formatRange.overlapsWith(nextRegion.getStartOffset(), nextRegion.getLength());
}
} else {
// there were no children, so keep end tag inlined
childrenConstraints.setWhitespaceStrategy(XMLFormattingConstraints.COLLAPSE);
childrenConstraints.setIndentStrategy(XMLFormattingConstraints.INLINE);
}
if (!passedFormatRange) {
// update the dom region with the last formatted region/dom
// node should be end tag and this tag's DOMNode
currentDOMRegion.documentRegion = nextRegion;
currentDOMRegion.domNode = currentDOMNode;
// end tag's indent level should be same as start tag's
childrenConstraints.setIndentLevel(thisConstraints.getIndentLevel());
// format end tag
boolean formatEndTag = false;
if (nextRegion != null && currentDOMNode != null) {
ITextRegionList rs = nextRegion.getRegions();
if (rs.size() > 1) {
ITextRegion r = rs.get(0);
if (r != null && DOMRegionContext.XML_END_TAG_OPEN.equals(r.getType())) {
r = rs.get(1);
if (r != null && DOMRegionContext.XML_TAG_NAME.equals(r.getType())) {
String tagName = nextRegion.getText(r);
if (tagName != null && tagName.equals(currentDOMNode.getNodeName()))
formatEndTag = true;
}
}
}
}
if (formatEndTag)
formatEndTag(textEdit, formatRange, childrenConstraints, currentDOMRegion, previousDocumentRegion);
else {
// missing end tag so return last formatted document
// region
currentDOMRegion.documentRegion = previousDocumentRegion;
}
} else {
// passed format range before could finish, so update dom
// region to last known formatted region
currentDOMRegion.documentRegion = nextRegion;
currentDOMRegion.domNode = childDOMNode;
}
// update parent constraint since this is what is passed back
parentConstraints.setAvailableLineWidth(childrenConstraints.getAvailableLineWidth());
} else {
// update available line width
parentConstraints.setAvailableLineWidth(thisConstraints.getAvailableLineWidth());
}
return currentDOMRegion;
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method updateFormattingConstraints.
/**
* Given the provided information (parentConstraints & currentDOMRegion),
* update the formatting constraints (for this & child)
*
* @param parentConstraints
* can be null
* @param thisConstraints
* can be null
* @param childConstraints
* can be null
* @param currentDOMRegion
* cannot be null
*/
protected void updateFormattingConstraints(XMLFormattingConstraints parentConstraints, XMLFormattingConstraints thisConstraints, XMLFormattingConstraints childConstraints, DOMRegion currentDOMRegion) {
IStructuredDocumentRegion currentRegion = currentDOMRegion.documentRegion;
IDOMNode currentNode = currentDOMRegion.domNode;
// default to whatever parent's constraint said to do
if (parentConstraints != null) {
if (thisConstraints != null) {
thisConstraints.copyConstraints(parentConstraints);
}
if (childConstraints != null) {
childConstraints.copyConstraints(parentConstraints);
// defaults are taken instead
if (parentConstraints.isWhitespaceStrategyAHint())
childConstraints.setWhitespaceStrategy(null);
}
}
// set up constraints for direct children of document root
Node parentNode = currentNode.getParentNode();
if (parentNode != null && parentNode.getNodeType() == Node.DOCUMENT_NODE) {
if (thisConstraints != null) {
thisConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
thisConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
thisConstraints.setIndentLevel(0);
}
if (childConstraints != null) {
childConstraints.setWhitespaceStrategy(null);
childConstraints.setIndentStrategy(null);
childConstraints.setIndentLevel(0);
}
}
// other conditions to check when setting up child constraints
if (childConstraints != null) {
XMLFormattingPreferences preferences = getFormattingPreferences();
// on a new line and have an indent level of 0
if (currentNode.getNodeType() == Node.DOCUMENT_NODE) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
childConstraints.setIndentStrategy(XMLFormattingConstraints.NEW_LINE);
childConstraints.setIndentLevel(0);
} else {
// BUG108074 & BUG84688 - preserve whitespace in xsl:text &
// xsl:attribute
String nodeNamespaceURI = currentNode.getNamespaceURI();
if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
String nodeName = ((Element) currentNode).getLocalName();
if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
}
} else {
// search within current tag for xml:space attribute
ITextRegionList textRegions = currentRegion.getRegions();
int i = 0;
boolean xmlSpaceFound = false;
boolean preserveFound = false;
while (i < textRegions.size() && !xmlSpaceFound) {
ITextRegion textRegion = textRegions.get(i);
if (DOMRegionContext.XML_TAG_ATTRIBUTE_NAME.equals(textRegion.getType())) {
String regionText = currentRegion.getText(textRegion);
if (XML_SPACE.equals(regionText)) {
if ((i + 1) < textRegions.size()) {
++i;
textRegion = textRegions.get(i);
if (DOMRegionContext.XML_TAG_ATTRIBUTE_EQUALS.equals(textRegion.getType()) && ((i + 1) < textRegions.size())) {
++i;
textRegion = textRegions.get(i);
regionText = currentRegion.getText(textRegion);
if (PRESERVE.equals(regionText) || PRESERVE_QUOTED.equals(regionText)) {
preserveFound = true;
}
}
}
xmlSpaceFound = true;
}
}
++i;
}
if (xmlSpaceFound) {
if (preserveFound) {
// preserve was found so set the strategy
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
} else {
// xml:space was found but it was not collapse, so
// use default whitespace strategy
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
}
} else {
// how to hande nodes that have nonwhitespace text
// content
NodeList nodeList = currentNode.getChildNodes();
int length = nodeList.getLength();
int index = 0;
boolean textNodeFound = false;
// still reflect the parent constraints
while (index < length && !textNodeFound && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
Node childNode = nodeList.item(index);
if (childNode.getNodeType() == Node.TEXT_NODE) {
textNodeFound = !((IDOMText) childNode).isElementContentWhitespace();
}
++index;
}
if (textNodeFound) {
if (length > 1) {
// more in here than just text, so consider
// this mixed content
childConstraints.setWhitespaceStrategy(preferences.getMixedWhitespaceStrategy());
childConstraints.setIndentStrategy(preferences.getMixedIndentStrategy());
} else {
// there's only text
childConstraints.setWhitespaceStrategy(preferences.getTextWhitespaceStrategy());
childConstraints.setIndentStrategy(preferences.getTextIndentStrategy());
}
childConstraints.setIsWhitespaceStrategyAHint(true);
childConstraints.setIsIndentStrategyAHint(true);
}
// try referring to content model for information on
// whitespace & indent strategy
ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) currentNode.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(currentNode);
if (elementDeclaration != null) {
// follow whitespace strategy preference for
// pcdata content
int contentType = elementDeclaration.getContentType();
String facetValue = null;
if (elementDeclaration.getDataType() != null)
facetValue = (String) elementDeclaration.getDataType().getProperty(PROPERTY_WHITESPACE_FACET);
if (facetValue != null) {
if (PRESERVE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
else // For XSD types, "collapse" corresponds to the IGNOREANDTRIM strategy
if (COLLAPSE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNOREANDTRIM);
else if (REPLACE.equals(facetValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.REPLACE);
} else if (contentType == CMElementDeclaration.PCDATA && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
childConstraints.setWhitespaceStrategy(preferences.getPCDataWhitespaceStrategy());
} else if (contentType == CMElementDeclaration.ELEMENT && parentConstraints != null && !XMLFormattingConstraints.PRESERVE.equals(parentConstraints.getWhitespaceStrategy())) {
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.IGNORE);
childConstraints.setIndentStrategy(XMLFormattingConstraints.INDENT);
childConstraints.setIsWhitespaceStrategyAHint(true);
childConstraints.setIsIndentStrategyAHint(true);
} else {
// look for xml:space in content model
CMNamedNodeMap cmAttributes = elementDeclaration.getAttributes();
// Not needed - we're looking for xml:space
// CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(cmAttributes);
// List nodes = ModelQueryUtil.getModelQuery(currentNode.getOwnerDocument()).getAvailableContent((Element) currentNode, elementDeclaration, ModelQuery.INCLUDE_ATTRIBUTES);
// for (int k = 0; k < nodes.size(); k++) {
// CMNode cmnode = (CMNode) nodes.get(k);
// if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
// allAttributes.put(cmnode);
// }
// }
// cmAttributes = allAttributes;
// Check implied values from the DTD way.
CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) cmAttributes.getNamedItem(XML_SPACE);
if (attributeDeclaration != null) {
// CMAttributeDeclaration found, check
// it
// out.
// BUG214516/196544 - Fixed NPE that was caused by an attr having
// a null attr type
String defaultValue = null;
CMDataType attrType = attributeDeclaration.getAttrType();
if (attrType != null) {
if ((attrType.getImpliedValueKind() != CMDataType.IMPLIED_VALUE_NONE) && attrType.getImpliedValue() != null)
defaultValue = attrType.getImpliedValue();
else if ((attrType.getEnumeratedValues() != null) && (attrType.getEnumeratedValues().length > 0)) {
defaultValue = attrType.getEnumeratedValues()[0];
}
}
// default.
if (PRESERVE.equals(defaultValue))
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.PRESERVE);
else
childConstraints.setWhitespaceStrategy(XMLFormattingConstraints.DEFAULT);
} else // If the node has no attributes, inherit the parents whitespace strategy
{
if (parentConstraints != null)
childConstraints.setWhitespaceStrategy(parentConstraints.getWhitespaceStrategy());
else
childConstraints.setWhitespaceStrategy(null);
}
}
}
}
}
}
// set default values according to preferences
if (childConstraints.getWhitespaceStrategy() == null) {
childConstraints.setWhitespaceStrategy(preferences.getElementWhitespaceStrategy());
}
if (childConstraints.getIndentStrategy() == null) {
childConstraints.setIndentStrategy(preferences.getElementIndentStrategy());
}
}
}
use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList in project webtools.sourceediting by eclipse.
the class DefaultXMLPartitionFormatter method formatWithinEndTag.
private void formatWithinEndTag(TextEdit textEdit, XMLFormattingConstraints constraints, IStructuredDocumentRegion currentDocumentRegion, IStructuredDocumentRegion previousDocumentRegion) {
String indentStrategy = constraints.getIndentStrategy();
String whitespaceStrategy = constraints.getWhitespaceStrategy();
int availableLineWidth = constraints.getAvailableLineWidth();
ITextRegionList textRegions = currentDocumentRegion.getRegions();
int currentNumberOfRegions = currentDocumentRegion.getNumberOfRegions();
int currentTextRegionIndex = 1;
ITextRegion currentTextRegion = textRegions.get(currentTextRegionIndex);
String currentType = currentTextRegion.getType();
// tag name should always be the first text region
if (DOMRegionContext.XML_TAG_NAME.equals(currentType) && currentTextRegionIndex < currentNumberOfRegions - 1) {
ITextRegion nextTextRegion = textRegions.get(currentTextRegionIndex + 1);
// Bug 221279 - Some non well-formed documents will not contribute a next region
if (nextTextRegion != null && DOMRegionContext.XML_TAG_CLOSE.equals(nextTextRegion.getType())) {
// calculate available line width
int tagNameLineWidth = currentTextRegion.getTextLength() + 3;
availableLineWidth -= tagNameLineWidth;
if (XMLFormattingConstraints.INLINE.equals(indentStrategy)) {
// width - Whitespace may have been corrected in the text content
if (availableLineWidth < 0 && XMLFormattingConstraints.IGNORE.equals(whitespaceStrategy)) {
// need to deindent if possible
int lineWidth = indentIfPossible(textEdit, constraints, currentDocumentRegion, previousDocumentRegion, whitespaceStrategy, indentStrategy, false);
// update available line width
if (lineWidth > 0)
availableLineWidth = lineWidth - tagNameLineWidth;
} else {
// needed
if (previousDocumentRegion != null) {
if (DOMRegionContext.XML_CONTENT.equals(previousDocumentRegion.getType())) {
String previousDocumentRegionText = previousDocumentRegion.getFullText();
if (previousDocumentRegionText.trim().length() == 0) {
availableLineWidth = collapseSpaces(textEdit, previousDocumentRegion.getStartOffset(), availableLineWidth, previousDocumentRegionText);
}
}
}
}
}
// delete any trail spaces after tag name
if (currentTextRegion.getTextLength() < currentTextRegion.getLength()) {
deleteTrailingSpaces(textEdit, currentTextRegion, currentDocumentRegion);
}
}
} else {
// end tag has unexpected stuff, so just leave it alone
}
constraints.setAvailableLineWidth(availableLineWidth);
}
Aggregations