use of org.apache.xmlbeans.XmlException in project knime-core by knime.
the class PMMLUtils method getUpdatedVersionAndNamespace.
/**
* Update the namespace and version of the document to PMML 4.2. This method forces an update of the header so
* the caller should make sure it's compatible with 4.2 (in the sense that there haven't been changes to the
* any of the fields in the models that KNIME can read).
*
* @param xmlDoc the {@link XmlObject} to update the namespace
* @return the updated PMML
* @throws XmlException when the PMML element could not be updated to 4.2
*/
public static String getUpdatedVersionAndNamespace(final XmlObject xmlDoc) throws XmlException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
DocumentBuilder builder = dbf.newDocumentBuilder();
String encoding = xmlDoc.documentProperties().getEncoding();
if (encoding == null) {
// use utf-8 as default encoding if none is set
encoding = "UTF-8";
}
// see bug 6306 - XML 1.1 features not parsed 'correctly'
ByteArrayInputStream inputStream = new ByteArrayInputStream(PMMLFormatter.xmlText(xmlDoc).getBytes(encoding));
Document doc = builder.parse(inputStream);
inputStream.close();
updatePmmlNamespaceAndVersion(doc);
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
writer.flush();
return writer.toString();
} catch (IOException | ParserConfigurationException | SAXException | TransformerException e) {
throw new XmlException("Could not update PMML document.", e);
}
}
use of org.apache.xmlbeans.XmlException in project knime-core by knime.
the class PMMLUtils method updatePmmlNamespaceAndVersion.
/**
* Sets the namespace and version of the document to PMML 4.2. Caller needs to make sure that this is a
* safe operation.
*
* @param document the document to update the namespace
* @throws XmlException when the PMML element could not be updated to 4.2
*/
public static void updatePmmlNamespaceAndVersion(final Document document) throws XmlException {
Element root = document.getDocumentElement();
String rootPrefix = root.getPrefix();
fixNamespace(document, root, rootPrefix);
NodeList nodeList = document.getElementsByTagName("PMML");
Node pmmlNode = nodeList.item(0);
if (pmmlNode == null) {
throw new XmlException("Invalid PMML document without a PMML element encountered.");
}
Node version = pmmlNode.getAttributes().getNamedItem("version");
version.setNodeValue(PMML_V42);
}
use of org.apache.xmlbeans.XmlException in project knime-core by knime.
the class SubNodeContainer method getXMLDescriptionForPorts.
/**
* @return a DOM which describes only the port connections.
*/
public Element getXMLDescriptionForPorts() {
try {
// Document
final Document doc = NodeDescription.getDocumentBuilderFactory().newDocumentBuilder().getDOMImplementation().createDocument("http://knime.org/node2012", "knimeNode", null);
addPortDescriptionToElement(doc, getMetadata(), getNrInPorts(), getNrOutPorts());
// we avoid validating the document since we don't include certain elements like 'name'
return (new NodeDescription27Proxy(doc, false)).getXMLDescription();
} catch (ParserConfigurationException | DOMException | XmlException e) {
LOGGER.warn("Could not generate ports description", e);
}
return null;
}
use of org.apache.xmlbeans.XmlException 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);
}
use of org.apache.xmlbeans.XmlException in project poi by apache.
the class XWPFFootnotes method onDocumentRead.
/**
* Read document
*/
@Override
protected void onDocumentRead() throws IOException {
FootnotesDocument notesDoc;
InputStream is = null;
try {
is = getPackagePart().getInputStream();
notesDoc = FootnotesDocument.Factory.parse(is, DEFAULT_XML_OPTIONS);
ctFootnotes = notesDoc.getFootnotes();
} catch (XmlException e) {
throw new POIXMLException();
} finally {
if (is != null) {
is.close();
}
}
// Find our footnotes
for (CTFtnEdn note : ctFootnotes.getFootnoteArray()) {
listFootnote.add(new XWPFFootnote(note, this));
}
}
Aggregations