本文整理汇总了Java中org.apache.xmlbeans.XmlCursor.toParent方法的典型用法代码示例。如果您正苦于以下问题:Java XmlCursor.toParent方法的具体用法?Java XmlCursor.toParent怎么用?Java XmlCursor.toParent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xmlbeans.XmlCursor
的用法示例。
在下文中一共展示了XmlCursor.toParent方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: render
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
@Override
public void render(ElementTemplate eleTemplate, Object data,
XWPFTemplate template) {
NiceXWPFDocument doc = template.getXWPFDocument();
RunTemplate runTemplate = (RunTemplate) eleTemplate;
XWPFRun run = runTemplate.getRun();
try {
XmlCursor newCursor = ((XWPFParagraph)run.getParent()).getCTP().newCursor();
newCursor.toParent();
//if (newCursor.getObject() instanceof CTTc)
newCursor.toParent();
newCursor.toParent();
XmlObject object = newCursor.getObject();
XWPFTable table = doc.getTable((CTTbl) object);
render(table, data);
} catch (Exception e) {
logger.error("dynamic table error:" + e.getMessage(), e);
}
}
示例2: formatQName
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
private static final String formatQName( XmlCursor xmlc, QName qName )
{
XmlCursor parent = xmlc.newCursor();
parent.toParent();
String prefix = parent.prefixForNamespace( qName.getNamespaceURI() );
parent.dispose();
String name;
if( prefix == null || prefix.length() == 0 )
name = qName.getLocalPart();
else
name = prefix + ":" + qName.getLocalPart();
return name;
}
示例3: changeNS
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
protected void changeNS (String oldURI, String newURI)
{
XmlCursor curs = newCursor();
while (curs.toParent()) {
/* Goto the top of the document */
}
TokenType tt = curs.currentTokenType();
if (tt.isStartdoc())
{
tt = curs.toFirstContentToken();
}
if (tt.isStart())
{
do
{
if (tt.isStart() || tt.isAttr() || tt.isNamespace())
{
javax.xml.namespace.QName currQName = curs.getName();
if (oldURI.equals(currQName.getNamespaceURI()))
{
curs.setName(new javax.xml.namespace.QName(newURI, currQName.getLocalPart()));
}
}
tt = curs.toNextToken();
} while (!tt.isEnddoc() && !tt.isNone());
}
curs.dispose();
}
示例4: namespaceDeclarations
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
/**
* @return List of Namespace objects that are declared in the container pointed to by the cursor.
*/
public static Object[] namespaceDeclarations(XMLLibImpl lib, XmlCursor cursor)
{
ObjArray declarations = new ObjArray();
NamespaceHelper helper = new NamespaceHelper(lib);
cursor.push();
int depth = 0;
while(cursor.hasPrevToken())
{
if(cursor.isContainer())
{
cursor.push();
depth++;
}
cursor.toParent();
}
for(int i = 0; i < depth - 1; i++)
{
cursor.pop();
helper.update(cursor, null);
}
if(depth > 0)
{
cursor.pop();
helper.update(cursor, declarations);
}
cursor.pop();
return declarations.toArray();
}
示例5: getAllNamespaces
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
/**
* @return Prefix to URI map of all namespaces in scope at the cursor.
*/
public static Map getAllNamespaces(XMLLibImpl lib, XmlCursor cursor)
{
NamespaceHelper helper = new NamespaceHelper(lib);
cursor.push();
int depth = 0;
while(cursor.hasPrevToken())
{
if(cursor.isContainer())
{
cursor.push();
depth++;
}
cursor.toParent();
}
for(int i = 0; i < depth; i++)
{
cursor.pop();
helper.update(cursor, null);
}
cursor.pop();
return helper.prefixToURI;
}
示例6: validateDoc
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
public ArrayList validateDoc( HsfHmpDataDocument doc ) throws IOException, XmlException
{
logger.debug( "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\n" + PrettyPrinter.indent( doc.xmlText() ) );
ArrayList<StringBuilder> messageArray = new ArrayList<StringBuilder>();
XmlOptions option = new XmlOptions();
ArrayList<XmlError> validationErrors = new ArrayList<XmlError>();
option.setErrorListener( validationErrors );
StringBuilder sb = new StringBuilder();
if ( doc.validate( option ) == false )
{
Iterator iter = validationErrors.iterator();
while ( iter.hasNext() ) {
XmlError error = (XmlError)iter.next();
String errorMessage = error.getMessage();
XmlCursor cursor = error.getCursorLocation();
cursor.toParent();
String loc = cursor.xmlText();
String name = getPatientName(doc.xmlText(), loc);
sb.append("\n Patient Name: "+name + " \n ** Error Message: " + errorMessage + " ");
sb.append( loc.substring(loc.indexOf("signedWhen"), loc.indexOf("/")) + " --- ");
messageArray.add( sb );
}
//String sb = printErrors( validationErrors );
messageArray.add( sb );
return messageArray;
}
else
return messageArray;
}
示例7: assertEquals
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
/**
* Uses cursors to compare two XML documents, ignoring whitespace and
* ordering of attributes. Fails the JUnit test if they're different.
*
* @param message to display on test failure (may be null)
* @param expected
* @param actual
*/
public static void assertEquals(String message, XmlCursor expected, XmlCursor actual)
{
for (int child = 0; true; child++)
{
boolean child1 = expected.toChild(child);
boolean child2 = actual.toChild(child);
if (child1 != child2)
{
fail(message, "Different XML structure near " + QNameHelper.pretty(expected.getName()));
}
else if (expected.getName() != null && !expected.getName().equals(actual.getName()))
{
fail(message, "Expected element: '" + expected.getName()
+ "' differs from actual element: '" + actual.getName() + "'");
}
else if (child == 0 && !child1)
{
if (!(expected.getTextValue().equals(actual.getTextValue())))
{
fail(message, "Expected value for element " + QNameHelper.pretty(expected.getName()) + " -> '"
+ expected.getTextValue() + "' differs from actual value '" + actual.getTextValue() + "'");
}
break;
}
else if (child1)
{
assertEquals(message, expected, actual);
expected.toParent();
actual.toParent();
}
else
{
break;
}
}
assertAttributesEqual(message, expected, actual);
}
示例8: isCursorInBody
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
private boolean isCursorInBody(XmlCursor cursor) {
XmlCursor verify = cursor.newCursor();
verify.toParent();
try {
return true;// (verify.getObject() == this.getDocument().getBody());
}
finally {
verify.dispose();
}
}
示例9: formatQName
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
private static final String formatQName(XmlCursor xmlc, QName qName)
{
XmlCursor parent = xmlc.newCursor();
parent.toParent();
String prefix = parent.prefixForNamespace(qName.getNamespaceURI());
parent.dispose();
String name;
if (prefix == null || prefix.length() == 0)
name = qName.getLocalPart();
else
// name = prefix + ":" + qName.getLocalPart();
name = qName.getLocalPart();
return name;
}
示例10: assertAttributesEqual
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
/**
* Compares the attributes of the elements at the current position of two XmlCursors.
* The ordering of the attributes is ignored in the comparison. Fails the JUnit test
* case if the attributes or their values are different.
*
* @param message to display on test failure (may be null)
* @param expected
* @param actual
*/
private static void assertAttributesEqual(String message, XmlCursor expected, XmlCursor actual)
{
Map<QName,String> map1 = new HashMap<QName,String>();
Map<QName,String> map2 = new HashMap<QName,String>();
boolean attr1 = expected.toFirstAttribute();
boolean attr2 = actual.toFirstAttribute();
if (attr1 != attr2)
{
if (expected.isAttr() || actual.isAttr())
{
expected.toParent();
}
fail(message, "Differing number of attributes for element: '" + QNameHelper.pretty(expected.getName()) + "'");
}
else if (!attr1)
{
return;
}
else
{
map1.put(expected.getName(), expected.getTextValue());
map2.put(actual.getName(), actual.getTextValue());
}
while (true)
{
attr1 = expected.toNextAttribute();
attr2 = actual.toNextAttribute();
if (attr1 != attr2)
{
if (expected.isAttr() || actual.isAttr())
{
expected.toParent();
}
fail(message, "Differing number of attributes for element: '" + QNameHelper.pretty(expected.getName()) + "'");
}
else if (!attr1)
{
break;
}
else
{
map1.put(expected.getName(), expected.getTextValue());
map2.put(actual.getName(), actual.getTextValue());
}
}
expected.toParent();
actual.toParent();
// check that attribute maps match, neglecting order
Iterator<QName> iter = map1.keySet().iterator();
while (iter.hasNext())
{
QName name = iter.next();
String value1 = map1.get(name);
String value2 = map2.get(name);
if (value2 == null)
{
fail(message, "Expected attribute value missing for element: "
+ QNameHelper.pretty(expected.getName()) + "--> '" + name + "'");
}
else if (!value2.equals(value1))
{
fail(message, "Attribute values for element '" + QNameHelper.pretty(expected.getName())
+ "': Expected '" + value1 + "', Actual '" + value2 + "'");
}
}
}
示例11: validateSoapMessage
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
/**
* Validate a soap message accordingly to its WSDL and linked XSD resources. The validation is
* done for a specified message part (maybe be the input, output or fault of an operation).
* @param partName The name of the part to validate ie. name of the input, output or fault part (ex: sayHello)
* @param partNamespace The namespace of the part to validate (ex: http://www.mma.fr/test/service)
* @param message The full soap message as a string
* @param wsdlUrl The URL where we can resolve service and operation WSDL
* @param validateMessageBody Should we validate also the body ? If false, only Soap envelope is validated.
* @return The list of validation failures. If empty, message is valid !
* @throws org.apache.xmlbeans.XmlException if given message is not a valid Xml document
*/
public static List<XmlError> validateSoapMessage(String partName, String partNamespace, String message, String wsdlUrl, boolean validateMessageBody)
throws XmlException {
//
WsdlContext ctx = new WsdlContext(wsdlUrl);
List<XmlError> errors = new ArrayList<XmlError>();
ctx.getSoapVersion().validateSoapEnvelope(message, errors);
log.debug("SoapEnvelope validation errors: " + errors.size());
if (validateMessageBody){
// Create XmlBeans object for the soap message.
XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setLoadLineNumbers();
xmlOptions.setLoadLineNumbers( XmlOptions.LOAD_LINE_NUMBERS_END_ELEMENT );
XmlObject xml = XmlUtils.createXmlObject(message, xmlOptions);
// Build the QName string of the part name. Ex: {http://www.github.com/lbroudoux/service}sayHello
String fullPartName = "{" + partNamespace + "}" + partName;
// Extract the corresponding part from soap body.
XmlObject[] paths = xml.selectPath( "declare namespace env='" + ctx.getSoapVersion().getEnvelopeNamespace() + "';"
+ "declare namespace ns='" + partNamespace + "';" + "$this/env:Envelope/env:Body/ns:" + partName);
SchemaGlobalElement elm;
try {
elm = ctx.getSchemaTypeLoader().findElement(QName.valueOf(fullPartName));
} catch (Exception e) {
log.error("Exception while loading schema information for " + fullPartName, e);
throw new XmlException("Exception while loading schema information for " + fullPartName, e);
}
if ( elm != null ){
validateMessageBody(ctx, errors, elm.getType(), paths[0]);
// Ensure no other elements in body.
NodeList children = XmlUtils.getChildElements((Element) paths[0].getDomNode().getParentNode());
for (int c = 0; c < children.getLength(); c++){
QName childName = XmlUtils.getQName(children.item(c));
// Compare child QName to full part QName.
if (!fullPartName.equals(childName.toString())){
XmlCursor cur = paths[0].newCursor();
cur.toParent();
cur.toChild( childName );
errors.add( XmlError.forCursor( "Invalid element [" + childName + "] in SOAP Body", cur ) );
cur.dispose();
}
}
}
log.debug("SoapBody validation errors: " + errors.size());
}
return errors;
}
示例12: insertNewTable
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
/**
* 在某个段落起始处插入表格
*
* @param run
* @param row
* @param col
* @return
*/
public XWPFTable insertNewTable(XWPFRun run, int row, int col) {
XmlCursor cursor = ((XWPFParagraph)run.getParent()).getCTP().newCursor();
// XmlCursor cursor = run.getCTR().newCursor();
if (isCursorInBody(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, row, col);
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 tableCursor = t.newCursor();
try {
cursor.toCursor(tableCursor);
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl){
i++;
}
}
bodyElements.add(i > bodyElements.size() ? bodyElements.size() : i, newT);
// bodyElements.add(i, newT);
cursor.toCursor(tableCursor);
cursor.toEndToken();
return newT;
} finally {
tableCursor.dispose();
}
}
return null;
}
示例13: insertNewParagraph
import org.apache.xmlbeans.XmlCursor; //导入方法依赖的package包/类
/**
* 在某个段落起始处插入段落
* @param run
* @return
*/
public XWPFParagraph insertNewParagraph(XWPFRun run) {
// XmlCursor cursor = run.getCTR().newCursor();
XmlCursor cursor = ((XWPFParagraph)run.getParent()).getCTP().newCursor();
if (isCursorInBody(cursor)) {
String uri = CTP.type.getName().getNamespaceURI();
/*
* TODO DO not use a coded constant, find the constant in the OOXML
* classes instead, as the child of type CT_Paragraph is defined in the
* OOXML schema as 'p'
*/
String localPart = "p";
// creates a new Paragraph, cursor is positioned inside the new
// element
cursor.beginElement(localPart, uri);
// move the cursor to the START token to the paragraph just created
cursor.toParent();
CTP p = (CTP) cursor.getObject();
XWPFParagraph newP = new XWPFParagraph(p, this);
XmlObject o = null;
/*
* move the cursor to the previous element until a) the next
* paragraph is found or b) all elements have been passed
*/
while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
o = cursor.getObject();
}
/*
* if the object that has been found is a) not a paragraph or b) is
* the paragraph that has just been inserted, as the cursor in the
* while loop above was not moved as there were no other siblings,
* then the paragraph that was just inserted is the first paragraph
* in the body. Otherwise, take the previous paragraph and calculate
* the new index for the new paragraph.
*/
if ((!(o instanceof CTP)) || (CTP) o == p) {
paragraphs.add(0, newP);
} else {
int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
paragraphs.add(pos, newP);
}
/*
* create a new cursor, that points to the START token of the just
* inserted paragraph
*/
XmlCursor newParaPos = p.newCursor();
try {
/*
* Calculate the paragraphs index in the list of all body
* elements
*/
int i = 0;
cursor.toCursor(newParaPos);
while (cursor.toPrevSibling()) {
o = cursor.getObject();
if (o instanceof CTP || o instanceof CTTbl)
i++;
}
bodyElements.add(i, newP);
cursor.toCursor(newParaPos);
cursor.toEndToken();
return newP;
} finally {
newParaPos.dispose();
}
}
return null;
}