use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class CleanupActionHTMLDelegate method isXHTML.
private boolean isXHTML() {
boolean isxhtml = false;
if (fEditor instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) fEditor;
IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
IStructuredModel model = null;
try {
model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model instanceof IDOMModel) {
IDOMDocument domDocument = ((IDOMModel) model).getDocument();
if (domDocument != null)
isxhtml = domDocument.isXMLType();
}
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
return isxhtml;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class JSPTranslator method postReadExternalSetup.
/**
* <p>This does mandatory setup needed after a JSPTranslator is restored from
* a persisted file</p>
*
* @param jspModel {@link IStructuredModel} representing the JSP file that this
* is a translator for
*/
protected void postReadExternalSetup(IStructuredModel jspModel) {
this.fStructuredDocument = jspModel.getStructuredDocument();
this.fJspTextBuffer = new StringBuffer(this.fStructuredDocument.get());
if (jspModel instanceof IDOMModel) {
this.fStructuredModel = (IDOMModel) jspModel;
}
// Do Post-Initialization of created problems if any
for (int i = 0; i < fTranslationProblems.size(); i++) {
if (fTranslationProblems.get(i) instanceof IJSPProblem) {
IJSPProblem problem = (IJSPProblem) fTranslationProblems.get(i);
if (problem.getSourceLineNumber() == -1) {
problem.setSourceLineNumber(fStructuredDocument.getLineOfOffset(problem.getSourceStart()));
}
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class AutoImportProposal method getInsertPosition.
/**
* @param doc
* @param isXml
* @return position after <jsp:root> if xml, otherwise right before the document element
*/
private int getInsertPosition(IDocument doc, boolean isXml) {
int pos = 0;
IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
try {
if (sModel != null) {
if (sModel instanceof IDOMModel) {
IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
/*
* document element must be sole Element child of Document
* to remain valid
*/
Node targetElement = null;
if (isXml) {
targetElement = documentNode.getDocumentElement();
}
if (targetElement == null)
targetElement = getInsertNode(documentNode);
if (targetElement != null) {
IStructuredDocumentRegion sdRegion = ((IDOMNode) targetElement).getFirstStructuredDocumentRegion();
if (isXml) {
/*
* document Element must be sole Element child of
* Document to remain valid, so insert after
*/
pos = sdRegion.getEndOffset();
try {
while (pos < doc.getLength() && (doc.getChar(pos) == '\r' || doc.getChar(pos) == '\n')) {
pos++;
}
} catch (BadLocationException e) {
// not important, use pos as determined earlier
}
} else {
// insert before target element
pos = sdRegion.getStartOffset();
}
} else {
pos = 0;
}
}
}
} finally {
if (sModel != null)
sModel.releaseFromRead();
}
return pos;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class AutoImportProposal method isXmlFormat.
/**
* @param doc
* @return true if this document is xml-jsp syntax, otherwise false
*/
private boolean isXmlFormat(IDocument doc) {
boolean isXml = false;
IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
try {
if (sModel != null) {
if (!isXml) {
if (sModel instanceof IDOMModel) {
IDOMDocument documentNode = ((IDOMModel) sModel).getDocument();
Element docElement = documentNode.getDocumentElement();
// $NON-NLS-1$ //$NON-NLS-2$
isXml = docElement != null && ((docElement.getNodeName().equals("jsp:root")) || docElement.getAttributeNode("xmlns:jsp") != null || ((((IDOMNode) docElement).getStartStructuredDocumentRegion() == null && ((IDOMNode) docElement).getEndStructuredDocumentRegion() == null)));
}
}
}
} finally {
if (sModel != null)
sModel.releaseFromRead();
}
return isXml;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel in project webtools.sourceediting by eclipse.
the class StructuredDocumentToDOMUnitTests method testNodeDeletion.
/**
* Unit test -- tests basic parsing results of inserting a test string
* into an initial string.
*/
void testNodeDeletion() {
// String initialString =
// "<par><x>\ntextx\n</x>\n<y>\ntexty\n</y></par>";
// String initialString = "<par><x>textx</x><y>texty</y></par>";
// test cases for two text nodes left together (remove <b/>):
// first case works, second doesn't
// String initialString = "<a>a<b /></a>"; // -> <a>a</a>
// String initialString = "<a>a<b />b</a>"; // -> <a>ab</a>
// String initialString = getTestString1();
String initialString = getTestString2();
// print out what we always can
System.out.println();
System.out.println("----------------");
System.out.println("Test Node Deletion");
String outString = StringUtils.escape(initialString);
System.out.println("Initial String: " + outString);
// always start with fresh model
IStructuredDocument f = null;
IModelManager mm = StructuredModelManager.getModelManager();
try {
f = mm.createStructuredDocumentFor("dummy.xml", (InputStream) null, null);
} catch (IOException e) {
// do nothing, since dummy
}
//
// we'll listen to structuredDocument events to print out diagnostics
f.addDocumentChangedListener(this);
//
IDOMModel tree = new DOMModelImpl();
f.addDocumentChangingListener((IStructuredDocumentListener) tree);
// set text to structuredDocument (which updates tree)
f.setText(null, initialString);
// dump initial structuredDocument
Debug.dump(f);
// dump initial dom
DebugDocument.dump(tree.getDocument());
//
//
// makeChange1(tree);
makeChange2(f);
// display resulting text
System.out.println("resultString (from structuredDocument): ");
System.out.println(StringUtils.escape(f.getText()));
//
//
// dump post change structuredDocument
Debug.dump(f);
// dump post change DOM
DebugDocument.dump(tree.getDocument());
//
}
Aggregations