Search in sources :

Example 56 with XmlObject

use of org.apache.xmlbeans.XmlObject 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 57 with XmlObject

use of org.apache.xmlbeans.XmlObject 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 58 with XmlObject

use of org.apache.xmlbeans.XmlObject in project poi by apache.

the class XWPFHeaderFooter method insertNewParagraph.

/**
     * add a new paragraph at position of the cursor
     *
     * @param cursor
     * @return the inserted paragraph
     */
public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
    if (isCursorInHdrF(cursor)) {
        String uri = CTP.type.getName().getNamespaceURI();
        String localPart = "p";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTP p = (CTP) cursor.getObject();
        XWPFParagraph newP = new XWPFParagraph(p, this);
        XmlObject o = null;
        while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if ((!(o instanceof CTP)) || (CTP) o == p) {
            paragraphs.add(0, newP);
        } else {
            int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
            paragraphs.add(pos, newP);
        }
        int i = 0;
        XmlCursor p2 = p.newCursor();
        cursor.toCursor(p2);
        p2.dispose();
        while (cursor.toPrevSibling()) {
            o = cursor.getObject();
            if (o instanceof CTP || o instanceof CTTbl)
                i++;
        }
        bodyElements.add(i, newP);
        p2 = p.newCursor();
        cursor.toCursor(p2);
        cursor.toEndToken();
        p2.dispose();
        return newP;
    }
    return null;
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 59 with XmlObject

use of org.apache.xmlbeans.XmlObject in project poi by apache.

the class XWPFHeaderFooter method insertNewTbl.

/**
     * @param cursor
     * @return the inserted table
     */
public XWPFTable insertNewTbl(final XmlCursor cursor) {
    if (isCursorInHdrF(cursor)) {
        String uri = CTTbl.type.getName().getNamespaceURI();
        String localPart = "tbl";
        cursor.beginElement(localPart, uri);
        cursor.toParent();
        CTTbl t = (CTTbl) cursor.getObject();
        XWPFTable newT = new XWPFTable(t, this);
        cursor.removeXmlContents();
        XmlObject o = null;
        while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
            o = cursor.getObject();
        }
        if (!(o instanceof CTTbl)) {
            tables.add(0, newT);
        } else {
            int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
            tables.add(pos, newT);
        }
        int i = 0;
        XmlCursor cursor2 = t.newCursor();
        while (cursor2.toPrevSibling()) {
            o = cursor2.getObject();
            if (o instanceof CTP || o instanceof CTTbl) {
                i++;
            }
        }
        cursor2.dispose();
        bodyElements.add(i, newT);
        cursor2 = t.newCursor();
        cursor.toCursor(cursor2);
        cursor.toEndToken();
        cursor2.dispose();
        return newT;
    }
    return null;
}
Also used : CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) XmlObject(org.apache.xmlbeans.XmlObject) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 60 with XmlObject

use of org.apache.xmlbeans.XmlObject in project poi by apache.

the class XWPFFooter method onDocumentRead.

@Override
protected void onDocumentRead() throws IOException {
    super.onDocumentRead();
    FtrDocument ftrDocument = null;
    InputStream is = null;
    try {
        is = getPackagePart().getInputStream();
        ftrDocument = FtrDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
        headerFooter = ftrDocument.getFtr();
        // parse the document with cursor and add
        // the XmlObject to its lists
        XmlCursor cursor = headerFooter.newCursor();
        cursor.selectPath("./*");
        while (cursor.toNextSelection()) {
            XmlObject o = cursor.getObject();
            if (o instanceof CTP) {
                XWPFParagraph p = new XWPFParagraph((CTP) o, this);
                paragraphs.add(p);
                bodyElements.add(p);
            }
            if (o instanceof CTTbl) {
                XWPFTable t = new XWPFTable((CTTbl) o, this);
                tables.add(t);
                bodyElements.add(t);
            }
            if (o instanceof CTSdtBlock) {
                XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
                bodyElements.add(c);
            }
        }
        cursor.dispose();
    } catch (Exception e) {
        throw new POIXMLException(e);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
Also used : InputStream(java.io.InputStream) XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) POIXMLException(org.apache.poi.POIXMLException) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP) IOException(java.io.IOException) POIXMLException(org.apache.poi.POIXMLException) FtrDocument(org.openxmlformats.schemas.wordprocessingml.x2006.main.FtrDocument) XmlCursor(org.apache.xmlbeans.XmlCursor) CTSdtBlock(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)

Aggregations

XmlObject (org.apache.xmlbeans.XmlObject)102 XmlCursor (org.apache.xmlbeans.XmlCursor)49 XmlException (org.apache.xmlbeans.XmlException)17 Test (org.junit.Test)14 CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)13 CTAxDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource)12 CTNumDataSource (org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource)12 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 ArrayList (java.util.ArrayList)9 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)9 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)9 CTPicture (org.openxmlformats.schemas.presentationml.x2006.main.CTPicture)7 IOException (java.io.IOException)6 QName (javax.xml.namespace.QName)6 POIXMLException (org.apache.poi.POIXMLException)6 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)6 CTGroupShape (org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape)6 CTGraphicalObjectFrame (org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame)5 Node (org.w3c.dom.Node)5 CTConnector (org.openxmlformats.schemas.presentationml.x2006.main.CTConnector)4