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);
}
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);
}
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;
}
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;
}
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();
}
}
}
Aggregations