001 /* 002 // $Id: //open/util/resgen/src/org/eigenbase/xom/ParserTester.java#3 $ 003 // Package org.eigenbase.xom is an XML Object Mapper. 004 // Copyright (C) 2005-2005 The Eigenbase Project 005 // Copyright (C) 2005-2005 Disruptive Tech 006 // Copyright (C) 2005-2005 LucidEra, Inc. 007 // Portions Copyright (C) 2000-2005 Kana Software, Inc. and others. 008 // 009 // This library is free software; you can redistribute it and/or modify it 010 // under the terms of the GNU Lesser General Public License as published by the 011 // Free Software Foundation; either version 2 of the License, or (at your 012 // option) any later version approved by The Eigenbase Project. 013 // 014 // This library is distributed in the hope that it will be useful, 015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 // GNU Lesser General Public License for more details. 018 // 019 // You should have received a copy of the GNU Lesser General Public License 020 // along with this library; if not, write to the Free Software 021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 022 // 023 // KLO, 22 July, 2001 024 */ 025 026 package org.eigenbase.xom; 027 import java.io.*; 028 029 /** 030 * Test the MSParser 031 */ 032 public class ParserTester { 033 034 // parser determines the type of parser to use. Currently we support 035 // MSXML and XERCES. 036 private int parserType; 037 private static final int MSXML = 1; 038 private static final int XERCES = 2; 039 040 // These members contain the actual parsers to use. Only one will 041 // ever be set. 042 private Parser parser; 043 044 // This member contain the model document type 045 String modelDocType; 046 047 // This member contain the URL for the DTD file 048 String dtdUrl; 049 050 //Read the XML file 051 public ParserTester(String dtdFile, 052 int parserType) 053 throws XOMException, IOException 054 { 055 this.parserType = parserType; 056 057 parser = null; 058 059 File dtdPath = new File(dtdFile); 060 this.modelDocType = dtdFile.substring(0, dtdFile.indexOf(".")); 061 this.dtdUrl = "file:" + dtdPath.getAbsolutePath(); 062 063 switch (parserType) { 064 // case MSXML: 065 // parser = org.eigenbase.xom.wrappers.MSXMLWrapper.createParser( 066 // modelDocType, dtdPath); 067 // break; 068 case XERCES: 069 parser = new org.eigenbase.xom.wrappers.XercesDOMParser(true); 070 break; 071 default: 072 throw new XOMException("Unknown parser type: " + parserType); 073 } 074 } 075 076 077 public void testFile(String testFile) 078 throws XOMException 079 { 080 // parsing directly from an input stream (rather than a reader). 081 String xmlString = null; 082 try { 083 StringWriter sWriter = new StringWriter(); 084 FileReader reader = new FileReader(testFile); 085 086 if(parserType != MSXML) { 087 PrintWriter out = new PrintWriter(sWriter); 088 out.println("<?xml version=\"1.0\" ?>"); 089 if(modelDocType != null) 090 out.println("<!DOCTYPE " + modelDocType 091 + " SYSTEM \"" + dtdUrl + "\">"); 092 out.flush(); 093 } 094 095 readerToWriter(reader, sWriter); 096 reader.close(); 097 xmlString = sWriter.toString(); 098 } catch (IOException ex) { 099 throw new XOMException("Unable to read input test " 100 + testFile + ": " + ex.getMessage()); 101 } 102 103 parser.parse(xmlString); 104 105 System.out.println("Parsing document succeeded."); 106 } 107 108 /** 109 * Helper function to copy from a reader to a writer 110 */ 111 private static void readerToWriter(Reader reader, Writer writer) 112 throws IOException 113 { 114 int numChars; 115 final int bufferSize = 16384; 116 char[] buffer = new char[bufferSize]; 117 while((numChars = reader.read(buffer)) != -1) { 118 if(numChars > 0) 119 writer.write(buffer, 0, numChars); 120 } 121 } 122 123 /** 124 * The ParserTester tests MSXML parser and Xerces Parser. Arguments: 125 * <ol> 126 * <li> The DTD file of the XML file 127 * <li> The XML file for this DTD file 128 * </ol> 129 * </p> 130 */ 131 public static void main(String args[]) 132 throws XOMException, IOException 133 { 134 int firstArg = 0; 135 if(args.length > 0 && args[0].equals("-debug")){ 136 System.err.println("parserTest pausing for debugging. " 137 + "Attach your debugger " 138 + "and press return."); 139 try { 140 System.in.read(); 141 firstArg++; 142 } 143 catch(IOException ex) { 144 // Do nothing 145 } 146 } 147 148 int parserType = MSXML; 149 if (firstArg < args.length && args[firstArg].equals("-msxml")) { 150 parserType = MSXML; 151 firstArg++; 152 } 153 else if (firstArg < args.length && args[firstArg].equals("-xerces")) { 154 parserType = XERCES; 155 firstArg++; 156 } 157 158 if(args.length < firstArg+2) { 159 System.err.println( 160 "Usage: java ParserTester [-debug] [-msxml | -xerces] " 161 + "<DTD file> <XML file>"); 162 System.exit(-1); 163 } 164 165 ParserTester parserTester = new ParserTester(args[firstArg], parserType); 166 firstArg++; 167 168 parserTester.testFile(args[firstArg]); 169 } 170 171 } 172 173 174 // End ParserTester.java