//
// XMLParse.java
//
// The contents of this file are subject to the SNIA Public License Version 1.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
//	http://www.snia.org/resources/openSource.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the 
// License for the specific language governing rights and limitations
// under the License.
//
// The Original Code is XMLParse.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s):  Adrian Schuur
//
//
// This source uses the Solaris WBEM APIs version 2.2. 
// Synchronization will be maintained with the Solaris WBEM APIS,
// which are being standardized through the Java Community Process.
//
//
// $Id: XMLParse.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// Attn: 08/7/2002 ( Bapu )
//    Convert all Log to Log
//
// $Log: XMLParse.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:17:56  karl
// import of SNIA 0_7
//
// Revision 1.8  2001/07/22 19:46:40  davidsi
// - From Adrian Schuur. Changed XMLParse(InputStream) to a public method
//   for Indication support
// - added XMLParse(Reader) method
//
// Revision 1.7  2001/06/23 10:44:02  davidsi
// - From HP.  Changed access level for XMLParse (InputStream) to public
//
// Revision 1.6  2000/08/16 06:00:00  shawn
// merge P_TOMCAT31 branch
//

// Revision 1.5.8.1  2000/08/10 00:03:36  shawn
// head merge
//
// Revision 1.5  2000/05/11 10:11:25  davidsi
// - Fixed tabs.
//
// Revision 1.4  2000/05/06 17:20:04  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.3  2000/04/10 23:33:41  davidsi
// - Added support for using new xerces based classes (those from
//   xml4j had been deprecated).
//
// Revision 1.2  2000/02/13 15:10:07  davidsi
// - Added text to cover re-use of Solaris WBEM APIs.
//
// Revision 1.1.1.1  2000/02/06 16:41:02  davidsi
// Initial version.
//
//
package org.snia.wbemcmd.xml;

import java.util.Vector;
import java.io.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
import org.snia.wbemcmd.utils.*;
import org.snia.wbem.cim.*;
import org.snia.wbemcfg.GlobalConfig;
import org.w3c.dom.Node;



public class XMLParse extends DefaultHandler {

    private static final String 
	parserName = "org.apache.xerces.parsers.SAXParser";
    XMLReader parser;
    XMLNode currentNode=null;
    XMLNode topNode=null;
    Vector nodeStack=new Vector();
    InputSource inputStream;
    
    public void startDocument() {
    } 
    

    public void startElement(String uri, String local, String name, Attributes attrs) {
	currentNode=new XMLNode(name,attrs);
	if (topNode==null)
	    topNode=currentNode;
	
	//
	// If there is at least 1 element on the stack, see if
	// we need to set up the child ptr
	//
	if (nodeStack.size()>0) {
	    XMLNode parent=(XMLNode)(nodeStack.elementAt(nodeStack.size()-1));
	    parent.setChild(currentNode);
	}
	
	nodeStack.addElement(currentNode);
	if (GlobalConfig.DEBUG_XMLDECODE)
	    Log.println("processing "+name);
    }

    public void endElement(String uri, String local, String name) {
	if (nodeStack.size()>0)
	    nodeStack.removeElementAt(nodeStack.size()-1);
	if (GlobalConfig.DEBUG_XMLDECODE)
	    Log.println("finished "+name);
    }

    public void characters(char ch[], int start, int length) {
	String line = new String( ch, start,length );

	//Added by Prabhakar .P (prabhakar@hp.com)
	//Added to make it interoperable with JWS
	//begin
	if(line.trim().length()<=0) return;
	//end

	if (length>0) {
	    
	    //
	    // If just a CR ignore it
	    //
	    if (ch[start]=='\n' && length==1)
		return;
	    
	    String text=currentNode.getText();
	    if (text==null)
		currentNode.assignText(line);
	    else
		currentNode.assignText(text.concat(line));
	}
    } 
    
    public void ignorableWhitespace(char ch[], int start, int length) {
    }
    
    
    //
    // ErrorHandlers
    //
    private String errorInfo(SAXParseException ex) {
	return "Line:"+ex.getLineNumber()+"("+ex.getColumnNumber()+")";
    } 

    public void warning(SAXParseException ex) {
	Log.println("WARNING: "+errorInfo(ex)+": "+
			   ex.getMessage());
    }


    public void error(SAXParseException ex) {
	Log.println("ERROR: "+errorInfo(ex)+": "+
			   ex.getMessage());
    }


    public void fatalError(SAXParseException ex) throws SAXException {
	Log.println("FATAL: "+errorInfo(ex)+": "+
			   ex.getMessage());
	throw ex;
	
    }



    
    public XMLParse(InputStream input) {
        try {
            parser = (XMLReader)Class.forName(parserName).newInstance();
            if (parser==null)
                throw new InstantiationException("Failed to create parser");
            parser.setContentHandler(this);
            parser.setErrorHandler(this);
        } catch(ClassNotFoundException ce) {
            Log.println("Failed to create parser: "+ce);
        } catch(IllegalAccessException ce) {
              Log.println("Failed to create parser: "+ce);
        } catch(InstantiationException ce) {
              Log.println("Failed to create parser: "+ce);
        }
        inputStream=new InputSource(input);
    }

    public XMLParse(Reader input) {
	try {
	    parser = (XMLReader)Class.forName(parserName).newInstance();
	    if (parser==null)
		throw new InstantiationException("Failed to create parser");
	    parser.setContentHandler(this);
	    parser.setErrorHandler(this);
	} catch(ClassNotFoundException ce) {
	    Log.println("Failed to create parser: "+ce);
	} catch(IllegalAccessException ce) {
	      Log.println("Failed to create parser: "+ce);
	} catch(InstantiationException ce) {
	      Log.println("Failed to create parser: "+ce);
	}
	inputStream=new InputSource(input);
        // inputStream.setEncoding(enCoding);
    }
    
    XMLParse() {
	try {
	    parser = (XMLReader)Class.forName(parserName).newInstance();
	    if (parser==null)
		throw new InstantiationException("Failed to create parser");
	    parser.setContentHandler(this);
	    parser.setErrorHandler(this);
	} catch(ClassNotFoundException ce) {
	    Log.println("Failed to create parser: "+ce);
	} catch(IllegalAccessException ce) {
	      Log.println("Failed to create parser: "+ce);
	} catch(InstantiationException ce) {
	      Log.println("Failed to create parser: "+ce);
	}
	inputStream=null;
    }
    
    public XMLNode parse(String fileName) {
	currentNode=null;
	topNode=null;
	nodeStack=new Vector();
	//nodeStack.removeAllElements();
	try {
	    parser.parse(fileName);
	} catch(SAXException ce) {
	    Log.println("sax exception "+ce);
	} catch(IOException ce) {
	      Log.println("IO exception "+ce);
	}
	return(topNode);
    }
    
    public XMLNode parse() {
	currentNode=null;
	topNode=null;
	//nodeStack.removeAllElements();
	nodeStack=new Vector();
	try {
	    parser.parse(inputStream);
	} catch(SAXException ce) {
	    Log.println("sax exception "+ce);
	} catch(IOException ce) {
	      Log.println("IO exception "+ce);
	}
	return(topNode);
    }
}
