Java Code Examples for org.apache.xmlbeans.XmlCursor#isNamespace()

The following examples show how to use org.apache.xmlbeans.XmlCursor#isNamespace() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EbicsXmlFactory.java    From ebics-java-client with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
  * Qualifies a valid member of a substitution group. This method tries to use the
  * built-in {@link XmlObject#substitute(QName, SchemaType)} and if succesful returns
  * a valid substitution which is usable (not disconnected). If it fails, it uses
  * low-level {@link XmlCursor} manipulation to qualify the substitution group. Note
  * that if the latter is the case the resulting document is disconnected and should
  * no longer be manipulated. Thus, use it as a final step after all markup is included.
  *
  * If newType is null, this method will skip {@link XmlObject#substitute(QName, SchemaType)}
  * and directly use {@link XmlCursor}. This can be used, if you are sure that the substitute
  * is not in the list of (pre-compiled) valid substitutions (this is the case if a schema
  * uses another schema's type as a base for elements. E.g. om:Observation uses gml:_Feature
  * as the base type).
  *
  * @param xobj
  * 		the abstract element
  * @param newInstance
  * 		the new {@link QName} of the instance
  * @param newType the new schemaType. if null, cursors will be used and the resulting object
  * 		will be disconnected.
  * @return if successful applied {@link XmlObject#substitute(QName, SchemaType)} a living object with a
  * 		type == newType is returned. Otherwise null is returned as you can no longer manipulate the object.
  */
 public static XmlObject qualifySubstitutionGroup(XmlObject xobj, QName newInstance, SchemaType newType) {
   XmlObject	substitute = null;

   if (newType != null) {
     substitute = xobj.substitute(newInstance, newType);
     if (substitute != null && substitute.schemaType() == newType
  && substitute.getDomNode().getLocalName().equals(newInstance.getLocalPart()))
     {
return substitute;
     }
   }

   XmlCursor cursor = xobj.newCursor();
   cursor.setName(newInstance);
   QName qName = new QName("http://www.w3.org/2001/XMLSchema-instance", "type");
   cursor.removeAttribute(qName);
   cursor.toNextToken();
   if (cursor.isNamespace()) {
     cursor.removeXml();
   }

   cursor.dispose();

   return null;
 }
 
Example 2
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 3
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: XmlHelper.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
/**
 * Remove namespace declarations from an xml fragment (useful for moving all
 * declarations to a document root
 *
 * @param x
 *            The fragment to localize
 */
public static void removeNamespaces(final XmlObject x) {
    final XmlCursor c = x.newCursor();
    while (c.hasNextToken()) {
        if (c.isNamespace()) {
            c.removeXml();
        } else {
            c.toNextToken();
        }
    }
    c.dispose();
}
 
Example 5
Source File: EbicsXmlFactory.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Qualifies a valid member of a substitution group. This method tries to use the built-in {@link
 * XmlObject#substitute(QName, SchemaType)} and if succesful returns a valid substitution which is
 * usable (not disconnected). If it fails, it uses low-level {@link XmlCursor} manipulation to
 * qualify the substitution group. Note that if the latter is the case the resulting document is
 * disconnected and should no longer be manipulated. Thus, use it as a final step after all markup
 * is included.
 *
 * <p>If newType is null, this method will skip {@link XmlObject#substitute(QName, SchemaType)}
 * and directly use {@link XmlCursor}. This can be used, if you are sure that the substitute is
 * not in the list of (pre-compiled) valid substitutions (this is the case if a schema uses
 * another schema's type as a base for elements. E.g. om:Observation uses gml:_Feature as the base
 * type).
 *
 * @param xobj the abstract element
 * @param newInstance the new {@link QName} of the instance
 * @param newType the new schemaType. if null, cursors will be used and the resulting object will
 *     be disconnected.
 * @return if successful applied {@link XmlObject#substitute(QName, SchemaType)} a living object
 *     with a type == newType is returned. Otherwise null is returned as you can no longer
 *     manipulate the object.
 */
public static XmlObject qualifySubstitutionGroup(
    XmlObject xobj, QName newInstance, SchemaType newType) {
  XmlObject substitute = null;

  if (newType != null) {
    substitute = xobj.substitute(newInstance, newType);
    if (substitute != null
        && substitute.schemaType() == newType
        && substitute.getDomNode().getLocalName().equals(newInstance.getLocalPart())) {
      return substitute;
    }
  }

  XmlCursor cursor = xobj.newCursor();
  cursor.setName(newInstance);
  QName qName = new QName("http://www.w3.org/2001/XMLSchema-instance", "type");
  cursor.removeAttribute(qName);
  cursor.toNextToken();
  if (cursor.isNamespace()) {
    cursor.removeXml();
  }

  cursor.dispose();

  return null;
}
 
Example 6
Source File: NamespaceHelper.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Updates the internal state of this NamespaceHelper with the
 * namespace information of the element pointed to by the cursor.
 */
private void update(XmlCursor cursor, ObjArray declarations)
{
    // Process the Namespace declarations
    cursor.push();
    while(cursor.toNextToken().isAnyAttr())
    {
        if(cursor.isNamespace())
        {
            javax.xml.namespace.QName name = cursor.getName();
            String prefix = name.getLocalPart();
            String uri = name.getNamespaceURI();

            declareNamespace(prefix, uri, declarations);
        }
    }
    cursor.pop();

    // Process the element
    processName(cursor, declarations);

    // Process the attributes
    cursor.push();
    boolean hasNext = cursor.toFirstAttribute();
    while(hasNext)
    {
        processName(cursor, declarations);
        hasNext = cursor.toNextAttribute();
    }
    cursor.pop();
}