private static void addNoSchemaAttributes(XmlObject elem, List attrList) { XmlCursor cursor = elem.newCursor(); boolean hasAttributes = cursor.toFirstAttribute(); while (hasAttributes) { QName name = cursor.getName(); String namespaceURI = name.getNamespaceURI(); if ("".equals(namespaceURI) || "http://www.w3.org/2001/XMLSchema".equals(namespaceURI)) ; // no nothing else { String attValue = cursor.getTextValue(); String valUri; String prefix; if (attValue.indexOf(':') > 0) prefix = attValue.substring(0, attValue.indexOf(':')); else prefix = ""; cursor.push(); cursor.toParent(); valUri = cursor.namespaceForPrefix(prefix); cursor.pop(); attrList.add(new AttributeImpl(name, attValue, valUri)); //add the attribute } hasAttributes = cursor.toNextAttribute(); } cursor.dispose(); }
c.push(); System.out.println(generateXPath(c, null, ns)); c.pop(); c.toNextSibling(); c.push(); System.out.println(generateXPath(c, null, ns)); c.pop(); XmlCursor d = c.newCursor(); d.toParent(); c.push(); System.out.println(generateXPath(c, d, ns)); c.pop(); d.toParent(); c.push(); System.out.println(generateXPath(c, d, ns)); c.pop(); c.push(); System.out.println(generateXPath(c, d, ns)); c.pop(); c.toParent(); c.push(); System.out.println(generateXPath(c, d, ns)); c.pop(); c.push(); System.out.println(generateXPath(c, d, ns)); c.pop(); c.push(); System.out.println(generateXPath(c, d, ns)); c.pop(); c.toParent(); c.toParent(); c.push(); System.out.println(generateXPath(c, d, ns)); c.pop(); c.push(); System.out.println(generateXPath(c, null, ns)); c.pop();
private static String generateInternal(XmlCursor node, XmlCursor context, NamespaceContext nsctx) throws XPathGenerationException { if (node.isStartdoc()) return ""; if (context != null && node.isAtSamePositionAs(context)) return "."; assert node.isStart(); QName name = node.getName(); XmlCursor d = node.newCursor(); if (!node.toParent()) return "/" + name; int elemIndex = 0, i = 1; node.push(); if (!node.toChild(name)) throw new IllegalStateException("Must have at least one child with name: " + name); do { if (node.isAtSamePositionAs(d)) elemIndex = i; else i++; } while (node.toNextSibling(name)); node.pop(); d.dispose(); String pathToParent = generateInternal(node, context, nsctx); return i == 1 ? pathToParent + '/' + qnameToString(name, nsctx) : pathToParent + '/' + qnameToString(name, nsctx) + '[' + elemIndex + ']'; }
/** * Computes how many text nodes the * @param c the position in the document * @return how many text nodes occur before the position determined by <code>c</code> */ private static int countTextTokens(XmlCursor c) { int k = 0; int l = 0; XmlCursor d = c.newCursor(); c.push(); c.toParent(); TokenType tt = c.toFirstContentToken(); while (!tt.isEnd()) { if (tt.isText()) { if (c.comparePosition(d) > 0) // We have moved after the initial position l++; else k++; } else if (tt.isStart()) c.toEndToken(); tt = c.toNextToken(); } c.pop(); return l == 0 ? 0 : k; }
public static void getNamespaces(XmlCursor cursor, Map prefixToURI) { cursor.push(); while(cursor.toNextToken().isAnyAttr()) { if(cursor.isNamespace()) { javax.xml.namespace.QName name = cursor.getName(); String prefix = name.getLocalPart(); String uri = name.getNamespaceURI(); prefixToURI.put(prefix, uri); } } cursor.pop(); }
public DDBeanImpl(DDBeanRoot root, DDBean parent, String xpath, XmlCursor c) { this.root = root; this.parent = parent; this.xpath = xpath; this.children = new HashMap(); this.attributeMap = new HashMap(); content = c.getTextValue(); c.push(); if (c.toFirstAttribute()) { do { attributeMap.put(c.getName().getLocalPart(), c.getTextValue()); } while (c.toNextAttribute()); } c.pop(); c.push(); if (c.toFirstChild()) { do { String name = c.getName().getLocalPart(); List nodes = (List) children.get(name); if (nodes == null) { nodes = new ArrayList(); children.put(name, nodes); } nodes.add(new DDBeanImpl(root, this, xpath + "/" + name, c)); } while (c.toNextSibling()); } c.pop(); }
public static void removeNamespace(XmlCursor cursor, String prefix) { cursor.push(); while(cursor.toNextToken().isAnyAttr()) { if(cursor.isNamespace()) { javax.xml.namespace.QName name = cursor.getName(); if(name.getLocalPart().equals(prefix)) { cursor.removeXml(); break; } } } cursor.pop(); } }
if( cursor.getName().getLocalPart().equals( name ) ) { // Found it... content = cursor.getTextValue().replaceAll( ">_;", ">" ).replaceAll( "<_;", "<" ) ; break ; } } else if( cursor.isStart() && cursor.getObject().schemaType() == SelectionListType.type ) { break ; } } // end while return content ; } finally { cursor.dispose(); } } // end of readProcessingInstruction }
private static void addNoSchemaAttributes(XmlObject elem, List attrList) { XmlCursor cursor = elem.newCursor(); boolean hasAttributes = cursor.toFirstAttribute(); while (hasAttributes) { QName name = cursor.getName(); String namespaceURI = name.getNamespaceURI(); if ("".equals(namespaceURI) || "http://www.w3.org/2001/XMLSchema".equals(namespaceURI)) ; // no nothing else { String attValue = cursor.getTextValue(); String valUri; String prefix; if (attValue.indexOf(':') > 0) prefix = attValue.substring(0, attValue.indexOf(':')); else prefix = ""; cursor.push(); cursor.toParent(); valUri = cursor.namespaceForPrefix(prefix); cursor.pop(); attrList.add(new AttributeImpl(name, attValue, valUri)); //add the attribute } hasAttributes = cursor.toNextAttribute(); } cursor.dispose(); }
/** * @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; }
protected void convertPersistenceSchemaVersion(XmlCursor cursor, List<XmlCursor> locations) { if (locations != null) { for (XmlCursor location : locations) { location.push(); XmlCursor end = null; try { end = location.newCursor(); end.toCursor(location); end.toEndToken(); SchemaConversionUtils.convertSchemaVersion(location, end, SchemaConversionUtils.JPA_PERSISTENCE_NAMESPACE, "http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd", "2.0"); } finally { if (end != null) { try { end.dispose(); } catch (Exception e) { } } location.pop(); } } } } }
/** * * @param cursor * @param opts * @return */ private static String dumpNode(XmlCursor cursor, XmlOptions opts) { if (cursor.isText()) return cursor.getChars(); if (cursor.isFinish()) return ""; cursor.push(); boolean wanRawText = cursor.isStartdoc() && !cursor.toFirstChild(); cursor.pop(); return wanRawText ? cursor.getTextValue() : cursor.xmlText( opts ); }
private static void extractDependency(XmlCursor cursor, Environment environment) { cursor.push(); cursor.toFirstChild(); Artifact artifact; if (cursor.getName().getLocalPart().equals("uri")) { String uri = cursor.getTextValue(); artifact = toArtifact(uri); } else { checkName(cursor, "groupId"); String groupId = cursor.getTextValue(); cursor.toNextSibling(); String type = "jar"; if (cursor.getName().getLocalPart().equals("type")) { type = cursor.getTextValue(); cursor.toNextSibling(); } checkName(cursor, "artifactId"); String artifactId = cursor.getTextValue(); cursor.toNextSibling(); checkName(cursor, "version"); String version = cursor.getTextValue(); artifact = new Artifact(groupId, artifactId, version, type); } environment.addDependency(artifact, ImportType.ALL); cursor.pop(); cursor.removeXml(); }
/** * Computes how many text nodes the * @param c the position in the document * @return how many text nodes occur before the position determined by <code>c</code> */ private static int countTextTokens(XmlCursor c) { int k = 0; int l = 0; XmlCursor d = c.newCursor(); c.push(); c.toParent(); TokenType tt = c.toFirstContentToken(); while (!tt.isEnd()) { if (tt.isText()) { if (c.comparePosition(d) > 0) // We have moved after the initial position l++; else k++; } else if (tt.isStart()) c.toEndToken(); tt = c.toNextToken(); } c.pop(); return l == 0 ? 0 : k; }
private static String generateInternal(XmlCursor node, XmlCursor context, NamespaceContext nsctx) throws XPathGenerationException { if (node.isStartdoc()) return ""; if (context != null && node.isAtSamePositionAs(context)) return "."; assert node.isStart(); QName name = node.getName(); XmlCursor d = node.newCursor(); if (!node.toParent()) return "/" + name; int elemIndex = 0, i = 1; node.push(); if (!node.toChild(name)) throw new IllegalStateException("Must have at least one child with name: " + name); do { if (node.isAtSamePositionAs(d)) elemIndex = i; else i++; } while (node.toNextSibling(name)); node.pop(); d.dispose(); String pathToParent = generateInternal(node, context, nsctx); return i == 1 ? pathToParent + '/' + qnameToString(name, nsctx) : pathToParent + '/' + qnameToString(name, nsctx) + '[' + elemIndex + ']'; }
private static String generateInternal(XmlCursor node, XmlCursor context, NamespaceContext nsctx) throws XPathGenerationException { if (node.isStartdoc()) return ""; if (context != null && node.isAtSamePositionAs(context)) return "."; assert node.isStart(); QName name = node.getName(); XmlCursor d = node.newCursor(); if (!node.toParent()) return "/" + name; int elemIndex = 0, i = 1; node.push(); if (!node.toChild(name)) throw new IllegalStateException("Must have at least one child with name: " + name); do { if (node.isAtSamePositionAs(d)) elemIndex = i; else i++; } while (node.toNextSibling(name)); node.pop(); d.dispose(); String pathToParent = generateInternal(node, context, nsctx); return i == 1 ? pathToParent + '/' + qnameToString(name, nsctx) : pathToParent + '/' + qnameToString(name, nsctx) + '[' + elemIndex + ']'; }
/** * Computes how many text nodes the * @param c the position in the document * @return how many text nodes occur before the position determined by <code>c</code> */ private static int countTextTokens(XmlCursor c) { int k = 0; int l = 0; XmlCursor d = c.newCursor(); c.push(); c.toParent(); TokenType tt = c.toFirstContentToken(); while (!tt.isEnd()) { if (tt.isText()) { if (c.comparePosition(d) > 0) // We have moved after the initial position l++; else k++; } else if (tt.isStart()) c.toEndToken(); tt = c.toNextToken(); } c.pop(); return l == 0 ? 0 : k; }