use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class ModelModifications method testDocumentJSPMods.
public void testDocumentJSPMods() throws UnsupportedEncodingException, IOException {
// assumes 0-byte html empty file
IDOMModel model = null;
model = createJSPModel();
try {
IDOMDocument doc = model.getDocument();
Element ele = doc.createElement(HTML40Namespace.ElementName.P);
doc.appendChild(ele);
IStructuredDocument structuredDocument = model.getStructuredDocument();
structuredDocument.replaceText(this, 0, structuredDocument.getLength(), "");
Element ele2 = doc.createElement(HTML40Namespace.ElementName.P);
doc.appendChild(ele2);
} finally {
if (model != null) {
model.releaseFromEdit();
}
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class StyleAdapterPerfTest method testPerformance.
public void testPerformance() {
try {
String filename = "testfiles/html/example04.html";
IStructuredModel model = StructuredModelManager.getModelManager().getModelForEdit(filename, getClass().getResourceAsStream(filename), null);
if (model instanceof IDOMModel) {
IDOMDocument doc = ((IDOMModel) model).getDocument();
NodeList nodes = doc.getElementsByTagName(HTML40Namespace.ElementName.STYLE);
if (nodes != null && nodes.getLength() > 0) {
Node node = nodes.item(0);
if (node instanceof LinkStyle) {
long start = System.currentTimeMillis();
((LinkStyle) node).getSheet();
// System.out.println("elapsed time = " + (System.currentTimeMillis() - start));
// TODO: we should probably use something likse o.e.core.runtime.PerformanceStats
// I picked the following fail criteria simple since before fix, the printlin reports about 7000
// on my computer, but with fix, reported about 1000.
// The appropriate time may vary greatly depending on the build/test machine.
// This is just to catch some large change in behavior
long elapsedTime = System.currentTimeMillis() - start;
if (elapsedTime > MAX_EXPECTED_TIME) {
fail("getSheet took longer than expected the expected " + MAX_EXPECTED_TIME + "ms");
}
}
}
}
if (model != null)
model.releaseFromRead();
} catch (Exception e) {
}
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class CleanupProcessorXML method cleanupModel.
public void cleanupModel(IStructuredModel structuredModel) {
Preferences preferences = getModelPreferences();
if (preferences != null && preferences.getBoolean(XMLCorePreferenceNames.FIX_XML_DECLARATION)) {
IDOMDocument document = ((DOMModelImpl) structuredModel).getDocument();
if (!fixExistingXmlDecl(document)) {
String encoding = preferences.getString(CommonEncodingPreferenceNames.OUTPUT_CODESET);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
Node xml = document.createProcessingInstruction("xml", "version=\"1.0\" " + "encoding=\"" + encoding + "\"");
document.insertBefore(xml, document.getFirstChild());
}
}
super.cleanupModel(structuredModel);
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class XSDHyperlinkDetector method getXSDSchema.
/**
* Gets the xsd schema from document
*
* @param document
* @return XSDSchema or null of one does not exist yet for document
*/
private XSDSchema getXSDSchema(IDocument document) {
XSDSchema schema = null;
IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
if (model != null) {
try {
if (model instanceof IDOMModel) {
IDOMDocument domDoc = ((IDOMModel) model).getDocument();
if (domDoc != null) {
XSDModelAdapter modelAdapter = (XSDModelAdapter) domDoc.getExistingAdapter(XSDModelAdapter.class);
/*
* ISSUE: Didn't want to go through initializing schema if it does
* not already exist, so just attempted to get existing adapter. If
* doesn't exist, just don't bother working.
*/
if (modelAdapter != null)
schema = modelAdapter.getSchema();
}
}
} finally {
model.releaseFromRead();
}
}
return schema;
}
use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument in project webtools.sourceediting by eclipse.
the class SourceValidator method convertSource.
/**
*/
public String convertSource(String source) {
if (source == null)
return null;
if (this.node == null)
// error
return null;
// setup conversion conditions
boolean acceptTag = false;
boolean acceptClose = false;
boolean acceptQuote = false;
boolean acceptAmpersand = false;
boolean acceptEntityRef = true;
boolean acceptJSPEnd = true;
String endTagName = null;
if (this.node.getNodeType() == Node.ATTRIBUTE_NODE) {
IDOMDocument document = (IDOMDocument) this.node.getOwnerDocument();
if (document != null && document.isJSPType())
acceptTag = true;
if (acceptTag) {
Attr attr = (Attr) this.node;
ElementImpl element = (ElementImpl) attr.getOwnerElement();
if (element != null && element.isJSPTag())
acceptTag = false;
}
// if the source does not include single quote,
// double quote is valid
acceptQuote = (source.indexOf('\'') < 0);
} else if (this.node.getNodeType() == Node.TEXT_NODE) {
TextImpl text = (TextImpl) this.node;
if (text.isJSPContent()) {
int index = source.indexOf(JSPTag.TAG_CLOSE);
if (index < 0)
return source;
acceptTag = true;
acceptClose = true;
acceptQuote = true;
acceptAmpersand = true;
acceptJSPEnd = false;
} else if (text.isCDATAContent()) {
endTagName = text.getParentNode().getNodeName();
if (endTagName == null)
// error
return null;
acceptTag = true;
acceptClose = true;
acceptQuote = true;
acceptAmpersand = true;
}
} else {
IDOMDocument document = null;
if (this.node.getNodeType() == Node.DOCUMENT_NODE) {
document = (IDOMDocument) this.node;
} else {
document = (IDOMDocument) this.node.getOwnerDocument();
}
if (document != null && document.isJSPType())
acceptTag = true;
}
StringBuffer buffer = null;
int copiedLength = 0;
int length = source.length();
for (int i = 0; i < length; i++) {
String ref = null;
char c = source.charAt(i);
switch(c) {
case '<':
if (acceptTag) {
if (endTagName != null) {
if (!matchEndTag(source, i + 1, endTagName))
continue;
} else {
int skip = skipTag(source, i + 1);
if (skip >= 0) {
i += skip;
continue;
}
}
// invalid JSP tag
}
ref = IXMLCharEntity.LT_REF;
break;
case '>':
if (acceptClose)
continue;
ref = IXMLCharEntity.GT_REF;
break;
case '&':
if (acceptAmpersand)
continue;
if (acceptEntityRef) {
int skip = skipEntityRef(source, i + 1);
if (skip >= 0) {
i += skip;
continue;
}
}
ref = IXMLCharEntity.AMP_REF;
break;
case '"':
if (acceptQuote)
continue;
ref = IXMLCharEntity.QUOT_REF;
break;
case '%':
if (acceptJSPEnd)
continue;
if (source.charAt(i + 1) != '>')
continue;
i++;
ref = IXMLCharEntity.GT_REF;
break;
default:
continue;
}
if (ref != null) {
if (buffer == null) {
buffer = new StringBuffer(length + 8);
}
if (i > copiedLength) {
buffer.append(source.substring(copiedLength, i));
}
buffer.append(ref);
// skip this character
copiedLength = i + 1;
}
}
if (buffer != null) {
if (copiedLength < length) {
buffer.append(source.substring(copiedLength, length));
}
return buffer.toString();
}
return source;
}
Aggregations