Пример #1
0
  /**
   * Method processAttributes.
   *
   * @param node
   */
  protected void processAttributes(OMElement node) {
    int attribCount = parser.getAttributeCount();
    for (int i = 0; i < attribCount; i++) {
      String uri = parser.getAttributeNamespace(i);
      String prefix = parser.getAttributePrefix(i);

      OMNamespace namespace = null;
      if (uri != null && uri.length() > 0) {

        // prefix being null means this elements has a default namespace or it has inherited
        // a default namespace from its parent
        namespace = node.findNamespace(uri, prefix);
        if (namespace == null) {
          if (prefix == null || "".equals(prefix)) {
            prefix = OMSerializerUtil.getNextNSPrefix();
          }
          namespace = node.declareNamespace(uri, prefix);
        }
      }

      // todo if the attributes are supposed to namespace qualified all the time
      // todo then this should throw an exception here

      OMAttribute attr =
          node.addAttribute(
              parser.getAttributeLocalName(i), parser.getAttributeValue(i), namespace);
      attr.setAttributeType(parser.getAttributeType(i));
    }
  }
Пример #2
0
 public String getAttributePrefix(int index) {
   if (state == STATE_START_DOCUMENT || state == STATE_END_DOCUMENT) {
     throw new IllegalStateException();
   } else {
     return parent.getAttributePrefix(index);
   }
 }
Пример #3
0
  /**
   * Get the attributes associated with the given START_ELEMENT or ATTRIBUTE StAXevent.
   *
   * @return the StAX attributes converted to an org.xml.sax.Attributes
   */
  private Attributes getAttributes() {
    AttributesImpl attrs = new AttributesImpl();

    int eventType = staxStreamReader.getEventType();
    if (eventType != XMLStreamConstants.ATTRIBUTE
        && eventType != XMLStreamConstants.START_ELEMENT) {
      throw new InternalError("getAttributes() attempting to process: " + eventType);
    }

    // in SAX, namespace declarations are not part of attributes by default.
    // (there's a property to control that, but as far as we are concerned
    // we don't use it.) So don't add xmlns:* to attributes.

    // gather non-namespace attrs
    for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) {
      String uri = staxStreamReader.getAttributeNamespace(i);
      if (uri == null) uri = "";
      String localName = staxStreamReader.getAttributeLocalName(i);
      String prefix = staxStreamReader.getAttributePrefix(i);
      String qName;
      if (prefix == null || prefix.length() == 0) qName = localName;
      else qName = prefix + ':' + localName;
      String type = staxStreamReader.getAttributeType(i);
      String value = staxStreamReader.getAttributeValue(i);

      attrs.addAttribute(uri, localName, qName, type, value);
    }

    return attrs;
  }
Пример #4
0
 private void fillXMLAttributes(XMLStreamReader input) {
   fAttributes.removeAllAttributes();
   final int len = input.getAttributeCount();
   for (int i = 0; i < len; ++i) {
     fillQName(
         fAttributeQName,
         input.getAttributeNamespace(i),
         input.getAttributeLocalName(i),
         input.getAttributePrefix(i));
     String type = input.getAttributeType(i);
     fAttributes.addAttributeNS(
         fAttributeQName,
         (type != null) ? type : XMLSymbols.fCDATASymbol,
         input.getAttributeValue(i));
     fAttributes.setSpecified(i, input.isAttributeSpecified(i));
   }
 }
Пример #5
0
  private static void printAttributes(XMLStreamReader xmlr) {

    if (xmlr.getAttributeCount() > 0) {

      int count = xmlr.getAttributeCount();
      for (int i = 0; i < count; i++) {

        QName name = xmlr.getAttributeName(i);
        String namespace = xmlr.getAttributeNamespace(i);
        String type = xmlr.getAttributeType(i);
        String prefix = xmlr.getAttributePrefix(i);
        String value = xmlr.getAttributeValue(i);

        System.out.println(
            "\tAttribute: {" + namespace + "}:" + name.toString() + "(" + type + ")=" + value);
      }
    }
  }
 public String getAttributePrefix(final int index) {
   return streamReader.getAttributePrefix(index);
 }
Пример #7
0
      public static NfeCabecMsg parse(final javax.xml.stream.XMLStreamReader reader)
          throws java.lang.Exception {
        final NfeCabecMsg object = new NfeCabecMsg();

        java.lang.String nillableValue = null;
        try {

          while (!reader.isStartElement() && !reader.isEndElement()) {
            reader.next();
          }

          if (reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "type")
              != null) {
            final java.lang.String fullTypeName =
                reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "type");
            if (fullTypeName != null) {
              java.lang.String nsPrefix = null;
              if (fullTypeName.contains(":")) {
                nsPrefix = fullTypeName.substring(0, fullTypeName.indexOf(":"));
              }
              nsPrefix = nsPrefix == null ? "" : nsPrefix;

              final java.lang.String type = fullTypeName.substring(fullTypeName.indexOf(":") + 1);

              if (!"nfeCabecMsg".equals(type)) {
                // find namespace for the prefix
                final java.lang.String nsUri =
                    reader.getNamespaceContext().getNamespaceURI(nsPrefix);
                return (NfeCabecMsg) ExtensionMapper.getTypeObject(nsUri, type, reader);
              }
            }
          }

          // Note all attributes that were handled. Used to differ normal attributes
          // from anyAttributes.
          final java.util.Vector<String> handledAttributes = new java.util.Vector<>();

          // now run through all any or extra attributes
          // which were not reflected until now
          for (int i = 0; i < reader.getAttributeCount(); i++) {
            if (!handledAttributes.contains(reader.getAttributeLocalName(i))) {
              // this is an anyAttribute and we create
              // an OMAttribute for this
              final org.apache.axiom.om.OMFactory factory =
                  org.apache.axiom.om.OMAbstractFactory.getOMFactory();
              final org.apache.axiom.om.OMAttribute attr =
                  factory.createOMAttribute(
                      reader.getAttributeLocalName(i),
                      factory.createOMNamespace(
                          reader.getAttributeNamespace(i), reader.getAttributePrefix(i)),
                      reader.getAttributeValue(i));

              // and add it to the extra attributes

              object.addExtraAttributes(attr);
            }
          }

          reader.next();

          while (!reader.isStartElement() && !reader.isEndElement()) {
            reader.next();
          }

          if (reader.isStartElement()
              && new javax.xml.namespace.QName(
                      "http://www.portalfiscal.inf.br/nfe/wsdl/CadConsultaCadastro2", "versaoDados")
                  .equals(reader.getName())) {

            nillableValue =
                reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "nil");
            if ("true".equals(nillableValue) || "1".equals(nillableValue)) {
              throw new org.apache.axis2.databinding.ADBException(
                  "The element: " + "versaoDados" + "  cannot be null");
            }

            final java.lang.String content = reader.getElementText();

            object.setVersaoDados(
                org.apache.axis2.databinding.utils.ConverterUtil.convertToString(content));

            reader.next();

          } // End of if for expected property start element
          else {

          }

          while (!reader.isStartElement() && !reader.isEndElement()) {
            reader.next();
          }

          if (reader.isStartElement()
              && new javax.xml.namespace.QName(
                      "http://www.portalfiscal.inf.br/nfe/wsdl/CadConsultaCadastro2", "cUF")
                  .equals(reader.getName())) {

            nillableValue =
                reader.getAttributeValue("http://www.w3.org/2001/XMLSchema-instance", "nil");
            if ("true".equals(nillableValue) || "1".equals(nillableValue)) {
              throw new org.apache.axis2.databinding.ADBException(
                  "The element: " + "cUF" + "  cannot be null");
            }

            final java.lang.String content = reader.getElementText();

            object.setCUF(
                org.apache.axis2.databinding.utils.ConverterUtil.convertToString(content));

            reader.next();

          } // End of if for expected property start element
          else {

          }

          while (!reader.isStartElement() && !reader.isEndElement()) {
            reader.next();
          }

          if (reader.isStartElement()) {
            // A start element we are not expecting indicates a trailing invalid property
            throw new org.apache.axis2.databinding.ADBException(
                "Unexpected subelement " + reader.getName());
          }

        } catch (final javax.xml.stream.XMLStreamException e) {
          throw new java.lang.Exception(e);
        }

        return object;
      }
Пример #8
0
 public String getAttributePrefix(int index) {
   return reader.getAttributePrefix(index);
 }
Пример #9
0
  /** Parses the XML via StAX. */
  static Document buildDocument(XMLStreamReader xmlReader) throws XMLStreamException, IOException {
    Document doc = null;

    Stack<Element> elements = new Stack<Element>();
    while (xmlReader.hasNext()) {
      int eventType = xmlReader.next();
      switch (eventType) {
        case XMLEvent.START_DOCUMENT:
          doc = null;
          break;

        case XMLEvent.START_ELEMENT:
          // StartElement startElement = event.asStartElement();

          Element subElement;
          if (null != xmlReader.getPrefix()) {
            subElement =
                new Element(
                    xmlReader.getLocalName(), xmlReader.getNamespaceURI(), xmlReader.getPrefix());
          } else {
            subElement = new Element(xmlReader.getLocalName(), xmlReader.getNamespaceURI());
          }

          if (elements.isEmpty()) {
            doc = new Document(subElement);
          } else {
            elements.peek().appendChild(subElement);
          }

          for (int i = 0; i < xmlReader.getAttributeCount(); ++i) {
            if (null != xmlReader.getAttributePrefix(i)) {
              subElement.addAttribute(
                  new org.eclipse.stardust.engine.core.struct.sxml.Attribute(
                      xmlReader.getAttributeLocalName(i),
                      xmlReader.getAttributeNamespace(i),
                      xmlReader.getAttributePrefix(i),
                      xmlReader.getAttributeValue(i)));
            } else {
              subElement.addAttribute(
                  new org.eclipse.stardust.engine.core.struct.sxml.Attribute(
                      xmlReader.getAttributeLocalName(i),
                      xmlReader.getAttributeNamespace(i),
                      xmlReader.getAttributeValue(i)));
            }
          }

          elements.push(subElement);
          break;

        case XMLEvent.CHARACTERS:
          org.eclipse.stardust.engine.core.struct.sxml.Text text =
              new org.eclipse.stardust.engine.core.struct.sxml.Text(xmlReader.getText());
          if (!elements.isEmpty()) {
            elements.peek().appendChild(text);
          } else {
            trace.info("Ignoring text placed outside of any element: " + xmlReader.getText());
          }
          break;

        case XMLEvent.SPACE:
          trace.debug("Ignoring whitespace inside XML.");
          break;

        case XMLEvent.END_ELEMENT:
          elements.pop();
          break;

        case XMLEvent.END_DOCUMENT:
          break;

        default:
          trace.error("Unsupported StAX event type: " + eventType);
      }
    }

    return doc;
  }