KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > XMLVerifier


1 /*
2  * %W% %E%
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 import java.io.File JavaDoc;
9 import java.io.BufferedReader JavaDoc;
10 import java.io.StringReader JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.io.StringWriter JavaDoc;
13 import java.io.IOException JavaDoc;
14
15 import org.jdom.Document;
16 import org.jdom.JDOMException;
17 import org.jdom.input.SAXBuilder;
18
19
20 public class XMLVerifier
21     {
22     private File JavaDoc xml;
23
24     public XMLVerifier(File JavaDoc file)
25         {
26         xml = file;
27         }
28
29     public XMLVerifier(String JavaDoc file)
30         {
31         this(new File JavaDoc(file));
32         }
33
34     public void verify()throws IOException JavaDoc
35         {
36         try
37             {
38             System.out.println("Verifying file "+xml+"...");
39             SAXBuilder builder = new SAXBuilder(true);
40             Document doc = builder.build(xml);
41             System.out.println("This file is valid...");
42             }
43         catch(JDOMException e)
44             {
45             System.out.println("\n\n*********************************************************************");
46             Throwable JavaDoc t = e.getCause();
47             if(t != null)
48                 {
49                 StringWriter JavaDoc stw = new StringWriter JavaDoc();
50                 PrintWriter JavaDoc sw = new PrintWriter JavaDoc(stw);
51                 t.printStackTrace(sw);
52                 BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc((stw.getBuffer()).toString()));
53
54                 String JavaDoc firstLine = br.readLine();
55                 if(firstLine.indexOf("java.net.UnknownHostException")!=-1)
56                     {
57                     System.out.println(t.getMessage());
58                     System.out.println("Unable to find external entity. Try invocation with \n java \"-DproxySet=true\" \"-DproxyHost=<IP of Proxy Host>\" \"-DproxyPort=<Proxy Port>\" XMLVerifier <absolute path to XML file>");
59                     System.out.println();
60                     System.out.println();
61                     t.printStackTrace();
62                     }
63                 else if(firstLine.indexOf("org.xml.sax.SAXParseException")!=-1)
64                     {
65                     System.out.println(t.getMessage());
66                     System.out.println("The XML given as input is Not Valid as per its DTD.");
67                     System.out.println();
68                     System.out.println();
69                     t.printStackTrace();
70                     }
71                 else
72                     {
73                     System.out.println(t.getMessage());
74                     System.out.println();
75                     System.out.println();
76                     t.printStackTrace();
77                     }
78                 }
79             System.out.println("\n\n*********************************************************************");
80             }
81         }
82
83     public static void main(String JavaDoc args[])throws IOException JavaDoc
84         {
85         if(args.length == 0)
86             {
87             System.out.println("Usage:");
88             System.out.println("java XMLVerifier <absolute path to XML file>");
89             System.exit(0);
90             }
91         else
92             {
93             XMLVerifier verifier = new XMLVerifier(args[0]);
94             verifier.verify();
95             }
96         }
97     }
Popular Tags