Search in sources :

Example 71 with IDOMDocument

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();
        }
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) Element(org.w3c.dom.Element) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 72 with IDOMDocument

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) {
    }
}
Also used : IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) LinkStyle(org.w3c.dom.stylesheets.LinkStyle)

Example 73 with IDOMDocument

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);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) DOMModelImpl(org.eclipse.wst.xml.core.internal.document.DOMModelImpl) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Preferences(org.eclipse.core.runtime.Preferences) IStructuredCleanupPreferences(org.eclipse.wst.sse.core.internal.cleanup.IStructuredCleanupPreferences) StructuredCleanupPreferences(org.eclipse.wst.sse.core.internal.cleanup.StructuredCleanupPreferences)

Example 74 with IDOMDocument

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;
}
Also used : XSDModelAdapter(org.eclipse.wst.xsd.ui.internal.text.XSDModelAdapter) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) XSDSchema(org.eclipse.xsd.XSDSchema)

Example 75 with IDOMDocument

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;
}
Also used : IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) Attr(org.w3c.dom.Attr)

Aggregations

IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)176 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)109 IFile (org.eclipse.core.resources.IFile)48 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)39 Element (org.w3c.dom.Element)39 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)33 NodeList (org.w3c.dom.NodeList)27 IStatus (org.eclipse.core.runtime.IStatus)23 Node (org.w3c.dom.Node)21 CoreException (org.eclipse.core.runtime.CoreException)15 ArrayList (java.util.ArrayList)14 IJsTranslation (org.eclipse.wst.jsdt.web.core.javascript.IJsTranslation)14 JsTranslationAdapter (org.eclipse.wst.jsdt.web.core.javascript.JsTranslationAdapter)14 Document (org.w3c.dom.Document)14 IJSPTranslation (org.eclipse.jst.jsp.core.internal.java.IJSPTranslation)12 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)12 IOException (java.io.IOException)11 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)11 JSPTranslationAdapter (org.eclipse.jst.jsp.core.internal.java.JSPTranslationAdapter)10 IModelManager (org.eclipse.wst.sse.core.internal.provisional.IModelManager)9