예제 #1
0
 public boolean getFeature(String name)
     throws SAXNotRecognizedException, SAXNotSupportedException {
   if (name == null) {
     throw new NullPointerException();
   }
   try {
     return fComponentManager.getFeature(name);
   } catch (XMLConfigurationException e) {
     final String identifier = e.getIdentifier();
     final String key =
         e.getType() == XMLConfigurationException.NOT_RECOGNIZED
             ? "feature-not-recognized"
             : "feature-not-supported";
     throw new SAXNotRecognizedException(
         SAXMessageFormatter.formatMessage(Locale.getDefault(), key, new Object[] {identifier}));
   }
 }
  public void validate(Source source, Result result) throws SAXException, IOException {

    if (result == null || result instanceof StAXResult) {

      if (identityTransformer1 == null) {
        try {
          SAXTransformerFactory tf =
              fComponentManager.getFeature(Constants.ORACLE_FEATURE_SERVICE_MECHANISM)
                  ? (SAXTransformerFactory) SAXTransformerFactory.newInstance()
                  : (SAXTransformerFactory)
                      TransformerFactory.newInstance(
                          DEFAULT_TRANSFORMER_IMPL, StAXValidatorHelper.class.getClassLoader());
          identityTransformer1 = tf.newTransformer();
          identityTransformer2 = tf.newTransformerHandler();
        } catch (TransformerConfigurationException e) {
          // this is impossible, but again better safe than sorry
          throw new TransformerFactoryConfigurationError(e);
        }
      }

      handler = new ValidatorHandlerImpl(fComponentManager);
      if (result != null) {
        handler.setContentHandler(identityTransformer2);
        identityTransformer2.setResult(result);
      }

      try {
        identityTransformer1.transform(source, new SAXResult(handler));
      } catch (TransformerException e) {
        if (e.getException() instanceof SAXException) throw (SAXException) e.getException();
        throw new SAXException(e);
      } finally {
        handler.setContentHandler(null);
      }
      return;
    }
    throw new IllegalArgumentException(
        JAXPValidationMessageFormatter.formatMessage(
            Locale.getDefault(),
            "SourceResultMismatch",
            new Object[] {source.getClass().getName(), result.getClass().getName()}));
  }