Пример #1
0
  public void read() throws ContentFileProcessingException {
    aliasesData.aliasOverrides = new ArrayList<>();
    aliasesData.groupNames = new ArrayList<>();

    if (aliasesFile.exists()) {
      try (Reader fileReader = new UnicodeReader(aliasesFile, defaultFileCharacterEncoding)) {
        XMLStreamReader2 streamReader =
            XmlStreamReaderFactory.createReader(fileReader, aliasesSchema);

        while (streamReader.hasNext()) {
          if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
            switch (streamReader.getLocalName()) {
              case "aliases":
                processAliases(streamReader);
                break;

              case "alias":
                processAlias(streamReader);
                break;
            }
          }

          streamReader.next();
        }
      } catch (XMLStreamException e) {
        Location location = e.getLocation();

        throw new ContentFileProcessingException(
            aliasesFile, location.getLineNumber(), location.getColumnNumber(), e.getMessage());
      } catch (IOException | IncompleteAliasException e) {
        throw new ContentFileProcessingException(aliasesFile, e);
      }
    }
  }
Пример #2
0
 public MetadataRecord fromXml(String recordContents) throws XMLStreamException {
   String recordString = createCompleteRecordString(recordContents);
   try {
     Reader reader = new StringReader(recordString);
     XMLStreamReader2 input = (XMLStreamReader2) inputFactory.createXMLStreamReader(reader);
     GroovyNode rootNode = null, node = null;
     StringBuilder value = new StringBuilder();
     while (true) {
       switch (input.getEventType()) {
         case XMLEvent.START_DOCUMENT:
           break;
         case XMLEvent.START_ELEMENT:
           if (node == null) {
             rootNode = node = new GroovyNode(null, "input");
           } else {
             node =
                 new GroovyNode(
                     node, input.getNamespaceURI(), input.getLocalName(), input.getPrefix());
           }
           if (input.getAttributeCount() > 0) {
             for (int walk = 0; walk < input.getAttributeCount(); walk++) {
               QName attributeName = input.getAttributeName(walk);
               node.attributes()
                   .put(attributeName.getLocalPart(), input.getAttributeValue(walk));
             }
           }
           value.setLength(0);
           break;
         case XMLEvent.CHARACTERS:
         case XMLEvent.CDATA:
           value.append(input.getText());
           break;
         case XMLEvent.END_ELEMENT:
           if (node == null) {
             throw new RuntimeException("Node cannot be null");
           }
           String valueString = sanitize(value.toString());
           value.setLength(0);
           if (valueString.length() > 0) {
             node.setValue(valueString);
           }
           node = node.parent();
           break;
         case XMLEvent.END_DOCUMENT:
           {
             break;
           }
       }
       if (!input.hasNext()) {
         break;
       }
       input.next();
     }
     return new MetadataRecord(rootNode, -1, -1);
   } catch (WstxParsingException e) {
     throw new XMLStreamException("Problem parsing record:\n" + recordString, e);
   }
 }
  /**
   * Parses the whole ESummaryResult XML object, delivering a List of ESummaryResults.
   *
   * @param in the input stream through which the response the response can be read.
   * @return multimap with the mappings from the XML.
   * @throws javax.xml.stream.XMLStreamException
   */
  public List<T> parseESummaryResult(InputStream in) throws XMLStreamException {

    XMLInputFactory2 xmlif = (XMLInputFactory2) XMLInputFactory2.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
    xmlif.configureForSpeed();

    XMLStreamReader2 xmlr = (XMLStreamReader2) xmlif.createXMLStreamReader(in);

    int event;

    List<T> results = new ArrayList<T>();
    T currentResult = getNewESummaryResult();

    while (xmlr.hasNext()) {
      event = xmlr.next();

      switch1:
      switch (event) {
        case XMLEvent.START_DOCUMENT:
          break;
        case XMLEvent.START_ELEMENT:
          // LOGGER.info("Start Element: "+xmlr.getLocalName());
          // LOGGER.info("Attributes: "+getAttributes(xmlr));
          if (xmlr.getLocalName().equalsIgnoreCase("Item")) {
            boolean done = false;

            for (Enum keyword : currentResult.getScalarKeywords()) {
              if (hasAttributeNameWithValue(xmlr, keyword.toString())) {
                // LOGGER.info("Entering addScalarForKeyword: "+keyword.toString()+" for
                // "+xmlr.getLocalName());
                currentResult.addScalarForKeyword(keyword, getFollowingCharacters(xmlr));
                break switch1;
              }
            }
            for (Enum keyword : currentResult.getListKeywords()) {
              if (hasAttributeNameWithValue(xmlr, keyword.toString())) {
                // LOGGER.info("Entering addListForKeyword: "+keyword.toString()+" for
                // "+xmlr.getLocalName());
                currentResult.addListForKeyword(keyword, parseList(xmlr));
                break switch1;
              }
            }
          }
          if (xmlr.getLocalName().equalsIgnoreCase("Id")) {
            for (Enum keyword : currentResult.getScalarKeywords()) {
              if (keyword.toString().equalsIgnoreCase("Id")) {
                currentResult.addScalarForKeyword(keyword, getFollowingCharacters(xmlr));
                break switch1;
              }
            }
          }
          /*
          if (xmlr.getLocalName().equalsIgnoreCase("Item") && hasAttributeNameWithValue(xmlr, "SID")) {
              currentResult.setId(getFollowingCharacters(xmlr));
          } else if (xmlr.getLocalName().equalsIgnoreCase("Item") && hasAttributeNameWithValue(xmlr, "SourceNameList")) {
              currentResult.setSourceNames(parseList(xmlr));
          } else if (xmlr.getLocalName().equalsIgnoreCase("Item") && hasAttributeNameWithValue(xmlr, "SourceID")) {
              currentResult.addSourceID(getFollowingCharacters(xmlr));
          } else if (xmlr.getLocalName().equalsIgnoreCase("Item") && hasAttributeNameWithValue(xmlr, "DBUrl")) {
              currentResult.setDBUrl(getFollowingCharacters(xmlr));
          } else if (xmlr.getLocalName().equalsIgnoreCase("Item") && hasAttributeNameWithValue(xmlr, "SynonymList")) {
              currentResult.setSynonyms(parseList(xmlr));
          }*/

          break;
        case XMLEvent.END_ELEMENT:
          // LOGGER.info("End Element: "+xmlr.getLocalName());
          if (xmlr.getLocalName().equalsIgnoreCase("DocSum")) {
            currentResult.wrap();
            results.add(currentResult);
            currentResult = getNewESummaryResult();
          }
          break;
      }
    }
    xmlr.closeCompletely();
    return results;
  }
 public MetadataRecord metadataRecordFrom(String recordContents) throws XMLStreamException {
   try {
     Reader reader = new StringReader(recordContents);
     XMLStreamReader2 input = (XMLStreamReader2) inputFactory.createXMLStreamReader(reader);
     GroovyNode rootNode = null, node = null;
     StringBuilder value = new StringBuilder();
     while (true) {
       switch (input.getEventType()) {
         case XMLEvent.START_DOCUMENT:
           break;
         case XMLEvent.START_ELEMENT:
           node =
               new GroovyNode(
                   node, input.getNamespaceURI(), input.getLocalName(), input.getPrefix());
           if (rootNode == null) {
             rootNode = node;
           }
           if (input.getAttributeCount() > 0) {
             for (int walk = 0; walk < input.getAttributeCount(); walk++) {
               QName attributeName = input.getAttributeName(walk);
               if (attributeName.getPrefix() == null || attributeName.getPrefix().isEmpty()) {
                 node.attributes()
                     .put(attributeName.getLocalPart(), input.getAttributeValue(walk));
               } else {
                 node.attributes()
                     .put(
                         String.format(
                             "%s:%s", attributeName.getPrefix(), attributeName.getLocalPart()),
                         input.getAttributeValue(walk));
               }
             }
           }
           value.setLength(0);
           break;
         case XMLEvent.CHARACTERS:
           value.append(input.getText());
           break;
         case XMLEvent.CDATA:
           value.append(String.format("<![CDATA[%s]]>", input.getText()));
           break;
         case XMLEvent.END_ELEMENT:
           if (node == null) throw new RuntimeException("Node cannot be null");
           String valueString = value.toString().trim();
           value.setLength(0);
           if (valueString.length() > 0) node.setNodeValue(valueString);
           node = node.parent();
           break;
         case XMLEvent.END_DOCUMENT:
           {
             break;
           }
       }
       if (!input.hasNext()) {
         break;
       }
       input.next();
     }
     return MetadataRecord.create(rootNode, -1, -1);
   } catch (WstxParsingException e) {
     throw new XMLStreamException("Problem parsing record:\n" + recordContents, e);
   }
 }