dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / XmlParser.java
1 /* $Id: XmlParser.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.parser.xml;
3
4 import java.net.MalformedURLException;
5 import java.net.URL;
6
7 import org.apache.xerces.parsers.SAXParser;
8 import org.xml.sax.SAXException;
9
10 import dol.main.UserInterface;
11
12 /**
13  * Parser for DOL XML files.
14  */
15 public class XmlParser extends org.xml.sax.helpers.DefaultHandler {
16
17     /**
18      * Constructor. Initializes the parser.
19      */
20     public XmlParser() {
21         super();
22         try {
23             _parser = new SAXParser();
24             _parser.setFeature("http://xml.org/sax/features/validation",
25                     true);
26             _parser.setFeature("http://apache.org/xml/features/"
27                     + "validation/schema", true);
28             _parser.setFeature("http://apache.org/xml/features/"
29                     + "validation/schema-full-checking", true);
30             _parser.setErrorHandler(new XmlErrorHandler());
31             _parser.setContentHandler(this);
32             _parser.setProperty("http://apache.org/xml/properties/schema/"
33                     + "external-schemaLocation",
34                     dol.util.SchemaLocation.getInternalSchemaLocation());
35             _parser.setContentHandler(this);
36             _parser.setErrorHandler(new XmlErrorHandler());
37         } catch (SAXException e) {
38             e.printStackTrace();
39         } catch (Throwable t) {
40             t.printStackTrace();
41         }
42     }
43
44     /**
45      * Return a absolute URL reference for the given URL.
46      *
47      * @param url url
48      * @return absolute URL reference
49      */
50     protected String _makeAbsoluteURL(String url)
51          throws MalformedURLException {
52         URL baseURL;
53         String fileSep = System.getProperty("file.separator");
54         String base = "";
55         
56         // Replace all separators in given URL
57         url = url.replace(fileSep.charAt(0), '/');
58         
59         // Add working directory if URL is not absolute
60         if (url.charAt(0) != '/' && !url.matches("^[A-Za-z]:/.*")) {
61             base = System.getProperty("user.dir") + '/';
62             base = base.replace(fileSep.charAt(0), '/');
63             if (base.charAt(0) != '/') {
64                 base = "/" + base;
65             }
66         }
67         
68         // Add front slash to Windows style URLs
69         if (url.matches("^[A-Za-z]:/.*")) {
70             url = "/" + url;
71         }
72         
73         baseURL = new URL("file", null, base);
74         System.out.println(" -- full filename: "
75                 + new URL(baseURL, url).toString());
76         return new URL(baseURL, url).toString();
77     }
78
79     /** XML parser */
80     protected SAXParser _parser;
81
82     /** user interface used for messages for users */
83     protected UserInterface _ui = UserInterface.getInstance();
84 }