Search in sources :

Example 91 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method createFromJS.

static XML createFromJS(XMLLibImpl lib, Object inputObject) {
    XmlObject xo;
    boolean isText = false;
    String frag;
    if (inputObject == null || inputObject == Undefined.instance) {
        frag = "";
    } else if (inputObject instanceof XMLObjectImpl) {
        // todo: faster way for XMLObjects?
        frag = ((XMLObjectImpl) inputObject).toXMLString(0);
    } else {
        if (inputObject instanceof Wrapper) {
            Object wrapped = ((Wrapper) inputObject).unwrap();
            if (wrapped instanceof XmlObject) {
                return createFromXmlObject(lib, (XmlObject) wrapped);
            }
        }
        frag = ScriptRuntime.toString(inputObject);
    }
    if (frag.trim().startsWith("<>")) {
        throw ScriptRuntime.typeError("Invalid use of XML object anonymous tags <></>.");
    }
    if (frag.indexOf("<") == -1) {
        // Must be solo text node, wrap in XML fragment
        isText = true;
        frag = "<textFragment>" + frag + "</textFragment>";
    }
    XmlOptions options = new XmlOptions();
    if (lib.ignoreComments) {
        options.put(XmlOptions.LOAD_STRIP_COMMENTS);
    }
    if (lib.ignoreProcessingInstructions) {
        options.put(XmlOptions.LOAD_STRIP_PROCINSTS);
    }
    if (lib.ignoreWhitespace) {
        options.put(XmlOptions.LOAD_STRIP_WHITESPACE);
    }
    try {
        xo = XmlObject.Factory.parse(frag, options);
        // Apply the default namespace
        Context cx = Context.getCurrentContext();
        String defaultURI = lib.getDefaultNamespaceURI(cx);
        if (defaultURI.length() > 0) {
            XmlCursor cursor = xo.newCursor();
            boolean isRoot = true;
            while (!cursor.toNextToken().isEnddoc()) {
                if (!cursor.isStart())
                    continue;
                // Check if this element explicitly sets the
                // default namespace
                boolean defaultNSDeclared = false;
                cursor.push();
                while (cursor.toNextToken().isAnyAttr()) {
                    if (cursor.isNamespace()) {
                        if (cursor.getName().getLocalPart().length() == 0) {
                            defaultNSDeclared = true;
                            break;
                        }
                    }
                }
                cursor.pop();
                if (defaultNSDeclared) {
                    cursor.toEndToken();
                    continue;
                }
                // Check if this element's name is in no namespace
                javax.xml.namespace.QName qname = cursor.getName();
                if (qname.getNamespaceURI().length() == 0) {
                    // Change the namespace
                    qname = new javax.xml.namespace.QName(defaultURI, qname.getLocalPart());
                    cursor.setName(qname);
                }
                if (isRoot) {
                    // Declare the default namespace
                    cursor.push();
                    cursor.toNextToken();
                    cursor.insertNamespace("", defaultURI);
                    cursor.pop();
                    isRoot = false;
                }
            }
            cursor.dispose();
        }
    } catch (XmlException xe) {
        /*
todo need to handle namespace prefix not found in XML look for namespace type in the scope change.

            String errorMsg = "Use of undefined namespace prefix: ";
            String msg = xe.getError().getMessage();
            if (msg.startsWith(errorMsg))
            {
                String prefix = msg.substring(errorMsg.length());
            }
*/
        String errMsg = xe.getMessage();
        if (errMsg.equals("error: Unexpected end of file after null")) {
            // Create an empty document.
            xo = XmlObject.Factory.newInstance();
        } else {
            throw ScriptRuntime.typeError(xe.getMessage());
        }
    } catch (Throwable e) {
        // todo: TLL Catch specific exceptions during parse.
        throw ScriptRuntime.typeError("Not Parsable as XML");
    }
    XmlCursor curs = xo.newCursor();
    if (curs.currentTokenType().isStartdoc()) {
        curs.toFirstContentToken();
    }
    if (isText) {
        // Move it to point to the text node
        curs.toFirstContentToken();
    }
    XScriptAnnotation anno;
    try {
        anno = new XScriptAnnotation(curs);
        curs.setBookmark(anno);
    } finally {
        curs.dispose();
    }
    return new XML(lib, anno);
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) XmlCursor(org.apache.xmlbeans.XmlCursor) XmlException(org.apache.xmlbeans.XmlException) XmlObject(org.apache.xmlbeans.XmlObject) XmlObject(org.apache.xmlbeans.XmlObject)

Example 92 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method createTextElement.

/**
     *
     * @param qname
     * @param value
     * @return
     */
static XML createTextElement(XMLLibImpl lib, javax.xml.namespace.QName qname, String value) {
    XScriptAnnotation anno;
    XmlObject xo = XmlObject.Factory.newInstance();
    XmlCursor cursor = xo.newCursor();
    try {
        cursor.toNextToken();
        cursor.beginElement(qname.getLocalPart(), qname.getNamespaceURI());
        //if(namespace.length() > 0)
        //    cursor.insertNamespace("", namespace);
        cursor.insertChars(value);
        cursor.toStartDoc();
        cursor.toNextToken();
        anno = new XScriptAnnotation(cursor);
        cursor.setBookmark(anno);
    } finally {
        cursor.dispose();
    }
    return new XML(lib, anno);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 93 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method setNamespace.

/**
     *
     * @param ns
     */
void setNamespace(Namespace ns) {
    XmlCursor cursor = newCursor();
    try {
        if (cursor.isStartdoc())
            cursor.toFirstContentToken();
        if (cursor.isText() || cursor.isComment() || cursor.isProcinst())
            return;
        String prefix = ns.prefix();
        if (prefix == null) {
            prefix = "";
        }
        cursor.setName(new javax.xml.namespace.QName(ns.uri(), localName(), prefix));
    } finally {
        cursor.dispose();
    }
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 94 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method getXmlChild.

/**
     *
     * @param index
     * @return
     */
XML getXmlChild(long index) {
    XML result = null;
    XmlCursor curs = newCursor();
    if (moveToChild(curs, index, false, true)) {
        result = createXML(lib, curs);
    }
    curs.dispose();
    return result;
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 95 with XmlCursor

use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.

the class XML method replace.

/**
     *
     * @param destCurs
     * @param newValue
     */
private void replace(XmlCursor destCurs, XML newValue) {
    if (destCurs.isStartdoc()) {
        // Can't overwrite a whole document (user really wants to overwrite the contents of).
        destCurs.toFirstContentToken();
    }
    // Orphan the token -- don't delete it outright on the XmlCursor.
    removeToken(destCurs);
    XmlCursor srcCurs = newValue.newCursor();
    if (srcCurs.currentTokenType().isStartdoc()) {
        // Cann't append a whole document (user really wants to append the contents of).
        srcCurs.toFirstContentToken();
    }
    moveSrcToDest(srcCurs, destCurs, false);
    // Re-link a new annotation to this cursor -- we just deleted the previous annotation on entrance to replace.
    if (!destCurs.toPrevSibling()) {
        destCurs.toPrevToken();
    }
    destCurs.setBookmark(new XScriptAnnotation(destCurs));
    // todo would be nice if destCurs.toNextSibling went to where the next token if the cursor was pointing at the last token in the stream.
    destCurs.toEndToken();
    destCurs.toNextToken();
    srcCurs.dispose();
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

XmlCursor (org.apache.xmlbeans.XmlCursor)160 XmlObject (org.apache.xmlbeans.XmlObject)68 QName (javax.xml.namespace.QName)21 XmlException (org.apache.xmlbeans.XmlException)16 TokenType (org.apache.xmlbeans.XmlCursor.TokenType)14 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)14 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)10 ArrayList (java.util.ArrayList)9 POSIXApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdlPosix.POSIXApplicationType)8 HPCProfileApplicationType (org.ggf.schemas.jsdl.x2006.x07.jsdlHpcpa.HPCProfileApplicationType)8 SPMDApplicationType (org.ogf.schemas.jsdl.x2007.x02.jsdlSpmd.SPMDApplicationType)8 IOException (java.io.IOException)5 POIXMLException (org.apache.poi.POIXMLException)5 InputStream (java.io.InputStream)4 DrawPaint (org.apache.poi.sl.draw.DrawPaint)3 ArrayType (org.dmg.pmml.ArrayType)3 ApplicationType (org.ggf.schemas.jsdl.x2005.x11.jsdl.ApplicationType)3 LineString (org.locationtech.jts.geom.LineString)3 CTRow (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)3 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3