FileDocCategorySizeDatePackage
SimpleXMLParserDocumentImpl.javaAPI DocAzureus 3.0.3.48471Thu Jul 26 14:47:38 BST 2007org.gudy.azureus2.pluginsimpl.local.utils.xml.simpleparser

SimpleXMLParserDocumentImpl

public class SimpleXMLParserDocumentImpl extends Object implements org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocument

Fields Summary
protected Document
document
protected SimpleXMLParserDocumentNodeImpl
root_node
Constructors Summary
public SimpleXMLParserDocumentImpl(File file)

		try{
			
			create( new FileInputStream( file ));
			
		}catch( Throwable e ){
			
			throw( new SimpleXMLParserDocumentException( e ));
		}
	
public SimpleXMLParserDocumentImpl(String data)

		try{
			create( new ByteArrayInputStream( data.getBytes( Constants.DEFAULT_ENCODING )));
			
		}catch( UnsupportedEncodingException e ){
			
		}
	
public SimpleXMLParserDocumentImpl(InputStream input_stream)

		create( input_stream );
	
Methods Summary
protected voidcreate(java.io.InputStream input_stream)

		try{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

			// Set namespaceAware to true to get a DOM Level 2 tree with nodes
			// containing namesapce information.  This is necessary because the
			// default value from JAXP 1.0 was defined to be false.
						
			dbf.setNamespaceAware(true);

			// Set the validation mode to either: no validation, DTD
			// validation, or XSD validation
					
			dbf.setValidating( false );
					
			// Optional: set various configuration options
					
			dbf.setIgnoringComments(true);
			dbf.setIgnoringElementContentWhitespace(true);
			dbf.setCoalescing(true);
					
			// The opposite of creating entity ref nodes is expanding them inline
			// NOTE that usage of, e.g. "&" in text results in an entity ref. e.g.
			//	if ("BUY".equals (type) "
			//		ENT_REF: nodeName="amp"
			//		TEXT: nodeName="#text" nodeValue="&"
			
			dbf.setExpandEntityReferences(true);
					

			// Step 2: create a DocumentBuilder that satisfies the constraints
			// specified by the DocumentBuilderFactory
					
			DocumentBuilder db = dbf.newDocumentBuilder();

			// Set an ErrorHandler before parsing
					
			OutputStreamWriter errorWriter = new OutputStreamWriter(System.err);
					
			MyErrorHandler error_handler = new MyErrorHandler(new PrintWriter(errorWriter, true));

			db.setErrorHandler( error_handler );

			// Step 3: parse the input file
					
			document = db.parse( input_stream );
						
			SimpleXMLParserDocumentNodeImpl[] root_nodes = parseNode( document, false );
	
			int	root_node_count	= 0;
			
				// remove any processing instructions such as <?xml-stylesheet
			
			for (int i=0;i<root_nodes.length;i++){
				
				SimpleXMLParserDocumentNodeImpl	node = root_nodes[i];
				
				if ( node.getNode().getNodeType() != Node.PROCESSING_INSTRUCTION_NODE ){
					
					root_node	= node;
					
					root_node_count++;
				}
			}
			
			if ( root_node_count != 1 ){
								
				throw( new SimpleXMLParserDocumentException( "invalid document - " + root_nodes.length + " root elements" ));
			}
						
		}catch( Throwable e ){
			
			// e.printStackTrace();
			
			throw( new SimpleXMLParserDocumentException( e ));
		}
	
public org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentAttributegetAttribute(java.lang.String name)

		return( root_node.getAttribute(name));
	
public org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentAttribute[]getAttributes()

		return( root_node.getAttributes());
	
public org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentNodegetChild(java.lang.String name)

		return( root_node.getChild(name));
	
public org.gudy.azureus2.plugins.utils.xml.simpleparser.SimpleXMLParserDocumentNode[]getChildren()

		return( root_node.getChildren());
	
public java.lang.StringgetName()

		return( root_node.getName());
	
public java.lang.StringgetValue()

		return( root_node.getValue());
	
public static voidmain(java.lang.String[] args)

    	
    	try{
			StringBuffer	data = new StringBuffer(1024);
			
			FileInputStream is = new FileInputStream( "C:\\temp\\upnp_trace3.log" );
			
			LineNumberReader	lnr = new LineNumberReader( new InputStreamReader( is, "UTF-8" ));
			
			while( true ){
				
				String	line = lnr.readLine();
				
				if ( line == null ){
					
					break;
				}
				
				for (int i=0;i<line.length();i++){
					char	c = line.charAt(i);
				
					if ( c < 0x20 ){
						data.append( ' " );
					}else{
						data.append( c );
					}
				}
				
				data.append( "\n" );	
			}
				
			String	data_str = data.toString();
			
	  		new SimpleXMLParserDocumentImpl( data_str ).print();
	  		
	  		//new SimpleXMLParserDocumentImpl(new File( "C:\\temp\\upnp_trace3.log")).print();
	  		    		
    	}catch( Throwable e ){
    		
    		e.printStackTrace();
    	}
    
protected SimpleXMLParserDocumentNodeImpl[]parseNode(org.w3c.dom.Node node, boolean skip_this_node)

        int type = node.getNodeType();
		
		if ( (	type == Node.ELEMENT_NODE ||
				type == Node.PROCESSING_INSTRUCTION_NODE )&& !skip_this_node ){
			
			return( new SimpleXMLParserDocumentNodeImpl[]{ new SimpleXMLParserDocumentNodeImpl( this, node )});
		}

		Vector	v = new Vector();
		
        for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()){
			
			SimpleXMLParserDocumentNodeImpl[] kids = parseNode( child, false );
			
			for (int i=0;i<kids.length;i++){
				
				v.addElement(kids[i]);
			}
        }
		
		SimpleXMLParserDocumentNodeImpl[]	res = new SimpleXMLParserDocumentNodeImpl[v.size()];
		
		v.copyInto( res );
		
		return( res );
	
public voidprint()

		root_node.print( "" );