use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.
the class CreatingJSPExpression method testCreateJSPExpression.
public void testCreateJSPExpression() throws IOException {
// First make (empty) structuredDocument
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel model = modelManager.createUnManagedStructuredModelFor(ContentTypeIdForJSP.ContentTypeID_JSP);
assertTrue("model could not be created!", model != null);
// Now, assigning use a page directive, but leaving embedded type the same as default
model.getStructuredDocument().setText(this, "<%@ page contentType=\"text/html\" language=\"java\" %>");
Document doc = ((IDOMModel) model).getDocument();
Element jspexpression = doc.createElement("jsp:expression");
((IDOMElement) jspexpression).setJSPTag(true);
doc.appendChild(jspexpression);
Text javacode = doc.createTextNode(" // some java code here; if (x <0) return new String(\"0\") else return new String (\"1\"); ");
jspexpression.appendChild(javacode);
Text cdatasection = doc.createCDATASection(" // some cdata java code here; if (x <0) return new String(\"0\") else return new String (\"1\"); ");
doc.appendChild(cdatasection);
// format's not needed, just prettier ... not sure why
// it won't work right on whole document.
new NodeFormatter().format(jspexpression);
new NodeFormatter().format(cdatasection);
System.out.println("document text: ");
System.out.println(model.getStructuredDocument().get());
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.
the class AddDocumentationCommand method execute.
public void execute() {
if (input instanceof XSDSchema) {
ensureSchemaElement((XSDSchema) input);
}
try {
beginRecording(input.getElement());
xsdAnnotation = XSDCommonUIUtils.getInputXSDAnnotation(input, true);
Element element = xsdAnnotation.getElement();
List documentationList = xsdAnnotation.getUserInformation();
documentationElement = null;
documentationExists = false;
if (documentationList.size() > 0) {
documentationExists = true;
documentationElement = (Element) documentationList.get(0);
}
if (documentationElement == null) {
documentationElement = xsdAnnotation.createUserInformation(null);
element.appendChild(documentationElement);
formatChild(documentationElement);
// Defect in model....I create it but the model object doesn't appear
// to be updated
xsdAnnotation.updateElement();
xsdAnnotation.setElement(element);
}
if (documentationElement.hasChildNodes()) {
if (documentationElement instanceof IDOMElement) {
IDOMElement domElement = (IDOMElement) documentationElement;
Node firstChild = documentationElement.getFirstChild();
Node lastChild = documentationElement.getLastChild();
int start = 0;
int end = 0;
// IDOMModel model = domElement.getModel();
// IDOMDocument doc = model.getDocument();
IDOMNode first = null;
if (firstChild instanceof IDOMNode) {
first = (IDOMNode) firstChild;
start = first.getStartOffset();
}
if (lastChild instanceof IDOMNode) {
IDOMNode last = (IDOMNode) lastChild;
end = last.getEndOffset();
}
if (domElement != null) {
oldValue = domElement.getModel().getStructuredDocument().get(start, end - start);
domElement.getModel().getStructuredDocument().replaceText(documentationElement, start, end - start, newValue);
}
}
} else {
if (newValue.length() > 0) {
// $NON-NLS-1$
oldValue = "";
Node childNode = documentationElement.getOwnerDocument().createTextNode(newValue);
documentationElement.appendChild(childNode);
}
}
} catch (Exception e) {
} finally {
endRecording();
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method insertEndTag.
protected IDOMNode insertEndTag(IDOMNode node) {
IDOMElement element = (IDOMElement) node;
int startTagStartOffset = node.getStartOffset();
IDOMModel structuredModel = node.getModel();
IDOMNode newNode = null;
if (element.isCommentTag()) {
// do nothing
} else if (isEmptyElement(element)) {
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
IStructuredDocumentRegion startStructuredDocumentRegion = node.getStartStructuredDocumentRegion();
ITextRegionList regions = startStructuredDocumentRegion.getRegions();
ITextRegion lastRegion = regions.get(regions.size() - 1);
replaceSource(structuredModel, structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), lastRegion.getLength(), EMPTY_TAG_CLOSE);
if (regions.size() > 1) {
ITextRegion regionBeforeTagClose = regions.get(regions.size() - 1 - 1);
// region does not have extra spaces
if (regionBeforeTagClose.getTextLength() == regionBeforeTagClose.getLength())
// $NON-NLS-1$
replaceSource(structuredModel, structuredDocument, startStructuredDocumentRegion.getStartOffset(lastRegion), 0, " ");
}
} else {
String tagName = node.getNodeName();
String endTag = END_TAG_OPEN.concat(tagName).concat(TAG_CLOSE);
IDOMNode lastChild = (IDOMNode) node.getLastChild();
int endTagStartOffset = 0;
if (lastChild != null)
// if this node has children, insert the end tag after the
// last child
endTagStartOffset = lastChild.getEndOffset();
else
// if this node does not has children, insert the end tag
// after the start tag
endTagStartOffset = node.getEndOffset();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
replaceSource(structuredModel, structuredDocument, endTagStartOffset, 0, endTag);
}
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
return newNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method cleanup.
public Node cleanup(Node node) {
IDOMNode renamedNode = (IDOMNode) cleanupChildren(node);
// call quoteAttrValue() first so it will close any unclosed attr
// quoteAttrValue() will return the new start tag if there is a
// structure change
renamedNode = quoteAttrValue(renamedNode);
// and not implicit tag
if (!((IDOMElement) renamedNode).isCommentTag() && (renamedNode.getStartStructuredDocumentRegion() != null)) {
IDOMModel structuredModel = renamedNode.getModel();
// save start offset before insertTagClose()
// or else renamedNode.getStartOffset() will be zero if
// renamedNode replaced by insertTagClose()
int startTagStartOffset = renamedNode.getStartOffset();
// for start tag
IStructuredDocumentRegion startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
insertTagClose(structuredModel, startTagStructuredDocumentRegion);
// update renamedNode and startTagStructuredDocumentRegion after
// insertTagClose()
renamedNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
startTagStructuredDocumentRegion = renamedNode.getStartStructuredDocumentRegion();
// for end tag
IStructuredDocumentRegion endTagStructuredDocumentRegion = renamedNode.getEndStructuredDocumentRegion();
if (endTagStructuredDocumentRegion != startTagStructuredDocumentRegion)
insertTagClose(structuredModel, endTagStructuredDocumentRegion);
}
// call insertMissingTags() next, it will generate implicit tags if
// there are any
// insertMissingTags() will return the new missing start tag if one is
// missing
// then compress any empty element tags
// applyTagNameCase() will return the renamed node.
// The renamed/new node will be saved and returned to caller when all
// cleanup is done.
renamedNode = insertMissingTags(renamedNode);
renamedNode = compressEmptyElementTag(renamedNode);
renamedNode = insertRequiredAttrs(renamedNode);
renamedNode = applyTagNameCase(renamedNode);
applyAttrNameCase(renamedNode);
cleanupCSSAttrValue(renamedNode);
return renamedNode;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement in project webtools.sourceediting by eclipse.
the class ElementNodeCleanupHandler method insertStartTag.
protected IDOMNode insertStartTag(IDOMNode node) {
IDOMElement element = (IDOMElement) node;
if (element.isCommentTag())
// do nothing
return node;
IDOMNode newNode = null;
String tagName = node.getNodeName();
String startTag = START_TAG_OPEN.concat(tagName).concat(TAG_CLOSE);
int startTagStartOffset = node.getStartOffset();
IDOMModel structuredModel = node.getModel();
IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
replaceSource(structuredModel, structuredDocument, startTagStartOffset, 0, startTag);
// save
newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
return newNode;
}
Aggregations