use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method toXMLString.
/**
*
* @return
*/
String toXMLString(int indent) {
// XXX indent is ignored
String result;
XmlCursor curs = newCursor();
if (curs.isStartdoc()) {
curs.toFirstContentToken();
}
try {
if (curs.isText()) {
result = curs.getChars();
} else if (curs.isAttr()) {
result = curs.getTextValue();
} else if (curs.isComment() || curs.isProcinst()) {
result = XML.dumpNode(curs, getOptions());
// todo: XBeans-dependent hack here
// If it's a comment or PI, take off the xml-frament stuff
String start = "<xml-fragment>";
String end = "</xml-fragment>";
if (result.startsWith(start)) {
result = result.substring(start.length());
}
if (result.endsWith(end)) {
result = result.substring(0, result.length() - end.length());
}
} else {
result = XML.dumpNode(curs, getOptions());
}
} finally {
curs.dispose();
}
return result;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method parent.
/**
*
* @return
*/
Object parent() {
Object parent;
XmlCursor curs = newCursor();
if (curs.isStartdoc()) {
// At doc level - no parent
parent = Undefined.instance;
} else {
if (curs.toParent()) {
if (curs.isStartdoc()) {
// Was top-level - no parent
parent = Undefined.instance;
} else {
parent = getFromAnnotation(lib, findAnnotation(curs));
}
} else {
// No parent
parent = Undefined.instance;
}
}
curs.dispose();
return parent;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method hasSimpleContent.
/**
*
* @return
*/
boolean hasSimpleContent() {
boolean simpleContent = false;
XmlCursor curs = newCursor();
if (curs.isAttr() || curs.isText()) {
return true;
}
if (curs.isStartdoc()) {
curs.toFirstContentToken();
}
simpleContent = !(curs.toFirstChild());
curs.dispose();
return simpleContent;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method setAttribute.
/**
*
* @param attrName
* @param value
*/
void setAttribute(XMLName xmlName, Object value) {
if (xmlName.uri() == null && xmlName.localName().equals("*")) {
throw ScriptRuntime.typeError("@* assignment not supported.");
}
XmlCursor curs = newCursor();
String strValue = ScriptRuntime.toString(value);
if (curs.currentTokenType().isStartdoc()) {
curs.toFirstContentToken();
}
javax.xml.namespace.QName qName;
try {
qName = new javax.xml.namespace.QName(xmlName.uri(), xmlName.localName());
} catch (Exception e) {
throw ScriptRuntime.typeError(e.getMessage());
}
if (!curs.setAttributeText(qName, strValue)) {
if (curs.currentTokenType().isStart()) {
// Can only add attributes inside of a start.
curs.toNextToken();
}
curs.insertAttributeWithValue(qName, strValue);
}
curs.dispose();
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method newCursor.
/**
*
* @return
*/
private XmlCursor newCursor() {
XmlCursor curs;
if (_anno != null) {
curs = _anno.createCursor();
if (curs == null) {
// Orphaned case.
XmlObject doc = XmlObject.Factory.newInstance();
curs = doc.newCursor();
if (_anno._name != null) {
curs.toNextToken();
curs.insertElement(_anno._name);
curs.toPrevSibling();
}
curs.setBookmark(_anno);
}
} else {
XmlObject doc = XmlObject.Factory.newInstance();
curs = doc.newCursor();
}
return curs;
}
Aggregations