use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method appendChild.
/**
*
* @param xml
* @return
*/
XML appendChild(Object xml) {
XmlCursor curs = newCursor();
if (curs.isStartdoc()) {
curs.toFirstContentToken();
}
// Move the cursor to the end of this element
if (curs.isStart()) {
curs.toEndToken();
}
insertChild(curs, xml);
curs.dispose();
return this;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method addNamespace.
//
//
// Methods from section 12.4.4 in the spec
//
//
/**
* The addNamespace method adds a namespace declaration to the in scope
* namespaces for this XML object and returns this XML object.
*
* @param toAdd
*/
XML addNamespace(Namespace ns) {
// When a namespace is used it will be added automatically
// to the inScopeNamespaces set. There is no need to add
// Namespaces with undefined prefixes.
String nsPrefix = ns.prefix();
if (nsPrefix == null)
return this;
XmlCursor cursor = newCursor();
try {
if (!cursor.isContainer())
return this;
javax.xml.namespace.QName qname = cursor.getName();
// with QNames in no namespace.
if (qname.getNamespaceURI().equals("") && nsPrefix.equals(""))
return this;
// Get all declared namespaces that are in scope
Map prefixToURI = NamespaceHelper.getAllNamespaces(lib, cursor);
String uri = (String) prefixToURI.get(nsPrefix);
if (uri != null) {
// Check if the Namespace is not already in scope
if (uri.equals(ns.uri()))
return this;
cursor.push();
// Let's see if we have to delete a namespace declaration
while (cursor.toNextToken().isAnyAttr()) {
if (cursor.isNamespace()) {
qname = cursor.getName();
String prefix = qname.getLocalPart();
if (prefix.equals(nsPrefix)) {
// Delete the current Namespace declaration
cursor.removeXml();
break;
}
}
}
cursor.pop();
}
cursor.toNextToken();
cursor.insertNamespace(nsPrefix, ns.uri());
} finally {
cursor.dispose();
}
return this;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method doPut.
/**
*
* @param currXMLNode
* @param xmlValue
* @return
*/
private boolean doPut(XMLName name, XML currXMLNode, XMLObjectImpl xmlValue) {
boolean result = false;
XmlCursor curs = currXMLNode.newCursor();
try {
// Replace the node with this new xml value.
XML xml;
int toAssignLen = xmlValue.length();
for (int i = 0; i < toAssignLen; i++) {
if (xmlValue instanceof XMLList) {
xml = ((XMLList) xmlValue).item(i);
} else {
xml = (XML) xmlValue;
}
// If it's an attribute or text node, make text node.
XmlCursor.TokenType tt = xml.tokenType();
if (tt == XmlCursor.TokenType.ATTR || tt == XmlCursor.TokenType.TEXT) {
xml = makeXmlFromString(lib, name, xml.toString());
}
if (i == 0) {
// 1st assignment is replaceChild all others are appendChild
replace(curs, xml);
} else {
insertChild(curs, xml);
}
}
// We're done we've blown away the node because the rvalue was XML...
result = true;
} catch (Exception ex) {
ex.printStackTrace();
throw ScriptRuntime.typeError(ex.getMessage());
} finally {
curs.dispose();
}
return result;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method matchChildren.
/**
*
* @return
*/
private XMLList matchChildren(XmlCursor.TokenType tokenType, XMLName name) {
XMLList result = new XMLList(lib);
XmlCursor curs = newCursor();
TokenType tt = curs.currentTokenType();
javax.xml.namespace.QName qname = new javax.xml.namespace.QName(name.uri(), name.localName());
javax.xml.namespace.QName targetProperty = qname;
if (tt.isStartdoc()) {
tt = curs.toFirstContentToken();
}
if (tt.isContainer()) {
tt = curs.toFirstContentToken();
while (!tt.isEnd()) {
if (tt == tokenType) {
// Only try to match names for elements or processing instructions.
if (!tt.isStart() && !tt.isProcinst()) {
// Not an element or no name specified.
result.addToList(findAnnotation(curs));
// Reset target property to null in this case
targetProperty = null;
} else {
// Match names as well
if (qnameMatches(name, curs.getName())) {
// Add it to the list
result.addToList(findAnnotation(curs));
// set to null
if (targetProperty != null) {
if (targetProperty.getLocalPart().equals("*")) {
targetProperty = curs.getName();
} else if (!targetProperty.getLocalPart().equals(curs.getName().getLocalPart())) {
// Not a match, unset target property
targetProperty = null;
}
}
}
}
}
// Skip over child elements
if (tt.isStart()) {
tt = curs.toEndToken();
}
tt = curs.toNextToken();
}
}
curs.dispose();
if (tokenType == XmlCursor.TokenType.START) {
// Set the targets for this XMLList.
result.setTargets(this, targetProperty);
}
return result;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method createFromXmlObject.
static XML createFromXmlObject(XMLLibImpl lib, XmlObject xo) {
XScriptAnnotation anno;
XmlCursor curs = xo.newCursor();
if (curs.currentTokenType().isStartdoc()) {
curs.toFirstContentToken();
}
try {
anno = new XScriptAnnotation(curs);
curs.setBookmark(anno);
} finally {
curs.dispose();
}
return new XML(lib, anno);
}
Aggregations