use of org.eclipse.wst.xml.core.internal.document.AttrImpl in project webtools.sourceediting by eclipse.
the class TestOrphan method testNonOrphanInBoth.
public void testNonOrphanInBoth() {
Logger.trace(fCategory, "testNonOrphanInBoth");
Document jspDoc = getJSPDoc();
Element commonElement = makeElement(jspDoc);
AttrImpl attr = (AttrImpl) commonElement.getAttributeNode("src");
String attrValue = attr.getValue();
Logger.trace(fCategory, "attrValue: " + attrValue);
jspDoc.appendChild(commonElement);
boolean isJspValue = attr.hasNestedValue();
Logger.trace(fCategory, "isJspValue: " + isJspValue);
assertTrue(isJspValue);
Document htmlDoc = getHTMLDoc();
// this test will cause a "region management error" to be
// thrown in parser (and its subsequent 'handleRefresh').
// this is "normal" in this error case, of appending an jsp
// element to an html document. This error/exception is not
// normally printed out, but is if 'debug' is turned on.
htmlDoc.appendChild(commonElement);
isJspValue = attr.hasNestedValue();
Logger.trace(fCategory, "isJspValue: " + isJspValue);
assertFalse(isJspValue);
}
use of org.eclipse.wst.xml.core.internal.document.AttrImpl 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.document.AttrImpl in project webtools.sourceediting by eclipse.
the class PageDirectiveWatcherImpl method notifyChanged.
public void notifyChanged(INodeNotifier notifier, int eventType, Object changedFeature, Object oldValue, Object newValue, int pos) {
// certain ones chane, so it can make its "centralized" decisions.
if (notifier instanceof IDOMNode) {
switch(eventType) {
case INodeNotifier.CHANGE:
if (changedFeature instanceof AttrImpl) {
AttrImpl attribute = (AttrImpl) changedFeature;
String name = attribute.getName();
if (name.equals("contentType")) {
// $NON-NLS-1$
// using concrete class below, since "changed" is something of an internal method
PageDirectiveAdapterImpl pageDirectiveAdapter = (PageDirectiveAdapterImpl) ((IDOMDocument) targetElement.getOwnerDocument()).getAdapterFor(PageDirectiveAdapter.class);
pageDirectiveAdapter.changedContentType(((IndexedRegion) targetElement).getStartOffset(), (String) newValue);
}
if (name.equals("language")) {
// $NON-NLS-1$ //$NON-NLS-2$
// using concrete class below, since "changed" is something of an internal method
PageDirectiveAdapterImpl pageDirectiveAdapter = (PageDirectiveAdapterImpl) ((IDOMDocument) targetElement.getOwnerDocument()).getAdapterFor(PageDirectiveAdapter.class);
pageDirectiveAdapter.changedLanguage(((IndexedRegion) targetElement).getStartOffset(), (String) newValue);
}
}
break;
case INodeNotifier.REMOVE:
// System.out.println("removed"+new Date().toString());
break;
default:
break;
}
}
}
use of org.eclipse.wst.xml.core.internal.document.AttrImpl in project liferay-ide by liferay.
the class LiferayJspValidator method validateReferenceToJava.
@Override
protected void validateReferenceToJava(IXMLReferenceTo referenceTo, IDOMNode node, IFile file, IValidator validator, IReporter reporter, boolean batchMode) {
if (node instanceof AttrImpl) {
AttrImpl attrNode = (AttrImpl) node;
Node parentNode = attrNode.getOwnerElement();
if (_isSupportedTag(parentNode.getNodeName())) {
IDOMAttr nameAttr = DOMUtils.getAttr((IDOMElement) parentNode, "name");
if ((nameAttr != null) && (nameAttr.getNodeValue().contains(_action_request_action_name) || nameAttr.getNodeValue().contains(_javax_portlet_action))) {
super.validateReferenceToJava(referenceTo, attrNode, file, validator, reporter, batchMode);
}
}
}
}
use of org.eclipse.wst.xml.core.internal.document.AttrImpl in project liferay-ide by liferay.
the class LiferayJspValidator method validateReferenceToStatic.
protected void validateReferenceToStatic(IXMLReferenceTo referenceTo, IDOMNode node, IFile file, IValidator validator, IReporter reporter, boolean batchMode) {
if (node instanceof AttrImpl) {
AttrImpl attrNode = (AttrImpl) node;
Element ownerElement = attrNode.getOwnerElement();
if (ownerElement.getNodeName().startsWith(_aui_prefix)) {
String nodeValue = node.toString();
boolean addMessage = false;
if (nodeValue.equals("class")) {
addMessage = true;
}
if (addMessage) {
ValidationType validationType = getValidationType(referenceTo, 0);
int severity = getServerity(validationType, file);
if (severity != ValidationMessage.IGNORE) {
String liferayPluginValidationType = getLiferayPluginValidationType(validationType, file);
String querySpecificationId = referenceTo.getQuerySpecificationId();
String messageText = getMessageText(validationType, referenceTo, node, file);
addMessage(node, file, validator, reporter, batchMode, messageText, severity, liferayPluginValidationType, querySpecificationId);
}
}
} else {
super.validateReferenceToStatic(referenceTo, attrNode, file, validator, reporter, batchMode);
}
}
}
Aggregations