예제 #1
0
  @Override
  public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (processingEnv.getOptions().containsKey(Const.CONFIG_FILE_OPTION.getValue())) {
      String value = processingEnv.getOptions().get(Const.CONFIG_FILE_OPTION.getValue());

      // For multiple config files we are following the format
      // -Aconfig=foo.config:bar.config where : is the pathSeparatorChar
      StringTokenizer st = new StringTokenizer(value, File.pathSeparator);
      if (!st.hasMoreTokens()) {
        errorListener.error(
            null, Messages.OPERAND_MISSING.format(Const.CONFIG_FILE_OPTION.getValue()));
        return true;
      }

      while (st.hasMoreTokens()) {
        File configFile = new File(st.nextToken());
        if (!configFile.exists()) {
          errorListener.error(null, Messages.NON_EXISTENT_FILE.format());
          continue;
        }

        try {
          Collection<TypeElement> rootElements = new ArrayList<TypeElement>();
          filterClass(rootElements, roundEnv.getRootElements());
          ConfigReader configReader =
              new ConfigReader(processingEnv, rootElements, configFile, errorListener);

          Collection<Reference> classesToBeIncluded = configReader.getClassesToBeIncluded();
          J2SJAXBModel model =
              XJC.createJavaCompiler()
                  .bind(
                      classesToBeIncluded,
                      Collections.<QName, Reference>emptyMap(),
                      null,
                      processingEnv);

          SchemaOutputResolver schemaOutputResolver = configReader.getSchemaOutputResolver();

          model.generateSchema(schemaOutputResolver, errorListener);
        } catch (IOException e) {
          errorListener.error(e.getMessage(), e);
        } catch (SAXException e) {
          // the error should have already been reported
        }
      }
    }
    return true;
  }
  /** Performs error check */
  public void errorCheck() {
    ErrorReceiver er = Ring.get(ErrorReceiver.class);
    for (QName n : enumBaseTypes) {
      XSSchemaSet xs = Ring.get(XSSchemaSet.class);
      XSSimpleType st = xs.getSimpleType(n.getNamespaceURI(), n.getLocalPart());
      if (st == null) {
        er.error(loc, Messages.ERR_UNDEFINED_SIMPLE_TYPE.format(n));
        continue;
      }

      if (!SimpleTypeBuilder.canBeMappedToTypeSafeEnum(st)) {
        er.error(loc, Messages.ERR_CANNOT_BE_BOUND_TO_SIMPLETYPE.format(n));
        continue;
      }
    }
  }
  /**
   * @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());
  }