1 /*
   2  * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.org.apache.xerces.internal.jaxp.validation;
  27 
  28 import com.sun.org.apache.xerces.internal.impl.Constants;
  29 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager;
  30 import java.io.IOException;
  31 
  32 import javax.xml.transform.Result;
  33 import javax.xml.transform.Source;
  34 import javax.xml.transform.Transformer;
  35 import javax.xml.transform.TransformerConfigurationException;
  36 import javax.xml.transform.TransformerException;
  37 import javax.xml.transform.TransformerFactory;
  38 import javax.xml.transform.TransformerFactoryConfigurationError;
  39 import javax.xml.transform.sax.SAXResult;
  40 import javax.xml.transform.sax.SAXTransformerFactory;
  41 import javax.xml.transform.sax.TransformerHandler;
  42 import javax.xml.transform.stax.StAXResult;
  43 import javax.xml.transform.stax.StAXSource;
  44 
  45 import org.xml.sax.SAXException;
  46 
  47 /**
  48  * <p>A validator helper for <code>StAXSource</code>s.</p>
  49  *
  50  * @author Sunitha Reddy
  51  */
  52 public final class StAXValidatorHelper implements ValidatorHelper {
  53     private static final String DEFAULT_TRANSFORMER_IMPL = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
  54 
  55     /** Component manager. **/
  56     private XMLSchemaValidatorComponentManager fComponentManager;
  57 
  58     private Transformer identityTransformer1 = null;
  59     private TransformerHandler identityTransformer2 = null;
  60     private ValidatorHandlerImpl handler = null;
  61 
  62     /** Creates a new instance of StaxValidatorHelper */
  63     public StAXValidatorHelper(XMLSchemaValidatorComponentManager componentManager) {
  64         fComponentManager = componentManager;
  65     }
  66 
  67     public void validate(Source source, Result result)
  68         throws SAXException, IOException {
  69 
  70         if (result == null || result instanceof StAXResult) {
  71 
  72             if( identityTransformer1==null ) {
  73                 try {
  74                     SAXTransformerFactory tf = fComponentManager.getFeature(Constants.ORACLE_FEATURE_SERVICE_MECHANISM) ?
  75                                     (SAXTransformerFactory)SAXTransformerFactory.newInstance()
  76                                     : (SAXTransformerFactory) TransformerFactory.newInstance(DEFAULT_TRANSFORMER_IMPL, StAXValidatorHelper.class.getClassLoader());
  77                     XMLSecurityManager securityManager = (XMLSecurityManager)fComponentManager.getProperty(Constants.SECURITY_MANAGER);
  78                     if (securityManager != null) {
  79                         for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
  80                             if (securityManager.isSet(limit.ordinal())){
  81                                 tf.setAttribute(limit.apiProperty(),
  82                                         securityManager.getLimitValueAsString(limit));
  83                             }
  84                         }
  85                         if (securityManager.printEntityCountInfo()) {
  86                             tf.setAttribute(Constants.JDK_ENTITY_COUNT_INFO, "yes");
  87                         }
  88                     }
  89 
  90                     identityTransformer1 = tf.newTransformer();
  91                     identityTransformer2 = tf.newTransformerHandler();
  92                 } catch (TransformerConfigurationException e) {
  93                     // this is impossible, but again better safe than sorry
  94                     throw new TransformerFactoryConfigurationError(e);
  95                 }
  96             }
  97 
  98             handler = new ValidatorHandlerImpl(fComponentManager);
  99             if( result!=null ) {
 100                 handler.setContentHandler(identityTransformer2);
 101                 identityTransformer2.setResult(result);
 102             }
 103 
 104             try {
 105                 identityTransformer1.transform( source, new SAXResult(handler) );
 106             } catch (TransformerException e) {
 107                 if( e.getException() instanceof SAXException )
 108                     throw (SAXException)e.getException();
 109                 throw new SAXException(e);
 110             } finally {
 111                 handler.setContentHandler(null);
 112             }
 113             return;
 114         }
 115         throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(fComponentManager.getLocale(),
 116                 "SourceResultMismatch",
 117                 new Object [] {source.getClass().getName(), result.getClass().getName()}));
 118     }
 119 }