All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.draagon.util.xml.XMLFileReader Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2003 Draagon Software LLC. All Rights Reserved.
 *
 * This software is the proprietary information of Draagon Software LLC.
 * Use is subject to license terms.
 */

package com.draagon.util.xml;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.w3c.dom.Document;

/**
 * Meta Class loader for XML files
 */
public class XMLFileReader
{
  private static Log log = LogFactory.getLog( XMLFileReader.class );

  /**
   * Loads all the classes specified in the Filename
   */
  public static Document loadFromStream( InputStream is )
    throws IOException
  {
    Document d = null;

    // Try the JAXP Parser
    d = getPluginAndTryLoad( is, "com.draagon.util.xml.JAXPXMLPlugin" );
    if ( d != null ) return d;

    // If not Plugin worked, then error out
    throw new IOException( "No valid XML Plugin was found to load XML document" );
  }

  private static Document getPluginAndTryLoad( InputStream is, String className )
    throws IOException
  {
    XMLPlugin plugin = getPlugin( className );
    if ( plugin == null ) return null;

    //ystem.out.println( "TRYING PLUGIN: " + plugin );

    try {
      return plugin.loadFromStream( is );
    }
    catch( IOException e ) {
      throw e;
    }
    catch( Throwable e ) {
      //ystem.out.println( "ERROR: Cannot use [" + className + "]: " + e.getMessage() );
      log.debug( "Error attempting to use plugin [" + className + "]: " + e.getMessage() );
    }

    return null;
  }

  private static XMLPlugin getPlugin( String className )
  {
    try {
      Class c = Class.forName( className );
      return (XMLPlugin) c.newInstance();
    }
    catch( Throwable e ) {
      //ystem.out.println( "ERROR: Cannot load [" + className + "]: " + e.getMessage() );
      log.debug( "Unable to load XML Plugin [" + className + "]: " + e.getMessage() );
      return null;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy