Ejemplo n.º 1
0
  /*
   * Analyze the schema at the given URI, the schema is parsed and stored in
   * XMLObjectInfos.
   */
  private void analyzeXSD(URI uri) {

    uri = uri.normalize();

    // already seen this xsd, skip it
    if (xmlObjectInfos.containsKey(uri.toString())) return;

    XSDSchema xsdSchema = XSDSchemaImpl.getSchemaForSchema(uri.toString());

    String encoding = null;
    // if schema is not cached, parse it
    if (xsdSchema == null) {
      XSDParser p = new XSDParser(null);
      InputStream is = NetUtils.getURLInputStream(uri.toString());

      if (is != null) {
        p.parse(is);
        xsdSchema = p.getSchema();
        encoding = p.getEncoding();
      }
    } else encoding = xsdSchema.getDocument().getXmlEncoding();

    if (xsdSchema != null) {

      if (encoding == null) encoding = "UTF-8";

      XMLObjectInfo info = new XMLObjectInfo(new Path(uri.getPath()), xsdSchema, encoding);
      xmlObjectInfos.put(uri.toString(), info);
      updatePathPrefix(info);

      // analyze its imports and includes
      analyzeXSD(uri, xsdSchema);
    }
  }
Ejemplo n.º 2
0
  @Override
  public void read(InputSource source, Node outputNode) throws Exception {
    logger.debug("Processing XSD '{0}'", outputNode);
    Reader reader = null;
    InputStream stream = null;
    try {
      // Parse the XSD, measuring the number of bytes as we read ...
      Map<?, ?> options = new HashMap<Object, Object>();
      XSDParser parser = new XSDParser(options);
      AtomicLong contentSize = new AtomicLong();
      if (source.getCharacterStream() != null) {
        reader = new SizeMeasuringReader(source.getCharacterStream(), contentSize);
        source = new InputSource(reader);
      } else {
        stream = new SizeMeasuringInputStream(source.getByteStream(), contentSize);
        source = new InputSource(stream);
      }
      parser.parse(source);

      // Get some metadata about the XSD ...
      String encoding = parser.getEncoding();

      // Convert the XSD to content ...
      XSDSchema schema = parser.getSchema();
      process(schema, encoding, contentSize.get(), outputNode);

    } finally {
      try {
        if (reader != null) reader.close();
      } catch (Exception e) {
        logger.debug(e, "Cannot close reader stream ");
      } finally {
        try {
          if (stream != null) stream.close();
        } catch (Exception e) {
          logger.debug(e, "Cannot close reader stream ");
        }
      }
    }
  }