KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > xml > XMLDefinitionHelper


1 /*
2  * This source code is heavily based on source code from the Apache
3  * Ant project. As such the following is included:
4  * ------------------------------------------------------------------
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 2000-2003 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "Ant" and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58
59 package org.oddjob.arooa.xml;
60
61 import java.io.IOException JavaDoc;
62 import java.io.UnsupportedEncodingException JavaDoc;
63
64 import org.oddjob.arooa.ArooaException;
65 import org.oddjob.arooa.ArooaHandler;
66 import org.oddjob.arooa.ArooaContext;
67 import org.oddjob.arooa.Location;
68 import org.oddjob.arooa.RootHandler;
69 import org.xml.sax.InputSource JavaDoc;
70 import org.xml.sax.SAXException JavaDoc;
71 import org.xml.sax.SAXParseException JavaDoc;
72 import org.xml.sax.XMLReader JavaDoc;
73 import org.xml.sax.helpers.DefaultHandler JavaDoc;
74
75 /**
76  * Sax2 based project reader
77  * <p>
78  * Based on an original by <b>duncan@x180.com</b> and <b>Costin Manolache</p>.
79  */

80 public class XMLDefinitionHelper {
81
82     private final ArooaContext context;
83
84     public XMLDefinitionHelper(ArooaContext context) {
85         this.context = context;
86     }
87         
88     /**
89      * Parse a source xml input.
90      *
91      * @param inputSource the xml source
92      * @param mainHandler The first handler.
93      *
94      * @exception ArooaException if an error occurs
95      */

96     public void parse(InputSource JavaDoc inputSource, ArooaHandler mainHandler)
97             throws ArooaException {
98
99         RootHandler handler = new RootHandler(context, mainHandler);
100
101         try {
102             /**
103              * SAX 2 style parser used to parse the given file.
104              */

105             XMLReader JavaDoc parser = JAXPUtils.getNamespaceXMLReader();
106
107             DefaultHandler JavaDoc hb = handler;
108
109             parser.setContentHandler(hb);
110             parser.setEntityResolver(hb);
111             parser.setErrorHandler(hb);
112             parser.setDTDHandler(hb);
113             parser.parse(inputSource);
114         } catch (SAXParseException JavaDoc exc) {
115             Location location = new Location(exc.getSystemId(),
116                 exc.getLineNumber(), exc.getColumnNumber());
117
118             Throwable JavaDoc t = exc.getException();
119             if (t == null) {
120                 throw new ArooaException(exc.getMessage(), exc, location);
121             }
122             if (t instanceof ArooaException) {
123                 ArooaException be = (ArooaException) t;
124                 if (be.getLocation() == Location.UNKNOWN_LOCATION) {
125                     be.setLocation(location);
126                 }
127                 throw be;
128             }
129             throw new ArooaException(exc.getMessage(), t, location);
130         } catch (SAXException JavaDoc exc) {
131             Throwable JavaDoc t = exc.getException();
132             if (t instanceof ArooaException) {
133                 throw (ArooaException) t;
134             }
135             throw new ArooaException(exc.getMessage(), t);
136         } catch (UnsupportedEncodingException JavaDoc exc) {
137               throw new ArooaException("Encoding of input is invalid.",
138                                        exc);
139         } catch (IOException JavaDoc exc) {
140             throw new ArooaException("Error reading input.",
141                                      exc);
142         }
143     }
144 }
145
Popular Tags