/**
   * This method generates an instance of the translet class that is wrapped inside this Template.
   * The translet instance will later be wrapped inside a Transformer object.
   */
  private Translet getTransletInstance() throws TransformerConfigurationException {
    try {
      if (_name == null) return null;

      if (_class == null) defineTransletClasses();

      // The translet needs to keep a reference to all its auxiliary
      // class to prevent the GC from collecting them
      AbstractTranslet translet = (AbstractTranslet) _class[_transletIndex].newInstance();
      translet.postInitialization();
      translet.setTemplates(this);
      translet.setServicesMechnism(_useServicesMechanism);
      translet.setAllowedProtocols(_accessExternalStylesheet);
      if (_auxClasses != null) {
        translet.setAuxiliaryClasses(_auxClasses);
      }

      return translet;
    } catch (InstantiationException e) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
      throw new TransformerConfigurationException(err.toString());
    } catch (IllegalAccessException e) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
      throw new TransformerConfigurationException(err.toString());
    }
  }
  /**
   * Defines the translet class and auxiliary classes. Returns a reference to the Class object that
   * defines the main class
   */
  private void defineTransletClasses() throws TransformerConfigurationException {

    if (_bytecodes == null) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.NO_TRANSLET_CLASS_ERR);
      throw new TransformerConfigurationException(err.toString());
    }

    TransletClassLoader loader =
        (TransletClassLoader)
            AccessController.doPrivileged(
                new PrivilegedAction() {
                  public Object run() {
                    return new TransletClassLoader(
                        ObjectFactory.findClassLoader(), _tfactory.getExternalExtensionsMap());
                  }
                });

    try {
      final int classCount = _bytecodes.length;
      _class = new Class[classCount];

      if (classCount > 1) {
        _auxClasses = new HashMap<>();
      }

      for (int i = 0; i < classCount; i++) {
        _class[i] = loader.defineClass(_bytecodes[i]);
        final Class superClass = _class[i].getSuperclass();

        // Check if this is the main class
        if (superClass.getName().equals(ABSTRACT_TRANSLET)) {
          _transletIndex = i;
        } else {
          _auxClasses.put(_class[i].getName(), _class[i]);
        }
      }

      if (_transletIndex < 0) {
        ErrorMsg err = new ErrorMsg(ErrorMsg.NO_MAIN_TRANSLET_ERR, _name);
        throw new TransformerConfigurationException(err.toString());
      }
    } catch (ClassFormatError e) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_CLASS_ERR, _name);
      throw new TransformerConfigurationException(err.toString());
    } catch (LinkageError e) {
      ErrorMsg err = new ErrorMsg(ErrorMsg.TRANSLET_OBJECT_ERR, _name);
      throw new TransformerConfigurationException(err.toString());
    }
  }
  /**
   * Overrides the default readObject implementation since we decided it would be cleaner not to
   * serialize the entire tranformer factory. [ ref bugzilla 12317 ] We need to check if the user
   * defined class for URIResolver also implemented Serializable if yes then we need to deserialize
   * the URIResolver Fix for bugzilla bug 22438
   */
  @SuppressWarnings("unchecked")
  private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
      String temp = SecuritySupport.getSystemProperty(DESERIALIZE_TRANSLET);
      if (temp == null || !(temp.length() == 0 || temp.equalsIgnoreCase("true"))) {
        ErrorMsg err = new ErrorMsg(ErrorMsg.DESERIALIZE_TRANSLET_ERR);
        throw new UnsupportedOperationException(err.toString());
      }
    }

    // We have to read serialized fields first.
    ObjectInputStream.GetField gf = is.readFields();
    _name = (String) gf.get("_name", null);
    _bytecodes = (byte[][]) gf.get("_bytecodes", null);
    _class = (Class[]) gf.get("_class", null);
    _transletIndex = gf.get("_transletIndex", -1);

    _outputProperties = (Properties) gf.get("_outputProperties", null);
    _indentNumber = gf.get("_indentNumber", 0);

    if (is.readBoolean()) {
      _uriResolver = (URIResolver) is.readObject();
    }

    _tfactory = new TransformerFactoryImpl();
  }
Beispiel #4
0
  /** Internal interface which returns a DOM for a given DTMManager and translet. */
  protected DOM getDOM(XSLTCDTMManager dtmManager, AbstractTranslet translet) throws SAXException {
    SAXImpl idom = (SAXImpl) _dom.get();

    if (idom != null) {
      if (dtmManager != null) {
        idom.migrateTo(dtmManager);
      }
    } else {
      Source source = _source;
      if (source == null) {
        if (_systemId != null && _systemId.length() > 0) {
          source = new StreamSource(_systemId);
        } else {
          ErrorMsg err = new ErrorMsg(ErrorMsg.XSLTC_SOURCE_ERR);
          throw new SAXException(err.toString());
        }
      }

      DOMWSFilter wsfilter = null;
      if (translet != null && translet instanceof StripFilter) {
        wsfilter = new DOMWSFilter(translet);
      }

      boolean hasIdCall = (translet != null) ? translet.hasIdCall() : false;

      if (dtmManager == null) {
        dtmManager = XSLTCDTMManager.newInstance();
      }

      idom = (SAXImpl) dtmManager.getDTM(source, true, wsfilter, false, false, hasIdCall);

      String systemId = getSystemId();
      if (systemId != null) {
        idom.setDocumentURI(systemId);
      }
      _dom.set(idom);
    }
    return idom;
  }
 /*     */ private void doTransform() {
   /*     */ try {
     /* 101 */ Class clazz = ObjectFactory.findProviderClass(this._className, true);
     /* 102 */ AbstractTranslet translet = (AbstractTranslet) clazz.newInstance();
     /* 103 */ translet.postInitialization();
     /*     */
     /* 106 */ SAXParserFactory factory = SAXParserFactory.newInstance();
     /*     */ try {
       /* 108 */ factory.setFeature("http://xml.org/sax/features/namespaces", true);
       /*     */ }
     /*     */ catch (Exception e) {
       /* 111 */ factory.setNamespaceAware(true);
       /*     */ }
     /* 113 */ SAXParser parser = factory.newSAXParser();
     /* 114 */ XMLReader reader = parser.getXMLReader();
     /*     */
     /* 117 */ XSLTCDTMManager dtmManager =
         (XSLTCDTMManager) XSLTCDTMManager.getDTMManagerClass().newInstance();
     /*     */ DTMWSFilter wsfilter;
     /*     */ DTMWSFilter wsfilter;
     /* 122 */ if ((translet != null) && ((translet instanceof StripFilter)))
       /* 123 */ wsfilter = new DOMWSFilter(translet);
     /*     */ else {
       /* 125 */ wsfilter = null;
       /*     */ }
     /*     */
     /* 128 */ DOMEnhancedForDTM dom =
         (DOMEnhancedForDTM)
             dtmManager.getDTM(
                 new SAXSource(reader, new InputSource(this._fileName)),
                 false,
                 wsfilter,
                 true,
                 false,
                 translet.hasIdCall());
     /*     */
     /* 133 */ dom.setDocumentURI(this._fileName);
     /* 134 */ translet.prepassDocument(dom);
     /*     */
     /* 137 */ int n = this._params.size();
     /* 138 */ for (int i = 0; i < n; i++) {
       /* 139 */ Parameter param = (Parameter) this._params.elementAt(i);
       /* 140 */ translet.addParameter(param._name, param._value);
       /*     */ }
     /*     */
     /* 144 */ TransletOutputHandlerFactory tohFactory =
         TransletOutputHandlerFactory.newInstance();
     /*     */
     /* 146 */ tohFactory.setOutputType(0);
     /* 147 */ tohFactory.setEncoding(translet._encoding);
     /* 148 */ tohFactory.setOutputMethod(translet._method);
     /*     */
     /* 150 */ if (this._iterations == -1) {
       /* 151 */ translet.transform(dom, tohFactory.getSerializationHandler());
       /*     */ }
     /* 153 */ else if (this._iterations > 0) {
       /* 154 */ long mm = System.currentTimeMillis();
       /* 155 */ for (int i = 0; i < this._iterations; i++) {
         /* 156 */ translet.transform(dom, tohFactory.getSerializationHandler());
         /*     */ }
       /*     */
       /* 159 */ mm = System.currentTimeMillis() - mm;
       /*     */
       /* 161 */ System.err.println("\n<!--");
       /* 162 */ System.err.println("  transform  = " + mm / this._iterations + " ms");
       /*     */
       /* 165 */ System.err.println(
           "  throughput = " + 1000.0D / (mm / this._iterations) + " tps");
       /*     */
       /* 169 */ System.err.println("-->");
       /*     */ }
     /*     */ }
   /*     */ catch (TransletException e) {
     /* 173 */ if (this._debug) e.printStackTrace();
     /* 174 */ System.err.println(new ErrorMsg("RUNTIME_ERROR_KEY") + e.getMessage());
     /*     */ }
   /*     */ catch (RuntimeException e)
   /*     */ {
     /* 178 */ if (this._debug) e.printStackTrace();
     /* 179 */ System.err.println(new ErrorMsg("RUNTIME_ERROR_KEY") + e.getMessage());
     /*     */ }
   /*     */ catch (FileNotFoundException e)
   /*     */ {
     /* 183 */ if (this._debug) e.printStackTrace();
     /* 184 */ ErrorMsg err = new ErrorMsg("FILE_NOT_FOUND_ERR", this._fileName);
     /* 185 */ System.err.println(new ErrorMsg("RUNTIME_ERROR_KEY") + err.toString());
     /*     */ }
   /*     */ catch (MalformedURLException e)
   /*     */ {
     /* 189 */ if (this._debug) e.printStackTrace();
     /* 190 */ ErrorMsg err = new ErrorMsg("INVALID_URI_ERR", this._fileName);
     /* 191 */ System.err.println(new ErrorMsg("RUNTIME_ERROR_KEY") + err.toString());
     /*     */ }
   /*     */ catch (ClassNotFoundException e)
   /*     */ {
     /* 195 */ if (this._debug) e.printStackTrace();
     /* 196 */ ErrorMsg err = new ErrorMsg("CLASS_NOT_FOUND_ERR", this._className);
     /* 197 */ System.err.println(new ErrorMsg("RUNTIME_ERROR_KEY") + err.toString());
     /*     */ }
   /*     */ catch (UnknownHostException e)
   /*     */ {
     /* 201 */ if (this._debug) e.printStackTrace();
     /* 202 */ ErrorMsg err = new ErrorMsg("INVALID_URI_ERR", this._fileName);
     /* 203 */ System.err.println(new ErrorMsg("RUNTIME_ERROR_KEY") + err.toString());
     /*     */ }
   /*     */ catch (SAXException e)
   /*     */ {
     /* 207 */ Exception ex = e.getException();
     /* 208 */ if (this._debug) {
       /* 209 */ if (ex != null) ex.printStackTrace();
       /* 210 */ e.printStackTrace();
       /*     */ }
     /* 212 */ System.err.print(new ErrorMsg("RUNTIME_ERROR_KEY"));
     /* 213 */ if (ex != null) /* 214 */ System.err.println(ex.getMessage());
     /*     */ else /* 216 */ System.err.println(e.getMessage());
     /*     */ }
   /*     */ catch (Exception e) {
     /* 219 */ if (this._debug) e.printStackTrace();
     /* 220 */ System.err.println(new ErrorMsg("RUNTIME_ERROR_KEY") + e.getMessage());
     /*     */ }
   /*     */ }