Example #1
0
  /** Entry point. */
  public static Model build(
      XSSchemaSet _schemas, JCodeModel codeModel, ErrorReceiver _errorReceiver, Options opts) {
    // set up a ring
    final Ring old = Ring.begin();
    try {
      ErrorReceiverFilter ef = new ErrorReceiverFilter(_errorReceiver);

      Ring.add(XSSchemaSet.class, _schemas);
      Ring.add(codeModel);
      Model model =
          new Model(opts, codeModel, null /*set later*/, opts.classNameAllocator, _schemas);
      Ring.add(model);
      Ring.add(ErrorReceiver.class, ef);
      Ring.add(CodeModelClassFactory.class, new CodeModelClassFactory(ef));

      BGMBuilder builder =
          new BGMBuilder(
              opts.defaultPackage,
              opts.defaultPackage2,
              opts.isExtensionMode(),
              opts.getFieldRendererFactory());
      builder._build();

      if (ef.hadError()) return null;
      else return model;
    } finally {
      Ring.end(old);
    }
  }
  /**
   * @param schemas Schema files to be checked.
   * @param errorHandler detected errors will be reported to this handler.
   * @return true if there was no error, false if there were errors.
   */
  public static boolean check(
      InputSource[] schemas,
      ErrorReceiver errorHandler,
      final EntityResolver entityResolver,
      boolean disableXmlSecurity) {

    ErrorReceiverFilter errorFilter = new ErrorReceiverFilter(errorHandler);
    boolean hadErrors = false;

    SchemaFactory sf = XmlFactory.createSchemaFactory(W3C_XML_SCHEMA_NS_URI, disableXmlSecurity);
    XmlFactory.allowExternalAccess(sf, "all", disableXmlSecurity);
    sf.setErrorHandler(errorFilter);
    if (entityResolver != null) {
      sf.setResourceResolver(
          new LSResourceResolver() {
            public LSInput resolveResource(
                String type,
                String namespaceURI,
                String publicId,
                String systemId,
                String baseURI) {
              try {
                // XSOM passes the namespace URI to the publicID parameter.
                // we do the same here .
                InputSource is = entityResolver.resolveEntity(namespaceURI, systemId);
                if (is == null) return null;
                return new LSInputSAXWrapper(is);
              } catch (SAXException e) {
                // TODO: is this sufficient?
                return null;
              } catch (IOException e) {
                // TODO: is this sufficient?
                return null;
              }
            }
          });
    }

    try {
      XmlFactory.allowExternalDTDAccess(sf, "all", disableXmlSecurity);
      sf.newSchema(getSchemaSource(schemas, entityResolver));
    } catch (SAXException e) {
      // TODO: we haven't thrown exceptions from here before. should we just trap them and return
      // false?
      hadErrors = true;
    } catch (OutOfMemoryError e) {
      errorHandler.warning(null, Messages.format(Messages.WARN_UNABLE_TO_CHECK_CORRECTNESS));
    }

    return !(hadErrors || errorFilter.hadError());
  }