use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method matchDescendantChildren.
/**
*
* @return
*/
private XMLList matchDescendantChildren(XMLName xmlName) {
XMLList result = new XMLList(lib);
XmlCursor curs = newCursor();
TokenType tt = curs.currentTokenType();
// Set the targets for this XMLList.
result.setTargets(this, null);
if (tt.isStartdoc()) {
tt = curs.toFirstContentToken();
}
if (tt.isContainer()) {
int nestLevel = 1;
while (nestLevel > 0) {
tt = curs.toNextToken();
if (!tt.isAttr() && !tt.isEnd() && !tt.isEnddoc()) {
// Only try to match names for elements or processing instructions.
if (!tt.isStart() && !tt.isProcinst()) {
// Not an element or procinst, only add if qname is all
if (xmlName.localName().equals("*")) {
result.addToList(findAnnotation(curs));
}
} else {
if (qnameMatches(xmlName, curs.getName())) {
result.addToList(findAnnotation(curs));
}
}
}
if (tt.isStart()) {
nestLevel++;
} else if (tt.isEnd()) {
nestLevel--;
} else if (tt.isEnddoc()) {
// Shouldn't get here, but just in case.
break;
}
}
}
curs.dispose();
return result;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method setName.
/**
*
* @param name
*/
void setName(QName qname) {
XmlCursor cursor = newCursor();
try {
if (cursor.isStartdoc())
cursor.toFirstContentToken();
if (cursor.isText() || cursor.isComment())
return;
if (cursor.isProcinst()) {
String localName = qname.localName();
cursor.setName(new javax.xml.namespace.QName(localName));
} else {
String prefix = qname.prefix();
if (prefix == null) {
prefix = "";
}
cursor.setName(new javax.xml.namespace.QName(qname.uri(), qname.localName(), prefix));
}
} finally {
cursor.dispose();
}
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method removeNamespace.
/**
*
* @param namespace
*/
XML removeNamespace(Namespace ns) {
XmlCursor cursor = newCursor();
try {
if (cursor.isStartdoc())
cursor.toFirstContentToken();
if (!cursor.isStart())
return this;
String nsPrefix = ns.prefix();
String nsURI = ns.uri();
Map prefixToURI = new HashMap();
int depth = 1;
while (!(cursor.isEnd() && depth == 0)) {
if (cursor.isStart()) {
// Get the namespaces declared in this element.
// The ones with undefined prefixes are not candidates
// for removal because they are used.
prefixToURI.clear();
NamespaceHelper.getNamespaces(cursor, prefixToURI);
ObjArray inScopeNSBag = new ObjArray();
Iterator i = prefixToURI.entrySet().iterator();
while (i.hasNext()) {
Map.Entry entry = (Map.Entry) i.next();
ns = new Namespace(lib, (String) entry.getKey(), (String) entry.getValue());
inScopeNSBag.add(ns);
}
// Add the URI we are looking for to avoid matching
// non-existing Namespaces.
ns = new Namespace(lib, nsURI);
inScopeNSBag.add(ns);
Object[] inScopeNS = inScopeNSBag.toArray();
// Check the element name
Namespace n = NamespaceHelper.getNamespace(lib, cursor, inScopeNS);
if (nsURI.equals(n.uri()) && (nsPrefix == null || nsPrefix.equals(n.prefix()))) {
// This namespace is used
return this;
}
// Check the attributes
cursor.push();
boolean hasNext = cursor.toFirstAttribute();
while (hasNext) {
n = NamespaceHelper.getNamespace(lib, cursor, inScopeNS);
if (nsURI.equals(n.uri()) && (nsPrefix == null || nsPrefix.equals(n.prefix()))) {
// This namespace is used
return this;
}
hasNext = cursor.toNextAttribute();
}
cursor.pop();
if (nsPrefix == null) {
// Remove all namespaces declarations that match nsURI
i = prefixToURI.entrySet().iterator();
while (i.hasNext()) {
Map.Entry entry = (Map.Entry) i.next();
if (entry.getValue().equals(nsURI))
NamespaceHelper.removeNamespace(cursor, (String) entry.getKey());
}
} else if (nsURI.equals(prefixToURI.get(nsPrefix))) {
// Remove the namespace declaration that matches nsPrefix
NamespaceHelper.removeNamespace(cursor, String.valueOf(nsPrefix));
}
}
switch(cursor.toNextToken().intValue()) {
case XmlCursor.TokenType.INT_START:
depth++;
break;
case XmlCursor.TokenType.INT_END:
depth--;
break;
}
}
} finally {
cursor.dispose();
}
return this;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method remove.
/**
*
*/
void remove() {
XmlCursor childCurs = newCursor();
if (childCurs.currentTokenType().isStartdoc()) {
// Remove on the document removes all children.
TokenType tt = childCurs.toFirstContentToken();
while (!tt.isEnd() && !tt.isEnddoc()) {
removeToken(childCurs);
// Now see where we're pointing after the delete -- next token.
tt = childCurs.currentTokenType();
}
} else {
removeToken(childCurs);
}
childCurs.dispose();
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method matchAttributes.
/**
*
* @param name
* @return
*/
private XMLList matchAttributes(XMLName xmlName) {
XMLList result = new XMLList(lib);
XmlCursor curs = newCursor();
if (curs.currentTokenType().isStartdoc()) {
curs.toFirstContentToken();
}
if (curs.isStart()) {
if (curs.toFirstAttribute()) {
do {
if (qnameMatches(xmlName, curs.getName())) {
result.addToList(createAttributeObject(curs));
}
} while (curs.toNextAttribute());
}
}
curs.dispose();
return result;
}
Aggregations