use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class ElementNodeFormatter method formatStartTag.
/**
* This method formats the start tag name, and formats the attributes if
* available.
*/
protected void formatStartTag(IDOMNode node, IStructuredFormatContraints formatContraints) {
StructuredFormatPreferencesXML preferences = (StructuredFormatPreferencesXML) getFormatPreferences();
String singleIndent = preferences.getIndent();
String lineIndent = formatContraints.getCurrentIndent();
String attrIndent = lineIndent + singleIndent;
boolean splitMultiAttrs = preferences.getSplitMultiAttrs();
boolean alignEndBracket = preferences.isAlignEndBracket();
boolean sawXmlSpace = false;
IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion();
NamedNodeMap attributes = node.getAttributes();
// 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 = preferences.getLineWidth() - usedWidth;
} catch (BadLocationException e) {
// log for now, unless we find reason not to
Logger.log(Logger.INFO, e.getMessage());
}
StringBuffer stringBuffer = new StringBuffer();
String lineDelimiter = node.getModel().getStructuredDocument().getLineDelimiter();
int attrLength = attributes.getLength();
int lastUndefinedRegionOffset = 0;
boolean startTagSpansOver1Line = false;
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) {
// [111674] If nothing has been written yet, treat as
// preserve, but only as hint
formatContraints.setInPreserveSpaceElement(true);
// Note we don't set 'sawXmlSpace', so that default or
// fixed DTD/XSD values may override.
} else {
ISourceGenerator generator = node.getModel().getGenerator();
String newAttrValue = generator.generateAttrValue(attr);
// 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.setInPreserveSpaceElement(true);
else
formatContraints.setInPreserveSpaceElement(false);
sawXmlSpace = true;
}
}
if (splitMultiAttrs && attrLength > 1) {
stringBuffer.append(lineDelimiter + attrIndent);
stringBuffer.append(flatNode.getText(nameRegion));
startTagSpansOver1Line = true;
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() != DOMRegionContext.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);
startTagSpansOver1Line = true;
currentAvailableLineWidth = preferences.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);
startTagSpansOver1Line = true;
currentAvailableLineWidth = preferences.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);
IDOMModel 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() == DOMRegionContext.XML_END_TAG_OPEN)
// skip formatting for end tags in this format: </tagName>
return;
else {
if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE || lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE)
length = length - lastRegion.getLength();
if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
// unless already going to move end bracket
if (!startTagSpansOver1Line || !alignEndBracket)
stringBuffer.append(SPACE_CHAR);
}
}
}
if (startTagSpansOver1Line && alignEndBracket) {
stringBuffer.append(lineDelimiter).append(lineIndent);
}
replace(structuredDocument, offset, length, stringBuffer.toString());
// BUG108074 & BUG84688 - preserve whitespace in xsl:text &
// xsl:attribute
String nodeNamespaceURI = node.getNamespaceURI();
if (XSL_NAMESPACE.equals(nodeNamespaceURI)) {
String nodeName = ((Element) node).getLocalName();
if (XSL_ATTRIBUTE.equals(nodeName) || XSL_TEXT.equals(nodeName)) {
sawXmlSpace = true;
formatContraints.setInPreserveSpaceElement(true);
}
}
// leave that to the validator.
if (!sawXmlSpace) {
ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node);
if (elementDeclaration != null) {
int contentType = elementDeclaration.getContentType();
if (preferences.isPreservePCDATAContent() && contentType == CMElementDeclaration.PCDATA) {
formatContraints.setInPreserveSpaceElement(true);
} else {
CMNamedNodeMap cmAttributes = elementDeclaration.getAttributes();
// Check implied values from the DTD way.
CMAttributeDeclaration attributeDeclaration = (CMAttributeDeclaration) cmAttributes.getNamedItem(XML_SPACE);
if (attributeDeclaration != null) {
// CMAttributeDeclaration found, check it out.
String defaultValue = attributeDeclaration.getAttrType().getImpliedValue();
// everything else means back to default.
if (PRESERVE.compareTo(defaultValue) == 0)
formatContraints.setInPreserveSpaceElement(true);
else
formatContraints.setInPreserveSpaceElement(false);
}
}
}
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class ModelQueryTester method testBodyElement.
/**
* Test the HTML BODY Element for the "bgcolor" attribute declaration
*/
public void testBodyElement() {
setUpHTML();
// set
fModel.getStructuredDocument().set("<html><body bgcolor=\"#ffffff\"><form method=\"post\"></form></body></html>");
// text
// TEST getting a CMElementDeclaration
// node at
Element bodyElement = (Element) fModel.getIndexedRegion(7);
// offset7--should
// be
// <body>
CMElementDeclaration bodyDecl = fModelQuery.getCMElementDeclaration(bodyElement);
int contentType = bodyDecl.getContentType();
assertTrue("CMElementDeclaration CONTENT TYPE INCORRECT FOR BODY", (contentType == CMElementDeclaration.MIXED));
// get permissible attrs
CMNamedNodeMap map = bodyDecl.getAttributes();
assertTrue("ATTRIBUTES FROM ELEMENT DECLARATION == NULL", (map != null));
Vector allowed = new Vector();
for (int i = 0; i < map.getLength(); i++) {
CMAttributeDeclaration node = (CMAttributeDeclaration) map.item(i);
String name = node.getNodeName();
allowed.add(name);
if (name.equalsIgnoreCase("bgcolor")) {
assertTrue("GOT INCORRECT ATTRIBUTE NODE TYPE", (node.getNodeType() == CMNode.ATTRIBUTE_DECLARATION));
CMDataType attrType = node.getAttrType();
// System.out.println("attribute type > " + attrType);
assertTrue("COULD NOT GET ATTRIBUTE TYPE", (attrType != null));
assertTrue("COULDN'T GET IMPLIED VALUE KIND", (attrType.getImpliedValueKind() == CMDataType.IMPLIED_VALUE_NONE));
}
}
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class XMLPropertySource method updatePropertyDescriptors.
protected void updatePropertyDescriptors() {
if ((fDescriptors == null) || (fDescriptors.length == 0)) {
// Nothing to update
return;
}
// List of all names encountered in the tag and defined by the element
List declaredNames = new ArrayList();
// New descriptor list that will become fDescriptors after all
// processing is done
List descriptors = new ArrayList();
// Names of the descriptors in the above List
List descriptorNames = new ArrayList();
// Update any descriptors derived from the metainfo
CMElementDeclaration ed = getDeclaration();
CMNamedNodeMap attrMap = null;
if (ed != null) {
attrMap = ed.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument());
if (modelQuery != null) {
List nodes = modelQuery.getAvailableContent((Element) fNode, ed, 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);
}
}
}
attrMap = allAttributes;
}
// Update exiting descriptors; not added to the final list here
if (attrMap != null) {
// Update existing descriptor types based on metainfo
CMAttributeDeclaration attrDecl = null;
for (int i = 0; i < attrMap.getLength(); i++) {
attrDecl = (CMAttributeDeclaration) attrMap.item(i);
String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
if (!declaredNames.contains(attrName)) {
declaredNames.add(attrName);
}
for (int j = 0; j < fDescriptors.length; j++) {
boolean sameName = (fCaseSensitive && fDescriptors[j].getId().equals(attrDecl.getNodeName())) || (!fCaseSensitive && attrDecl.getNodeName().equals(fDescriptors[j].getId().toString()));
if (sameName) {
String[] validValues = getValidValues(attrDecl);
// updated for now)
if (fDescriptors[j] instanceof EnumeratedStringPropertyDescriptor) {
((EnumeratedStringPropertyDescriptor) fDescriptors[j]).updateValues(validValues);
} else // Replace with better descriptor
if ((validValues != null) && (validValues.length > 0)) {
fDescriptors[j] = createPropertyDescriptor(attrDecl, null);
}
}
}
}
} else {
// Update existing descriptors based on not having any metainfo
for (int j = 0; j < fDescriptors.length; j++) {
// Replace with basic descriptor
if (!(fDescriptors[j] instanceof TextPropertyDescriptor)) {
fDescriptors[j] = createDefaultPropertyDescriptor((String) fDescriptors[j].getId());
}
}
}
NamedNodeMap attributes = fNode.getAttributes();
// are present or declared
for (int i = 0; i < fDescriptors.length; i++) {
if (fDescriptors[i] != null) {
String descriptorName = fDescriptors[i].getId().toString();
if ((declaredNames.contains(descriptorName) || (attributes.getNamedItem(descriptorName) != null)) && !descriptorNames.contains(descriptorName)) {
descriptorNames.add(descriptorName);
descriptors.add(fDescriptors[i]);
}
}
}
// Add descriptors for declared attributes that don't already have one
if (attrMap != null) {
// Update existing descriptor types based on metainfo
CMAttributeDeclaration attrDecl = null;
for (int i = 0; i < attrMap.getLength(); i++) {
attrDecl = (CMAttributeDeclaration) attrMap.item(i);
String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
if (fCaseSensitive) {
if (!descriptorNames.contains(attrName)) {
IPropertyDescriptor descriptor = createPropertyDescriptor(attrDecl, null);
if (descriptor != null) {
descriptorNames.add(attrName);
descriptors.add(descriptor);
}
}
} else {
boolean exists = false;
for (int j = 0; j < descriptorNames.size(); j++) {
exists = (descriptorNames.get(j).toString().equalsIgnoreCase(attrName)) || exists;
}
if (!exists) {
descriptorNames.add(attrName);
IPropertyDescriptor descriptor = createPropertyDescriptor(attrDecl, null);
if (descriptor != null) {
descriptorNames.add(attrName);
descriptors.add(descriptor);
}
}
}
}
}
// Add descriptors for existing attributes that don't already have one
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
Attr attr = (Attr) attributes.item(i);
String attrName = attr.getName();
if (fCaseSensitive) {
if (!descriptorNames.contains(attrName)) {
descriptorNames.add(attrName);
descriptors.add(createDefaultPropertyDescriptor(attrName));
}
} else {
boolean exists = false;
for (int j = 0; j < descriptorNames.size(); j++) {
exists = (descriptorNames.get(j).toString().equalsIgnoreCase(attrName)) || exists;
}
if (!exists) {
descriptorNames.add(attrName);
descriptors.add(createDefaultPropertyDescriptor(attrName));
}
}
}
}
// Update fDescriptors
IPropertyDescriptor[] newDescriptors = new IPropertyDescriptor[descriptors.size()];
for (int i = 0; i < newDescriptors.length; i++) {
newDescriptors[i] = (IPropertyDescriptor) descriptors.get(i);
}
fDescriptors = newDescriptors;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class XMLPropertySource method createPropertyDescriptors.
/**
* Returns the current collection of property descriptors.
*
* @return all valid descriptors.
*/
private IPropertyDescriptor[] createPropertyDescriptors() {
CMNamedNodeMap attrMap = null;
CMElementDeclaration ed = getDeclaration();
if (ed != null) {
attrMap = ed.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
List nodes = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument()).getAvailableContent((Element) fNode, ed, 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);
}
}
attrMap = allAttributes;
}
List descriptorList = new ArrayList();
List names = new ArrayList();
IPropertyDescriptor descriptor;
CMAttributeDeclaration attrDecl = null;
// add descriptors for existing attributes
NamedNodeMap attributes = fNode.getAttributes();
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
Attr attr = (Attr) attributes.item(i);
// CMAttributeDeclaration to derive a descriptor
if (attrMap != null) {
String attrName = attr.getName();
if (fCaseSensitive) {
attrDecl = (CMAttributeDeclaration) attrMap.getNamedItem(attrName);
} else {
attrDecl = null;
for (int j = 0; j < attrMap.getLength(); j++) {
if (!fCaseSensitive && attrMap.item(j).getNodeName().equalsIgnoreCase(attrName)) {
attrDecl = (CMAttributeDeclaration) attrMap.item(j);
break;
}
}
}
}
// be consistent: if there's metainfo, use *that* as the
// descriptor ID
descriptor = null;
if (attrDecl != null) {
String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
if (!names.contains(attrName)) {
descriptor = createPropertyDescriptor(attrDecl, attr);
if (descriptor != null)
names.add(attrName);
}
} else {
if (!names.contains(attr.getName())) {
descriptor = createDefaultPropertyDescriptor(attr.getName());
if (descriptor != null)
names.add(attr.getName());
}
}
if (descriptor != null) {
descriptorList.add(descriptor);
}
}
}
// add descriptors from the metainfo that are not yet listed
if (attrMap != null) {
for (int i = 0; i < attrMap.getLength(); i++) {
attrDecl = (CMAttributeDeclaration) attrMap.item(i);
String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
if (!names.contains(attrName)) {
IPropertyDescriptor holdDescriptor = createPropertyDescriptor(attrDecl, null);
if (holdDescriptor != null) {
names.add(attrName);
descriptorList.add(holdDescriptor);
}
}
}
}
// add MQE-based descriptors
if (ed != null && fNode.getNodeType() == Node.ELEMENT_NODE) {
List nodes = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument()).getAvailableContent((Element) fNode, ed, ModelQuery.INCLUDE_ATTRIBUTES);
for (int i = 0; i < nodes.size(); i++) {
CMNode node = (CMNode) nodes.get(i);
if (node.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
attrDecl = (CMAttributeDeclaration) node;
String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
if (!names.contains(attrName)) {
IPropertyDescriptor holdDescriptor = createPropertyDescriptor(attrDecl, null);
if (holdDescriptor != null) {
names.add(attrName);
descriptorList.add(holdDescriptor);
}
}
}
}
}
IPropertyDescriptor[] descriptors = new IPropertyDescriptor[descriptorList.size()];
for (int i = 0; i < descriptors.length; i++) {
descriptors[i] = (IPropertyDescriptor) descriptorList.get(i);
}
return descriptors;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap in project webtools.sourceediting by eclipse.
the class XMLTagInfoHoverProcessor method getCMAttributeDeclaration.
/**
* Retreives CMAttributeDeclaration indicated by attribute name within
* elementDecl
*/
protected CMAttributeDeclaration getCMAttributeDeclaration(IDOMNode node, CMElementDeclaration elementDecl, String attName) {
CMAttributeDeclaration attrDecl = null;
if (elementDecl != null) {
CMNamedNodeMap attributes = elementDecl.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes);
List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, 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);
}
}
attributes = allAttributes;
String noprefixName = DOMNamespaceHelper.getUnprefixedName(attName);
if (attributes != null) {
attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(noprefixName);
if (attrDecl == null) {
attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(attName);
}
}
}
return attrDecl;
}
Aggregations