Search in sources :

Example 16 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project knime-core by knime.

the class NodeDescription210Proxy method validate.

/**
 * Validate against the XML Schema. If violations are found they are reported via the logger as coding problems.
 *
 * @return <code>true</code> if the document is valid, <code>false</code> otherwise
 */
protected final boolean validate() {
    // this method has default visibility so that we can use it in testcases
    XmlOptions options = new XmlOptions(OPTIONS);
    List<XmlError> errors = new ArrayList<XmlError>();
    options.setErrorListener(errors);
    boolean valid = m_document.validate(options);
    if (!valid) {
        logger.coding("Node description of '" + m_document.getKnimeNode().getName() + "' does not conform to the Schema. Violations follow.");
        for (XmlError err : errors) {
            logger.coding(err.toString());
        }
    }
    return valid;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) ArrayList(java.util.ArrayList) XmlError(org.apache.xmlbeans.XmlError)

Example 17 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project knime-core by knime.

the class NodeDescription27Proxy method validate.

/**
 * Validate against the XML Schema. If violations are found they are reported via the logger as coding problems.
 *
 * @return <code>true</code> if the document is valid, <code>false</code> otherwise
 */
protected final boolean validate() {
    // this method has default visibility so that we can use it in testcases
    XmlOptions options = new XmlOptions(OPTIONS);
    List<XmlError> errors = new ArrayList<XmlError>();
    options.setErrorListener(errors);
    boolean valid = m_document.validate(options);
    if (!valid) {
        logger.coding(// .getName()
        "Node description of '" + m_document.getKnimeNode() + "' does not conform to the Schema. Violations follow.");
        for (XmlError err : errors) {
            logger.coding(err.toString());
        }
    }
    return valid;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) ArrayList(java.util.ArrayList) XmlError(org.apache.xmlbeans.XmlError)

Example 18 with XmlOptions

use of org.apache.xmlbeans.XmlOptions in project knime-core by knime.

the class NodeDescription31Proxy method validate.

/**
 * Validate against the XML Schema. If violations are found they are reported via the logger as coding problems.
 *
 * @return <code>true</code> if the document is valid, <code>false</code> otherwise
 */
protected final boolean validate() {
    // this method has default visibility so that we can use it in testcases
    XmlOptions options = new XmlOptions(OPTIONS);
    List<XmlError> errors = new ArrayList<XmlError>();
    options.setErrorListener(errors);
    boolean valid = m_document.validate(options);
    if (!valid) {
        logger.coding("Node description of '" + m_document.getKnimeNode().getName() + "' does not conform to the Schema. Violations follow.");
        for (XmlError err : errors) {
            logger.coding(err.toString());
        }
    }
    return valid;
}
Also used : XmlOptions(org.apache.xmlbeans.XmlOptions) ArrayList(java.util.ArrayList) XmlError(org.apache.xmlbeans.XmlError)

Example 19 with XmlOptions

use of org.apache.xmlbeans.XmlOptions 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 20 with XmlOptions

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

the class XWPFFooter method commit.

/**
     * save and commit footer
     */
@Override
protected void commit() throws IOException {
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    xmlOptions.setSaveSyntheticDocumentElement(new QName(CTNumbering.type.getName().getNamespaceURI(), "ftr"));
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    super._getHdrFtr().save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Aggregations

XmlOptions (org.apache.xmlbeans.XmlOptions)36 OutputStream (java.io.OutputStream)18 QName (javax.xml.namespace.QName)18 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)17 XmlException (org.apache.xmlbeans.XmlException)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 Beta (org.apache.poi.util.Beta)7 XmlError (org.apache.xmlbeans.XmlError)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 HashMap (java.util.HashMap)2 MarshalException (javax.xml.crypto.MarshalException)2 EncryptedDocumentException (org.apache.poi.EncryptedDocumentException)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 XmlObject (org.apache.xmlbeans.XmlObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1