private void compareStartElement(XMLStreamReader orig, XMLStreamReader actual) throws Exception {
   Assert.assertEquals("Start element is not matched", orig.getName(), actual.getName());
   int origAttrCount = orig.getAttributeCount();
   int actualAttrCount = actual.getAttributeCount();
   for (int i = 0; i < origAttrCount; i++) {
     QName origAttrName = orig.getAttributeName(i);
     if ((origAttrName.getLocalPart().equals("location"))
         || (origAttrName.getLocalPart().equals("schemaLocation"))) {
       // skip this atribute
       origAttrCount--;
     } else {
       Assert.assertEquals(
           "Attribute " + origAttrName + " not found or value not matching",
           orig.getAttributeValue(origAttrName.getNamespaceURI(), origAttrName.getLocalPart()),
           actual.getAttributeValue(origAttrName.getNamespaceURI(), origAttrName.getLocalPart()));
     }
   }
   for (int i = 0; i < actualAttrCount; i++) {
     QName actualAttrName = actual.getAttributeName(i);
     if ((actualAttrName.getLocalPart().equals("location"))
         || (actualAttrName.getLocalPart().equals("schemaLocation"))) {
       // skip this atribute
       actualAttrCount--;
     }
   }
   Assert.assertEquals(
       "Attribute count is not matched for element " + orig.getName(),
       origAttrCount,
       actualAttrCount);
 }
Beispiel #2
0
 /**
  * Returns the attributes of the element that the given {@link XMLStreamReader} points to.
  *
  * @param reader xml stream, must not be <code>null</code> and point to a {@link
  *     XMLStreamConstants#START_ELEMENT}
  * @return attributes of the element, can be empty, but never <code>null</code>
  */
 public static Map<QName, String> getAttributes(XMLStreamReader reader) {
   HashMap<QName, String> attrs = new HashMap<QName, String>();
   for (int i = 0; i < reader.getAttributeCount(); i++) {
     attrs.put(reader.getAttributeName(i), reader.getAttributeValue(i));
   }
   return attrs;
 }
 public QName getAttributeName(int index) {
   if (state == STATE_START_DOCUMENT || state == STATE_END_DOCUMENT) {
     throw new IllegalStateException();
   } else {
     return parent.getAttributeName(index);
   }
 }
  private static void parsePath(
      final XMLStreamReader reader, final boolean include, final MultiplePathFilterBuilder builder)
      throws XMLStreamException {
    String path = null;
    final Set<Attribute> required = EnumSet.of(Attribute.PATH);
    final int count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
      final Attribute attribute = Attribute.of(reader.getAttributeName(i));
      required.remove(attribute);
      switch (attribute) {
        case PATH:
          path = reader.getAttributeValue(i);
          break;
        default:
          throw unexpectedContent(reader);
      }
    }
    if (!required.isEmpty()) {
      throw missingAttributes(reader.getLocation(), required);
    }

    builder.addFilter(PathFilters.match(path), include);

    // consume remainder of element
    parseNoContent(reader);
  }
Beispiel #5
0
 public void parseAttributes(XMLStreamReader parser) {
   for (int i = 0; i < parser.getAttributeCount(); i++) {
     QName attr = parser.getAttributeName(i);
     if (attr.getLocalPart().equals("id")) {
       id = parser.getAttributeValue(i);
     } else if (attr.getLocalPart().equals("name")) {
       name = parser.getAttributeValue(i);
     }
   }
 }
Beispiel #6
0
 public static String getAttributeByLocalName(XMLStreamReader reader, String localName) {
   String result = "";
   for (int i = 0; i < reader.getAttributeCount(); i++) {
     QName attribute = reader.getAttributeName(i);
     if (attribute != null && attribute.getLocalPart().equals(localName)) {
       result = reader.getAttributeValue(i);
     }
   }
   return result;
 }
Beispiel #7
0
  private void doParsing(XMLStreamReader parser) throws XMLStreamException {
    for (int i = 0; i < parser.getAttributeCount(); i++) {
      QName attr = parser.getAttributeName(i);
      if (attr.getLocalPart().equals("source")) {
        source = XMLIDREFUtils.parse(parser.getAttributeValue(i));
      } else {
        JAGTLog.exception(
            "Unsupported ", this.getClass().getSimpleName(), " Attr tag: ", attr.getLocalPart());
      }
    }

    for (int event = parser.next();
        event != XMLStreamConstants.END_DOCUMENT;
        event = parser.next()) {
      switch (event) {
        case XMLStreamConstants.START_ELEMENT:
          {
            String localName = parser.getLocalName();
            if (localName.equals("source")) {
              XMLSource src = new XMLSource();
              src.parse(parser);
              sources.add(src);
            } else if (localName.equals("bind_shape_matrix")) {
              if (bindShapeMatrix != null) {
                JAGTLog.exception(
                    this.getClass().getSimpleName(), " too many bind_shape_matrix tags.");
              }

              bindShapeMatrix = XMLMatrixUtils.readColumnMajor(StAXHelper.parseText(parser));
              // bindShapeMatrix = XMLMatrixUtils.readRowMajor( StAXHelper.parseText( parser ) );
            } else if (localName.equals("joints")) {
              jointsInputs = getJointInputs(parser);
            } else if (localName.equals("vertex_weights")) {
              vertexWeights = new XMLVertexWeights();
              vertexWeights.parse(parser);
            } else {
              JAGTLog.exception(
                  "Unsupported ",
                  this.getClass().getSimpleName(),
                  " Start tag: ",
                  parser.getLocalName());
            }
            break;
          }
        case XMLStreamConstants.END_ELEMENT:
          {
            if (parser.getLocalName().equals("skin")) {
              return;
            }
            break;
          }
      }
    }
  }
Beispiel #8
0
 /**
  * Method to get a HashMap of XML attributes and values
  *
  * @param reader an XMLStreamReader object
  * @return a HashMap of attributes
  */
 private HashMap<QName, String> getAttributes(XMLStreamReader reader) {
   HashMap<QName, String> rawAttributesMap = new HashMap<QName, String>();
   int attributeCount = reader.getAttributeCount();
   for (int i = 0; i < attributeCount; i++) {
     QName attributeQName = reader.getAttributeName(i);
     String attributeValue = reader.getAttributeValue(i);
     if (log.isTraceEnabled())
       log.trace("attributeQName: " + attributeQName + " attributeValue: " + attributeValue);
     rawAttributesMap.put(attributeQName, attributeValue);
   }
   return rawAttributesMap;
 }
Beispiel #9
0
  public void parse(XMLStreamReader parser) throws XMLStreamException {

    for (int i = 0; i < parser.getAttributeCount(); i++) {
      QName attr = parser.getAttributeName(i);
      if (attr.getLocalPart().equals("id")) {
        id = parser.getAttributeValue(i);
      } else if (attr.getLocalPart().equals("name")) {
        name = parser.getAttributeValue(i);
      } else {
        JAGTLog.exception(
            "Unsupported ", this.getClass().getSimpleName(), " Attr tag: ", parser.getLocalName());
      }
    }

    for (int event = parser.next();
        event != XMLStreamConstants.END_DOCUMENT;
        event = parser.next()) {
      switch (event) {
        case XMLStreamConstants.START_ELEMENT:
          {
            String localName = parser.getLocalName();
            if (localName.equals("geometry")) {
              parseAttributes(parser);
            } else if (localName.equals("asset")) {
              asset = new XMLAsset();
              asset.parse(parser);
            } else if (localName.equals("mesh")) {
              mesh = new XMLMesh();
              mesh.parse(parser);
            } else {
              JAGTLog.exception(
                  "Unsupported ",
                  this.getClass().getSimpleName(),
                  " Start tag: ",
                  parser.getLocalName());
            }
            break;
          }
        case XMLStreamConstants.END_ELEMENT:
          {
            if (parser.getLocalName().equals("geometry")) return;
            break;
          }
      }
    }
  }
  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);
      }
    }
  }
 private String parseName(final XMLStreamReader reader) throws XMLStreamException {
   final int count = reader.getAttributeCount();
   String name = null;
   final Set<Attribute> required = EnumSet.of(Attribute.NAME);
   for (int i = 0; i < count; i++) {
     final Attribute attribute = Attribute.of(reader.getAttributeName(i));
     required.remove(attribute);
     switch (attribute) {
       case NAME:
         name = reader.getAttributeValue(i);
         break;
       default:
         throw ParsingUtils.unexpectedContent(reader);
     }
   }
   if (!required.isEmpty()) {
     throw ParsingUtils.missingAttributes(reader.getLocation(), required);
   }
   ParsingUtils.parseNoContent(reader);
   return propertyReplacer.replaceProperties(name);
 }
 private Artifact parseArtifact(final XMLStreamReader reader) throws XMLStreamException {
   final int count = reader.getAttributeCount();
   String groupId = null;
   String artifactId = null;
   String version = null;
   String classifier = null;
   String extension = null;
   final Set<Attribute> required =
       EnumSet.of(Attribute.GROUP_ID, Attribute.ARTIFACT_ID, Attribute.VERSION);
   for (int i = 0; i < count; i++) {
     final Attribute attribute = Attribute.of(reader.getAttributeName(i));
     required.remove(attribute);
     switch (attribute) {
       case GROUP_ID:
         groupId = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case ARTIFACT_ID:
         artifactId = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case VERSION:
         version = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case CLASSIFIER:
         classifier = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       case EXTENSION:
         extension = propertyReplacer.replaceProperties(reader.getAttributeValue(i));
         break;
       default:
         throw ParsingUtils.unexpectedContent(reader);
     }
   }
   if (!required.isEmpty()) {
     throw ParsingUtils.missingAttributes(reader.getLocation(), required);
   }
   ParsingUtils.parseNoContent(reader);
   return new Artifact(groupId, artifactId, classifier, extension, version);
 }
 private static void parseMainClass(
     final XMLStreamReader reader, final ModuleSpec.Builder specBuilder)
     throws XMLStreamException {
   String name = null;
   final Set<Attribute> required = EnumSet.of(Attribute.NAME);
   final int count = reader.getAttributeCount();
   for (int i = 0; i < count; i++) {
     final Attribute attribute = Attribute.of(reader.getAttributeName(i));
     required.remove(attribute);
     switch (attribute) {
       case NAME:
         name = reader.getAttributeValue(i);
         break;
       default:
         throw unexpectedContent(reader);
     }
   }
   if (!required.isEmpty()) {
     throw missingAttributes(reader.getLocation(), required);
   }
   specBuilder.setMainClass(name);
   // consume remainder of element
   parseNoContent(reader);
 }
Beispiel #14
0
    private Element(XMLStreamReader stream, Element parent) throws XMLStreamException {
      // We assume that the stream points to the start of the modelled element
      if (stream.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new AssertionError();
      }

      //
      QName name = stream.getName();
      Location location = stream.getLocation();

      //
      Map<String, String> attributes = Collections.emptyMap();
      Map<QName, String> qualifiedAttributes = Collections.emptyMap();
      int attributeCount = stream.getAttributeCount();
      for (int i = 0; i < attributeCount; i++) {
        String attributeValue = stream.getAttributeValue(i);
        QName attributeName = stream.getAttributeName(i);
        if (XMLConstants.NULL_NS_URI.equals(attributeName.getNamespaceURI())) {
          if (attributes.isEmpty()) {
            attributes = new HashMap<String, String>();
          }
          attributes.put(attributeName.getLocalPart(), attributeValue);
        } else {
          if (qualifiedAttributes.isEmpty()) {
            qualifiedAttributes = new HashMap<QName, String>();
          }
          qualifiedAttributes.put(attributeName, attributeValue);
        }
      }

      //
      Map<String, String> namespaces;
      int namespaceCount = stream.getNamespaceCount();
      if (namespaceCount > 0) {
        namespaces = new HashMap<String, String>();
        for (int i = 0; i < namespaceCount; i++) {
          String namespacePrefix = stream.getNamespacePrefix(i);
          if (namespacePrefix == null) {
            namespacePrefix = "";
          }
          String namespaceURI = stream.getNamespaceURI(i);
          namespaces.put(namespacePrefix, namespaceURI);
        }
      } else {
        namespaces = Collections.emptyMap();
      }

      // When we leave we assume that we are positionned on the next element start or the document
      // end
      StringBuilder sb = null;
      String chunk = null;
      Object content = null;
      while (true) {
        stream.next();
        int type = stream.getEventType();
        if (type == XMLStreamConstants.END_DOCUMENT || type == XMLStreamConstants.START_ELEMENT) {
          break;
        } else if (type == XMLStreamConstants.CHARACTERS) {
          if (chunk == null) {
            chunk = stream.getText();
          } else {
            if (sb == null) {
              sb = new StringBuilder(chunk);
            }
            sb.append(stream.getText());
          }
        } else if (type == XMLStreamConstants.END_ELEMENT) {
          if (sb != null) {
            content = sb;
          } else {
            content = chunk;
          }
          break;
        }
      }

      //
      int depth = 1 + (parent != null ? parent.getDepth() : 0);

      //
      this.parent = parent;
      this.name = name;
      this.depth = depth;
      this.content = content;
      this.attributes = attributes;
      this.qualifiedAttributes = qualifiedAttributes;
      this.namespaces = namespaces;
      this.location = location;
    }
 public QName getAttributeName(final int index) {
   return streamReader.getAttributeName(index);
 }
 private static void parseModuleContents(
     final File root, final XMLStreamReader reader, final ModuleSpec.Builder specBuilder)
     throws XMLStreamException {
   final int count = reader.getAttributeCount();
   String name = null;
   String slot = null;
   final Set<Attribute> required = EnumSet.of(Attribute.NAME);
   for (int i = 0; i < count; i++) {
     final Attribute attribute = Attribute.of(reader.getAttributeName(i));
     required.remove(attribute);
     switch (attribute) {
       case NAME:
         name = reader.getAttributeValue(i);
         break;
       case SLOT:
         slot = reader.getAttributeValue(i);
         break;
       default:
         throw unexpectedContent(reader);
     }
   }
   if (!required.isEmpty()) {
     throw missingAttributes(reader.getLocation(), required);
   }
   if (!specBuilder.getIdentifier().equals(ModuleIdentifier.create(name, slot))) {
     throw invalidModuleName(reader.getLocation(), specBuilder.getIdentifier());
   }
   // xsd:all
   Set<Element> visited = EnumSet.noneOf(Element.class);
   while (reader.hasNext()) {
     switch (reader.nextTag()) {
       case XMLStreamConstants.END_ELEMENT:
         {
           specBuilder.addDependency(DependencySpec.createLocalDependencySpec());
           return;
         }
       case XMLStreamConstants.START_ELEMENT:
         {
           final Element element = Element.of(reader.getName());
           if (visited.contains(element)) {
             throw unexpectedContent(reader);
           }
           visited.add(element);
           switch (element) {
             case DEPENDENCIES:
               parseDependencies(reader, specBuilder);
               break;
             case MAIN_CLASS:
               parseMainClass(reader, specBuilder);
               break;
             case RESOURCES:
               parseResources(root, reader, specBuilder);
               break;
             default:
               throw unexpectedContent(reader);
           }
           break;
         }
       default:
         {
           throw unexpectedContent(reader);
         }
     }
   }
   throw endOfDocument(reader.getLocation());
 }
 private static void parseModuleDependency(
     final XMLStreamReader reader, final ModuleSpec.Builder specBuilder)
     throws XMLStreamException {
   String name = null;
   String slot = null;
   boolean export = false;
   boolean optional = false;
   final Set<Attribute> required = EnumSet.of(Attribute.NAME);
   final int count = reader.getAttributeCount();
   for (int i = 0; i < count; i++) {
     final Attribute attribute = Attribute.of(reader.getAttributeName(i));
     required.remove(attribute);
     switch (attribute) {
       case NAME:
         name = reader.getAttributeValue(i);
         break;
       case SLOT:
         slot = reader.getAttributeValue(i);
         break;
       case EXPORT:
         export = Boolean.parseBoolean(reader.getAttributeValue(i));
         break;
       case OPTIONAL:
         optional = Boolean.parseBoolean(reader.getAttributeValue(i));
         break;
       default:
         throw unexpectedContent(reader);
     }
   }
   if (!required.isEmpty()) {
     throw missingAttributes(reader.getLocation(), required);
   }
   final MultiplePathFilterBuilder importBuilder = PathFilters.multiplePathFilterBuilder(true);
   final MultiplePathFilterBuilder exportBuilder = PathFilters.multiplePathFilterBuilder(true);
   while (reader.hasNext()) {
     switch (reader.nextTag()) {
       case XMLStreamConstants.END_ELEMENT:
         {
           final PathFilter exportFilter =
               export ? exportBuilder.create() : PathFilters.rejectAll();
           final PathFilter importFilter = importBuilder.create();
           specBuilder.addDependency(
               DependencySpec.createModuleDependencySpec(
                   importFilter,
                   exportFilter,
                   null,
                   ModuleIdentifier.create(name, slot),
                   optional));
           return;
         }
       case XMLStreamConstants.START_ELEMENT:
         {
           switch (Element.of(reader.getName())) {
             case EXPORTS:
               parseFilterList(reader, exportBuilder);
               break;
             case IMPORTS:
               parseFilterList(reader, importBuilder);
               break;
             default:
               throw unexpectedContent(reader);
           }
           break;
         }
       default:
         {
           throw unexpectedContent(reader);
         }
     }
   }
 }
Beispiel #18
0
 public QName getAttributeName(int index) {
   return reader.getAttributeName(index);
 }
  private static void parseResourceRoot(
      final File root, final XMLStreamReader reader, final ModuleSpec.Builder specBuilder)
      throws XMLStreamException {
    final ModuleIdentifier identifier = specBuilder.getIdentifier();
    String name = null;
    String path = null;
    final Set<Attribute> required = EnumSet.of(Attribute.PATH);
    final int count = reader.getAttributeCount();
    for (int i = 0; i < count; i++) {
      final Attribute attribute = Attribute.of(reader.getAttributeName(i));
      required.remove(attribute);
      switch (attribute) {
        case NAME:
          name = reader.getAttributeValue(i);
          break;
        case PATH:
          path = reader.getAttributeValue(i);
          break;
        default:
          throw unexpectedContent(reader);
      }
    }
    if (!required.isEmpty()) {
      throw missingAttributes(reader.getLocation(), required);
    }
    if (name == null) name = path;
    // todo add to spec
    final File file = new File(root, path);

    final ResourceLoader resourceLoader;
    final MultiplePathFilterBuilder builder = PathFilters.multiplePathFilterBuilder(true);

    while (reader.hasNext()) {
      switch (reader.nextTag()) {
        case XMLStreamConstants.END_ELEMENT:
          {
            final PathFilter exportFilter = builder.create();
            if (file.isDirectory()) {
              resourceLoader = new FileResourceLoader(identifier, file, name, exportFilter);
            } else {
              try {
                resourceLoader =
                    new JarFileResourceLoader(identifier, new JarFile(file), name, exportFilter);
              } catch (IOException e) {
                throw new XMLStreamException("Invalid JAR file specified", reader.getLocation(), e);
              }
            }
            specBuilder.addResourceRoot(resourceLoader);
            return;
          }
        case XMLStreamConstants.START_ELEMENT:
          {
            switch (Element.of(reader.getName())) {
              case EXPORTS:
                parseFilterList(reader, builder);
                break;
              default:
                throw unexpectedContent(reader);
            }
            break;
          }
        default:
          {
            throw unexpectedContent(reader);
          }
      }
    }
  }