KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > xml > XmlParserTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s):
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.xml;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileReader JavaDoc;
31 import java.io.FilenameFilter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 import org.objectweb.cjdbc.common.xml.XmlValidator;
38 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
39
40 /**
41  * Test to validate xml document of c-jdbc DTDs.
42  *
43  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
44  */

45 public class XmlParserTest extends NoTemplate
46 {
47   /** File name containing the C-JDBC URLs to test. */
48   public static final String JavaDoc XML_FILE = "xmls.txt";
49   /** The absolute path to the above xmlFile */
50   public static String JavaDoc xmlFiles;
51
52   private int count = 0;
53   private int failed = 0;
54
55   /**
56    * Initialize the xml parser
57    */

58   public void setUp()
59   {
60     xmlFiles = getTextPath(XML_FILE);
61   }
62
63   private String JavaDoc getRelativePath(String JavaDoc pathFromTxt)
64   {
65     return getUserDir() + File.separator + pathFromTxt;
66   }
67
68   /**
69    * Use the file <code>XML_FILES</code> to verify XML document
70    *
71    * @throws IOException when fails reading
72    */

73   public void testXmlDocuments() throws IOException JavaDoc
74   {
75     File JavaDoc file = new File JavaDoc(xmlFiles);
76     BufferedReader JavaDoc d = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
77         new FileInputStream JavaDoc(file)));
78     String JavaDoc line;
79     boolean allResultsOk = true;
80     while ((line = d.readLine()) != null)
81     {
82       if (line.startsWith("###") || line.trim().equals(""))
83         continue;
84       else
85       {
86         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line, ";");
87         String JavaDoc xmlFile = getRelativePath(st.nextToken());
88         String JavaDoc dtdFile = getRelativePath(st.nextToken());
89         boolean valid = new Boolean JavaDoc(st.nextToken()).booleanValue();
90
91         File JavaDoc xfile = new File JavaDoc(xmlFile);
92         boolean value = false;
93         if (xfile.isDirectory())
94         {
95           value = analyseDirectoryWithDtd(dtdFile, xfile);
96         }
97         else
98         {
99           value = analyseXmlAndDtd(dtdFile, xmlFile);
100         }
101     System.out.print("File '" + xmlFile + "': ");
102     System.out.println((valid == value) ? "ok" : "ko");
103         allResultsOk = allResultsOk && (valid == value);
104       }
105     }
106
107     System.out.println("SUMMARY: " + count + " files tested, " + failed
108         + " failed.");
109     assertTrue(failed + " tests failed", allResultsOk);
110   }
111
112   private boolean analyseDirectoryWithDtd(String JavaDoc pathToDtd, File JavaDoc dir)
113   {
114     System.out.println("Analysing directory:" + dir.getAbsolutePath() + ":");
115     String JavaDoc[] toAnalyse = dir.list(new FilenameFilter JavaDoc()
116     {
117       /**
118        * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
119        */

120       public boolean accept(File JavaDoc dir, String JavaDoc name)
121       {
122         if (name.endsWith(".xml"))
123           return true;
124         else
125           return false;
126       }
127     });
128
129     boolean allResultsOk = true;
130     boolean test = true;
131
132     for (int i = 0; i < toAnalyse.length; i++)
133     {
134       try
135       {
136         test = analyseXmlAndDtd(pathToDtd, dir.getAbsolutePath()
137             + File.separator + toAnalyse[i]);
138       }
139       catch (Exception JavaDoc e)
140       {
141         test = false;
142       }
143       allResultsOk = allResultsOk && test;
144
145     }
146
147     return allResultsOk;
148   }
149
150   private boolean analyseXmlAndDtd(String JavaDoc pathToDtd, String JavaDoc pathToXml)
151       throws IOException JavaDoc
152   {
153
154     File JavaDoc dtd = new File JavaDoc(pathToDtd);
155     File JavaDoc file = new File JavaDoc(pathToXml);
156     System.out.print("Analysing file[" + (count++) + "]\t:");
157     FileReader JavaDoc reader = new FileReader JavaDoc(file);
158
159     XmlValidator validator = new XmlValidator(pathToDtd, reader);
160     if (!validator.isDtdValid())
161       System.out.print("[FAILED: dtd is not valid]");
162     else if (!validator.isXmlValid())
163       System.out.print("[FAILED: xml is not valid]");
164     else if (validator.isXmlValid())
165       System.out.print("[OK]");
166
167     System.out.println("\t:" + file.getName() + " with dtd:" + dtd.getName());
168
169     if (validator.getLastException() != null)
170     {
171       ArrayList JavaDoc errors = validator.getExceptions();
172       for (int i = 0; i < errors.size(); i++)
173         System.out.println("\t(parsing error):"
174             + ((Exception JavaDoc) errors.get(i)).getMessage());
175     }
176
177     if (!validator.isValid())
178       failed++;
179
180     return validator.isValid();
181   }
182 }
183
Popular Tags