📄 xmlinfo.java
字号:
/* * XMLInfo.java * 11/05/00 * CoolServlets.com */package com.coolservlets.forum.xml;import java.io.*;import java.util.*;import org.xml.sax.*;import org.xml.sax.helpers.*;import org.apache.xerces.parsers.SAXParser;/** * USAGE: java XMLInfo [URI] */public class XMLInfo { private InputStream in; private XMLReader parser; private String jiveXMLVersion; private Date exportDate; private Hashtable properties; private List forumNames; private List usernames; /** * @param in The input stream where we can read Jive XML data from. */ public XMLInfo( InputStream in ) throws SAXException { // set the input stream this.in = in; // initialize the parser, register content handlers initParser(); // initialize our datastructures properties = new Hashtable(); forumNames = new ArrayList(); usernames = new ArrayList(); } /** * @return version The Jive version found in the parsed XML document, * ie, "1.0.2" */ public String getJiveXMLVersion() { return jiveXMLVersion; } /** * @param jiveXMLVersion The XML schema version indicated in this XML document. */ protected void setJiveXMLVersion( String jiveXMLVersion ) { this.jiveXMLVersion = jiveXMLVersion; } /** * @return The date this Jive data was exported. */ public Date getExportDate() { return exportDate; } /** * @param exportDate The date this data was exported */ protected void setExportDate( Date exportDate ) { this.exportDate = exportDate; } /** * @return An iterator of forum names found in the imported XML document */ public Iterator getForumNames() { return forumNames.iterator(); } /** * @return The number of forums found in this XML document */ public int getForumCount() { return forumNames.size(); } /** * @param forumName A forum name. */ protected void addForumName( String forumName ) { forumNames.add(forumName); } /** * @return An enumeration of Jive properties (upper-level properties like * database connection info) found in the imported XML document. */ public Enumeration getPropertyNames() { return properties.keys(); } /** * @param A property name */ public String getProperty( String propertyName ) { if( propertyName == null ) { return null; } return (String)properties.get(propertyName); } /** * @return The number of upper-level properties found in this XML file */ public int getPropertyCount() { return properties.size(); } /** * @param name The name (id) of the property to be added * @param value The value of the property */ protected void addProperty( String name, String value ) { if( name != null ) { properties.put(name,value); } } /** * @return An iterator of usernames found in the imported XML document. */ public Iterator getUsernames() { return usernames.iterator(); } /** * @return The number of users found in the imported XML document. */ public int getUserCount() { return usernames.size(); } /** * @param username A username to add. */ protected void addUsername( String username ) { if( username != null ) { usernames.add(username); } } // Initialize the parser, register content & error handlers. private void initParser() { parser = new SAXParser(); XMLInfoHandler handler = new XMLInfoHandler( this ); ContentHandler contentHandler = handler; ErrorHandler errorHandler = handler; parser.setContentHandler( contentHandler ); parser.setErrorHandler( errorHandler ); } /** * Parses the XML document. */ public void parse() throws SAXException, IOException { InputSource src = new InputSource(in); parser.parse( new InputSource(in) ); } public static void main(String args[]) { if( args.length < 1 ) { System.err.println( "USAGE: java XMLInfo [xml_uri]" ); System.exit(1); } long time = System.currentTimeMillis(); String uri = args[0]; // make a stream of the input source InputStream instream = null; try { System.out.println( "Parsing: " + uri ); instream = new FileInputStream( uri ); XMLInfo info = new XMLInfo( instream ); info.parse(); // print out jive xml version System.out.println( "\tjive XML version = " + info.getJiveXMLVersion() ); // print out the export date System.out.println( "\texport Date = " + info.getExportDate() ); // print out the list of jive properties System.out.println( "Number of properties: " + info.getPropertyCount() ); /* Enumeration enum = info.getPropertyNames(); if( !enum.hasMoreElements() ) { System.out.println( "\tNo props found" ); } while( enum.hasMoreElements() ) { String name = (String)enum.nextElement(); System.out.println( "\tprop> " + name + "=" + info.getProperty(name) ); } */ // print out the list of forums System.out.println( "Number of forums: " + info.getForumCount() ); /* Iterator it = info.getForumNames(); while( it.hasNext() ) { String name = (String)it.next(); System.out.println( "\tforum> " + name ); } */ // print out the list of users System.out.println( "Number of users: " + info.getUserCount() ); /* it = info.getUsernames(); while( it.hasNext() ) { String username = (String)it.next(); System.out.println( "\tusername> " + username ); } */ time = System.currentTimeMillis() - time; System.out.println("------------------"); System.out.println("time: " + ((double)time/1000.0) ); } catch( Exception e ) { System.err.println( "Exception in XMLInfo:" ); e.printStackTrace(); } finally { try { if( instream != null ) { instream.close(); } System.err.println( "Stream closed" ); } catch( Exception e ) {} } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -