Beispiel #1
0
 public Factory(Map<String, String> namespaces) {
   this.namespaces = namespaces;
   inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
   inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
   inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
   inputFactory.configureForSpeed();
 }
Beispiel #2
0
  private void processAliasFile(File aliasesFile, Set<String> validClasses)
      throws ContentFileProcessingException, XMLStreamException, FileNotFoundException, IOException,
          NamespaceException {
    XMLStreamReader2 streamReader = null;

    try (FileReader fileReader = new FileReader(aliasesFile)) {
      streamReader = (XMLStreamReader2) inputFactory.createXMLStreamReader(fileReader);
      AliasProcessor aliasProcessor =
          new AliasProcessor(this, streamReader, aliasesFile, validClasses);

      if (aliasesFile.getName().equals("aliasDefinitions.xml")) {
        streamReader.validateAgainst(aliasDefinitionsSchema);
        aliasProcessor.processAliasDefinitionsFile();
      } else {
        streamReader.validateAgainst(aliasSchema);
        aliasProcessor.processAliasesFile();
      }
    } catch (WstxValidationException e) {
      Location location = e.getLocation();

      throw new ContentFileProcessingException(
          aliasesFile, location.getLineNumber(), location.getColumnNumber(), e.getMessage());
    } finally {
      streamReader.close();
    }
  }
Beispiel #3
0
  protected XMLInputFactory2 getFactory() {
    XMLInputFactory2 f = super.getFactory();

    f.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.TRUE);
    f.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.TRUE);

    // Usually we don't care about full stress testing...
    if (f.isPropertySupported(WstxInputProperties.P_INPUT_BUFFER_LENGTH)) {
      f.setProperty(WstxInputProperties.P_INPUT_BUFFER_LENGTH, new Integer(2000));
    }

    // Do we want to handle validation problems gracefully?
    // f.setProperty(XMLInputFactory.REPORTER, new TestReporter());

    return f;
  }
 private SMInputFactory initStax() {
   XMLInputFactory xmlFactory = XMLInputFactory2.newInstance();
   xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
   xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
   xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
   xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
   return new SMInputFactory(xmlFactory);
 }
Beispiel #5
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);
   }
 }
 /**
  * Creates a {@link org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader} from a
  * StringBuilder.
  *
  * @param input The XML to read and modify.
  * @return The {@link org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader}.
  */
 protected final ModifiedPomXMLEventReader newModifiedPomXER(StringBuilder input) {
   ModifiedPomXMLEventReader newPom = null;
   try {
     XMLInputFactory inputFactory = XMLInputFactory2.newInstance();
     inputFactory.setProperty(XMLInputFactory2.P_PRESERVE_LOCATION, Boolean.TRUE);
     newPom = new ModifiedPomXMLEventReader(input, inputFactory);
   } catch (XMLStreamException e) {
     getLog().error(e);
   }
   return newPom;
 }
  /**
   * 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 class OAIPMHParser extends ElementParser<OAIPMHtype> {
  private static final String NAME = "OAI-PMH";
  private static final String RESPONSE_DATE = "responseDate";
  private static XMLInputFactory factory = XMLInputFactory2.newInstance();

  private static void untilFirstStartElement(XMLEventReader reader) throws XMLStreamException {
    while (reader.peek() != null
        && !reader.peek().isStartElement()
        && !reader.peek().isEndDocument()) reader.nextEvent();
  }

  public static OAIPMHtype parse(
      InputStream instream,
      OAIServiceConfiguration<MetadataParser, AboutItemParser, DescriptionParser, AboutSetParser>
          oaiServiceConfiguration)
      throws ParseException {
    XMLEventReader stream;
    try {
      stream = factory.createXMLEventReader(instream);
      untilFirstStartElement(stream);
    } catch (XMLStreamException e) {
      throw new ParseException(e);
    }
    OAIPMHParser parser = new OAIPMHParser(oaiServiceConfiguration);
    return parser.parse(stream);
  }

  public static OAIPMHtype parse(
      File f,
      OAIServiceConfiguration<MetadataParser, AboutItemParser, DescriptionParser, AboutSetParser>
          oaiServiceConfiguration)
      throws FileNotFoundException, ParseException {
    try {
      XMLEventReader stream = factory.createXMLEventReader(new FileInputStream(f));
      untilFirstStartElement(stream);
      OAIPMHParser parser = new OAIPMHParser(oaiServiceConfiguration);
      return parser.parse(stream);
    } catch (XMLStreamException e) {
      throw new ParseException(e);
    }
  }

  public static OAIPMHtype parse(
      String filepath,
      OAIServiceConfiguration<MetadataParser, AboutItemParser, DescriptionParser, AboutSetParser>
          oaiServiceConfiguration)
      throws FileNotFoundException, XMLStreamException, ParseException {
    try {
      XMLEventReader stream = factory.createXMLEventReader(new FileInputStream(filepath));
      untilFirstStartElement(stream);
      OAIPMHParser parser = new OAIPMHParser(oaiServiceConfiguration);
      return parser.parse(stream);
    } catch (XMLStreamException e) {
      throw new ParseException(e);
    }
  }

  private RequestParser reqParser;
  private ListRecordsParser listRecordsParser;
  private GetRecordParser getRecordParser;
  private ListIdentifiersParser listIdentifiersParser;
  private ListSetsParser listSetsParser;
  private ListMetadataFormatsParser listMetadataFormatsParser;
  private IdentifyParser identifyParser;
  private ErrorParser errorParser;

  public OAIPMHParser(
      OAIServiceConfiguration<MetadataParser, AboutItemParser, DescriptionParser, AboutSetParser>
          oaiServiceConfiguration) {
    super(oaiServiceConfiguration);
    reqParser = new RequestParser(oaiServiceConfiguration);
    listRecordsParser = new ListRecordsParser(oaiServiceConfiguration);
    getRecordParser = new GetRecordParser(oaiServiceConfiguration);
    listIdentifiersParser = new ListIdentifiersParser(oaiServiceConfiguration);
    listSetsParser = new ListSetsParser(oaiServiceConfiguration);
    listMetadataFormatsParser = new ListMetadataFormatsParser(oaiServiceConfiguration);
    identifyParser = new IdentifyParser(oaiServiceConfiguration);
    errorParser = new ErrorParser(oaiServiceConfiguration);
  }

  @Override
  protected OAIPMHtype parseElement(XMLEventReader reader) throws ParseException {
    OAIPMHtype pmh = new OAIPMHtype();

    try {
      if (!reader.peek().asStartElement().getName().getLocalPart().equals(NAME))
        throw new ParseException("Expecting " + NAME + " element");

      reader.nextEvent();
      this.nextElement(reader);

      pmh.setResponseDate(
          super.getConfiguration().getFormatter().parse(this.getElement(reader, RESPONSE_DATE)));
      pmh.setRequest(reqParser.parse(reader));

      String name = reader.peek().asStartElement().getName().getLocalPart();

      if (name.equals("ListRecords")) pmh.setListRecords(listRecordsParser.parse(reader));
      else if (name.equals("GetRecord")) pmh.setGetRecord(getRecordParser.parse(reader));
      else if (name.equals("ListIdentifiers"))
        pmh.setListIdentifiers(listIdentifiersParser.parse(reader));
      else if (name.equals("ListSets")) pmh.setListSets(listSetsParser.parse(reader));
      else if (name.equals("ListMetadataFormats"))
        pmh.setListMetadataFormats(listMetadataFormatsParser.parse(reader));
      else if (name.equals("Identify")) pmh.setIdentify(identifyParser.parse(reader));
      else {
        while (reader.peek().asStartElement().getName().getLocalPart().equals("error")) {
          pmh.getError().add(errorParser.parse(reader));
        }
      }

    } catch (XMLStreamException e) {
      throw new ParseException(e);
    } catch (java.text.ParseException e) {
      throw new ParseException(e);
    }

    return pmh;
  }
}
Beispiel #9
0
  public static class Factory {
    private XMLInputFactory2 inputFactory = (XMLInputFactory2) XMLInputFactory2.newInstance();
    private Map<String, String> namespaces;

    public Factory(Map<String, String> namespaces) {
      this.namespaces = namespaces;
      inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
      inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
      inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
      inputFactory.configureForSpeed();
    }

    public MetadataRecord fromGroovyNode(GroovyNode rootNode, int recordNumber, int recordCount) {
      return new MetadataRecord(rootNode, recordNumber, recordCount);
    }

    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);
      }
    }

    private String createCompleteRecordString(String xmlRecord) {
      StringBuilder out = new StringBuilder("<?xml version=\"1.0\"?>\n");
      out.append("<record");
      for (Map.Entry<String, String> namespace : namespaces.entrySet()) {
        out.append(String.format(" xmlns:%s=\"%s\"", namespace.getKey(), namespace.getValue()));
      }
      out.append(">");
      out.append(xmlRecord);
      out.append("</record>");
      return out.toString();
    }

    public static String sanitize(String dirty) {
      if (dirty.contains("\n") || dirty.contains("\r")) {
        StringBuilder out = new StringBuilder(dirty.length());
        for (char c : dirty.toCharArray()) {
          out.append(Character.isWhitespace(c) ? ' ' : c);
        }
        return out.toString().replaceAll(" +", " ").trim();
      } else {
        return dirty;
      }
    }
  }