Search in sources :

Example 6 with XmlCursor

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

the class XML method normalize.

/**
     *
     */
void normalize() {
    XmlCursor curs = newCursor();
    TokenType tt = curs.currentTokenType();
    // Walk through the tokens removing empty text nodes and merging adjacent text nodes.
    if (tt.isStartdoc()) {
        tt = curs.toFirstContentToken();
    }
    if (tt.isContainer()) {
        int nestLevel = 1;
        String previousText = null;
        while (nestLevel > 0) {
            tt = curs.toNextToken();
            if (tt == XmlCursor.TokenType.TEXT) {
                String currentText = curs.getChars().trim();
                if (currentText.trim().length() == 0) {
                    // Empty text node, remove.
                    removeToken(curs);
                    curs.toPrevToken();
                } else if (previousText == null) {
                    // No previous text node, reset to trimmed version
                    previousText = currentText;
                } else {
                    // It appears that this case never happens with XBeans.
                    // Previous text node exists, concatenate
                    String newText = previousText + currentText;
                    curs.toPrevToken();
                    removeToken(curs);
                    removeToken(curs);
                    curs.insertChars(newText);
                }
            } else {
                previousText = null;
            }
            if (tt.isStart()) {
                nestLevel++;
            } else if (tt.isEnd()) {
                nestLevel--;
            } else if (tt.isEnddoc()) {
                // Shouldn't get here, but just in case.
                break;
            }
        }
    }
    curs.dispose();
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 7 with XmlCursor

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

the class XML method equivalentXml.

//
// Other public Functions from XMLObject
//
/**
     *
     * @param target
     * @return
     */
boolean equivalentXml(Object target) {
    boolean result = false;
    if (target instanceof XML) {
        XML otherXml = (XML) target;
        // Compare with toString() if either side is text node or attribute
        // otherwise compare as XML
        XmlCursor.TokenType thisTT = tokenType();
        XmlCursor.TokenType otherTT = otherXml.tokenType();
        if (thisTT == XmlCursor.TokenType.ATTR || otherTT == XmlCursor.TokenType.ATTR || thisTT == XmlCursor.TokenType.TEXT || otherTT == XmlCursor.TokenType.TEXT) {
            result = toString().equals(otherXml.toString());
        } else {
            XmlCursor cursOne = newCursor();
            XmlCursor cursTwo = otherXml.newCursor();
            result = LogicalEquality.nodesEqual(cursOne, cursTwo);
            cursOne.dispose();
            cursTwo.dispose();
        // Old way of comparing by string.
        //                boolean orgPrettyPrinting = prototype.prettyPrinting;
        //                prototype.prettyPrinting = true;
        //                result = toXMLString(0).equals(otherXml.toXMLString(0));
        //                prototype.prettyPrinting = orgPrettyPrinting;
        }
    } else if (target instanceof XMLList) {
        XMLList otherList = (XMLList) target;
        if (otherList.length() == 1) {
            result = equivalentXml(otherList.getXmlFromAnnotation(0));
        }
    } else if (hasSimpleContent()) {
        String otherStr = ScriptRuntime.toString(target);
        result = toString().equals(otherStr);
    }
    return result;
}
Also used : TokenType(org.apache.xmlbeans.XmlCursor.TokenType) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 8 with XmlCursor

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

the class XML method createEmptyXML.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Public factories for creating a XScript XML object given an XBean cursor.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static XML createEmptyXML(XMLLibImpl lib) {
    XScriptAnnotation anno;
    XmlObject xo = XmlObject.Factory.newInstance();
    XmlCursor curs = xo.newCursor();
    try {
        anno = new XScriptAnnotation(curs);
        curs.setBookmark(anno);
    } finally {
        curs.dispose();
    }
    return new XML(lib, anno);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 9 with XmlCursor

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

the class XML method toString.

/**
     *
     * @return
     */
public String toString() {
    String result;
    XmlCursor curs = newCursor();
    if (curs.isStartdoc()) {
        curs.toFirstContentToken();
    }
    if (curs.isText()) {
        result = curs.getChars();
    } else if (curs.isStart() && hasSimpleContent()) {
        result = curs.getTextValue();
    } else {
        result = toXMLString(0);
    }
    return result;
}
Also used : XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 10 with XmlCursor

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

the class XML method localName.

/**
     *
     * @return
     */
String localName() {
    XmlCursor cursor = newCursor();
    if (cursor.isStartdoc())
        cursor.toFirstContentToken();
    String name = null;
    if (cursor.isStart() || cursor.isAttr() || cursor.isProcinst()) {
        javax.xml.namespace.QName qname = cursor.getName();
        name = qname.getLocalPart();
    }
    cursor.dispose();
    return name;
}
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