dol: initial dol commit
[jump.git] / dol / src / dol / parser / xml / XmlParser.java
diff --git a/dol/src/dol/parser/xml/XmlParser.java b/dol/src/dol/parser/xml/XmlParser.java
new file mode 100644 (file)
index 0000000..2032788
--- /dev/null
@@ -0,0 +1,84 @@
+/* $Id: XmlParser.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.parser.xml;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.xerces.parsers.SAXParser;
+import org.xml.sax.SAXException;
+
+import dol.main.UserInterface;
+
+/**
+ * Parser for DOL XML files.
+ */
+public class XmlParser extends org.xml.sax.helpers.DefaultHandler {
+
+    /**
+     * Constructor. Initializes the parser.
+     */
+    public XmlParser() {
+        super();
+        try {
+            _parser = new SAXParser();
+            _parser.setFeature("http://xml.org/sax/features/validation",
+                    true);
+            _parser.setFeature("http://apache.org/xml/features/"
+                    + "validation/schema", true);
+            _parser.setFeature("http://apache.org/xml/features/"
+                    + "validation/schema-full-checking", true);
+            _parser.setErrorHandler(new XmlErrorHandler());
+            _parser.setContentHandler(this);
+            _parser.setProperty("http://apache.org/xml/properties/schema/"
+                    + "external-schemaLocation",
+                    dol.util.SchemaLocation.getInternalSchemaLocation());
+            _parser.setContentHandler(this);
+            _parser.setErrorHandler(new XmlErrorHandler());
+        } catch (SAXException e) {
+            e.printStackTrace();
+        } catch (Throwable t) {
+            t.printStackTrace();
+        }
+    }
+
+    /**
+     * Return a absolute URL reference for the given URL.
+     *
+     * @param url url
+     * @return absolute URL reference
+     */
+    protected String _makeAbsoluteURL(String url)
+         throws MalformedURLException {
+        URL baseURL;
+        String fileSep = System.getProperty("file.separator");
+        String base = "";
+        
+        // Replace all separators in given URL
+        url = url.replace(fileSep.charAt(0), '/');
+        
+        // Add working directory if URL is not absolute
+        if (url.charAt(0) != '/' && !url.matches("^[A-Za-z]:/.*")) {
+            base = System.getProperty("user.dir") + '/';
+            base = base.replace(fileSep.charAt(0), '/');
+            if (base.charAt(0) != '/') {
+                base = "/" + base;
+            }
+        }
+        
+        // Add front slash to Windows style URLs
+        if (url.matches("^[A-Za-z]:/.*")) {
+            url = "/" + url;
+        }
+        
+        baseURL = new URL("file", null, base);
+        System.out.println(" -- full filename: "
+                + new URL(baseURL, url).toString());
+        return new URL(baseURL, url).toString();
+    }
+
+    /** XML parser */
+    protected SAXParser _parser;
+
+    /** user interface used for messages for users */
+    protected UserInterface _ui = UserInterface.getInstance();
+}