public Element parse(URLConnection uc, Node instruction,
          Document response_doc) throws Exception {
	// Fortify Mod: To prevent external entity injections
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    Transformer idt = tf.newTransformer();
	// End Fortify Mod
    Element parser_e = response_doc.createElement("parser");
    Element response_e = response_doc.createElement("response");
    Element content_e = response_doc.createElement("content");
    if (instruction == null) {
      InputStream is = null;
      uc.connect();
      String contentType = uc.getContentType();
      try {
        is = URLConnectionUtils.getInputStream(uc);
        if (contentType != null && contentType.contains("xml")) { // a crude check
          idt.transform(new StreamSource(is),
                  new DOMResult(content_e));
        } else {
          content_e.setTextContent(IOUtils.inputStreamToString(is));
        }
      } finally {
                if (null != is)
          is.close();
        }
    } else {
      Element instruction_e;
      if (instruction instanceof Element) {
        instruction_e = (Element) instruction;
      } else {
        instruction_e = ((Document) instruction).getDocumentElement();
      }
      String key = "{" + instruction_e.getNamespaceURI() + "}"
              + instruction_e.getLocalName();
      ParserEntry pe = index.getParser(key);
      Object instance = null;

      // Instantiate the parser object if requested.
      if (pe.isInitialized()) {
        instance = parserInstances.get(key);
        if (instance == null) {
          try {
            TEClassLoader cl = engine.getClassLoader(opts
                    .getSourcesName());
            instance = Misc.makeInstance(pe.getClassName(),
                    pe.getClassParams(), cl);
          } catch (Exception e) {
            throw new Exception("Can't instantiate parser "
                    + pe.getName(), e);
          }
          parserInstances.put(key, instance);
        }
      }

      Method method = parserMethods.get(key);
      if (method == null) {
        TEClassLoader cl = engine.getClassLoader(opts.getSourcesName());
        method = Misc.getMethod(pe.getClassName(), pe.getMethod(), cl,
                3, 4);
        parserMethods.put(key, method);
      }
      StringWriter swLogger = new StringWriter();
      PrintWriter pwLogger = new PrintWriter(swLogger);
      int arg_count = method.getParameterTypes().length;
      Object[] args = new Object[arg_count];
      args[0] = uc;
      args[1] = instruction_e;
      args[2] = pwLogger;
      if (arg_count > 3) {
        args[3] = this;
      }
      Object return_object;
      try {
        if (LOGR.isLoggable(Level.FINER)) {
          LOGR.finer("Invoking method " + method.toGenericString()
                  + "size args[] = " + args.length + "\n args[0]: "
                  + args[0].toString() + "\n args[1]:\n"
                  + DomUtils.serializeNode((Node) args[1]));
        }
        return_object = method.invoke(instance, args);
      } catch (java.lang.reflect.InvocationTargetException e) {
        Throwable cause = e.getCause();
        String msg = "Error invoking parser " + pe.getId() + "\n"
                + cause.getClass().getName();
        if (cause.getMessage() != null) {
          msg += ": " + cause.getMessage();
        }
        jlogger.log( SEVERE, msg, e);
        // CTL says parsers should return null if they encounter an error.
        // Apparently this parser is broken. Wrap the thrown exception in a
        // RuntimeException since we really know nothing about what went wrong.
        throw new RuntimeException(
                "Parser " + pe.getId() + " threw an exception.", cause);
      }
      pwLogger.close();
      if (return_object instanceof Node) {
        idt.transform(new DOMSource((Node) return_object),
                new DOMResult(content_e));
      } else if (return_object != null) {
        content_e.appendChild(response_doc.createTextNode(return_object
                .toString()));
      }
      parser_e.setAttribute("prefix", instruction_e.getPrefix());
      parser_e.setAttribute("local-name", instruction_e.getLocalName());
      parser_e.setAttribute("namespace-uri",
              instruction_e.getNamespaceURI());
      parser_e.setTextContent(swLogger.toString());
    }
    response_e.appendChild(parser_e);
    response_e.appendChild(content_e);
    return response_e;
  }
public Element parse ( URLConnection uc , Node instruction , Document response_doc ) throws Exception { TransformerFactory tf = TransformerFactory . newInstance ( ) ; tf . setFeature ( XMLConstants . FEATURE_SECURE_PROCESSING , true ) ; Transformer idt = tf . newTransformer ( ) ; Element parser_e = response_doc . createElement ( "parser" ) ; Element response_e = response_doc . createElement ( "response" ) ; Element content_e = response_doc . createElement ( "content" ) ; if ( instruction == null ) { InputStream is = null ; uc . connect ( ) ; String contentType = uc . getContentType ( ) ; try { is = URLConnectionUtils . getInputStream ( uc ) ; if ( contentType != null && contentType . contains ( "xml" ) ) { idt . transform ( new StreamSource ( is ) , new DOMResult ( content_e ) ) ; } else { content_e . setTextContent ( IOUtils . inputStreamToString ( is ) ) ; } } finally { if ( null != is ) is . close ( ) ; } } else { Element instruction_e ; if ( instruction instanceof Element ) { instruction_e = ( Element ) instruction ; } else { instruction_e = ( ( Document ) instruction ) . getDocumentElement ( ) ; } String key = "{" + instruction_e . getNamespaceURI ( ) + "}" + instruction_e . getLocalName ( ) ; ParserEntry pe = index . getParser ( key ) ; Object instance = null ; if ( pe . isInitialized ( ) ) { instance = parserInstances . get ( key ) ; if ( instance == null ) { try { TEClassLoader cl = engine . getClassLoader ( opts . getSourcesName ( ) ) ; instance = Misc . makeInstance ( pe . getClassName ( ) , pe . getClassParams ( ) , cl ) ; } catch ( Exception e ) { throw new Exception ( "Can't instantiate parser " + pe . getName ( ) , e ) ; } parserInstances . put ( key , instance ) ; } } Method method = parserMethods . get ( key ) ; if ( method == null ) { TEClassLoader cl = engine . getClassLoader ( opts . getSourcesName ( ) ) ; method = Misc . getMethod ( pe . getClassName ( ) , pe . getMethod ( ) , cl , 3 , 4 ) ; parserMethods . put ( key , method ) ; } StringWriter swLogger = new StringWriter ( ) ; PrintWriter pwLogger = new PrintWriter ( swLogger ) ; int arg_count = method . getParameterTypes ( ) . length ; Object [ ] args = new Object [ arg_count ] ; args [ 0 ] = uc ; args [ 1 ] = instruction_e ; args [ 2 ] = pwLogger ; if ( arg_count > 3 ) { args [ 3 ] = this ; } Object return_object ; try { if ( LOGR . isLoggable ( Level . FINER ) ) { LOGR . finer ( "Invoking method " + method . toGenericString ( ) + "size args[] = " + args . length + "\n args[0]: " + args [ 0 ] . toString ( ) + "\n args[1]:\n" + DomUtils . serializeNode ( ( Node ) args [ 1 ] ) ) ; } return_object = method . invoke ( instance , args ) ; } catch ( java . lang . reflect . InvocationTargetException e ) { Throwable cause = e . getCause ( ) ; String msg = "Error invoking parser " + pe . getId ( ) + "\n" + cause . getClass ( ) . getName ( ) ; if ( cause . getMessage ( ) != null ) { msg += ": " + cause . getMessage ( ) ; } jlogger . log ( SEVERE , msg , e ) ; throw new RuntimeException ( "Parser " + pe . getId ( ) + " threw an exception." , cause ) ; } pwLogger . close ( ) ; if ( return_object instanceof Node ) { idt . transform ( new DOMSource ( ( Node ) return_object ) , new DOMResult ( content_e ) ) ; } else if ( return_object != null ) { content_e . appendChild ( response_doc . createTextNode ( return_object . toString ( ) ) ) ; } parser_e . setAttribute ( "prefix" , instruction_e . getPrefix ( ) ) ; parser_e . setAttribute ( "local-name" , instruction_e . getLocalName ( ) ) ; parser_e . setAttribute ( "namespace-uri" , instruction_e . getNamespaceURI ( ) ) ; parser_e . setTextContent ( swLogger . toString ( ) ) ; } response_e . appendChild ( parser_e ) ; response_e . appendChild ( content_e ) ; return response_e ; }
Invoke a parser or chain of parsers as specified by instruction element
and children. Parsers in chain share uc, strip off their own
instructions, and pass child instructions to next parser in chain. Final
parser in chain modifies content. All parsers in chain can return info in
attributes and child elements of instructions. If parser specified in
instruction, call it to return specified info from uc.