KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > SaxMapper


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.util;
24
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.util.Stack JavaDoc;
29
30 import javax.xml.parsers.SAXParserFactory JavaDoc;
31
32 import org.xml.sax.*;
33 import org.xml.sax.helpers.DefaultHandler JavaDoc;
34 import org.xquark.schema.validation.SchemaValidationContext;
35 import org.xquark.schema.validation.ValidatingSchemaFilter;
36
37 /**
38  * This class allows hiding the SAX specificities when constructing java objects
39  * from an XML document with an XML schema. This abstract has to be subclassed
40  * to make a custom builder for java objects
41  *
42  */

43 public abstract class SaxMapper extends DefaultHandler JavaDoc
44 {
45     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
46     private static final String JavaDoc RCSName = "$Name: $";
47
48     public abstract Object JavaDoc getMappedObject();
49     public abstract TagTracker createTagTrackerNetwork();
50
51     // A stack for the tag trackers to
52
// coordinate on.
53
//
54
private Stack JavaDoc tagStack = new Stack JavaDoc();
55
56     // The SAX 2 parser...
57
private XMLReader xr;
58
59     private SchemaValidationContext context;
60
61     public SaxMapper()
62     {
63
64         try
65         {
66             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
67             factory.setNamespaceAware(true);
68             xr = factory.newSAXParser().getXMLReader();
69         }
70         catch (Exception JavaDoc e)
71         {
72             throw new RuntimeException JavaDoc(
73                 "JAXP exception while intantiating a SAX parser for internal use ("
74                     + e.getMessage()
75                     + ").");
76         }
77
78         context = new SchemaValidationContext();
79         xr = new ValidatingSchemaFilter(xr, context);
80
81         // Create the tag tracker network
82
// and initialize the stack with
83
// it.
84
//
85
// This constructor anchors the tag
86
// tracking network to the begining
87
// of the XML document. ( before the
88
// first tag name is located ).
89
//
90
// By placing it first on the stack
91
// all future tag tracking will follow
92
// the network anchored by this
93
// root tag tracker.
94
//
95
// The createTagTrackerNetwork() method
96
// is abstract. All sub classes are
97
// responsible for reacting to this
98
// request with the creation of a
99
// tag tracking network that will
100
// perform the mapping for the sub class.
101
//
102
tagStack.push(createTagTrackerNetwork());
103
104     }
105
106     public Object JavaDoc fromXML(String JavaDoc url)
107     {
108
109         try
110         {
111             return fromXML(new InputSource(url));
112
113         }
114         catch (Exception JavaDoc e)
115         {
116             e.printStackTrace();
117             return null;
118         }
119     }
120
121     public Object JavaDoc fromXML(InputStream JavaDoc in)
122     {
123         try
124         {
125             return fromXML(new InputSource(in));
126
127         }
128         catch (Exception JavaDoc e)
129         {
130             e.printStackTrace();
131             return null;
132         }
133     }
134
135     public Object JavaDoc fromXML(Reader JavaDoc in)
136     {
137         try
138         {
139             return fromXML(new InputSource(in));
140
141         }
142         catch (Exception JavaDoc e)
143         {
144             e.printStackTrace();
145             return null;
146         }
147     }
148
149     private synchronized Object JavaDoc fromXML(InputSource in)
150     throws SAXException, IOException JavaDoc
151     {
152
153         // notes,
154
// 1. The calling "fromXML" methods catch
155
// any parsing exceptions.
156
// 2. The method is synchronized to keep
157
// multiple threads from accessing the XML parser
158
// at once. This is a limitation imposed by SAX.
159

160         // Set the ContentHandler...
161
xr.setContentHandler(this);
162
163         // Parse the file...
164
xr.parse(in);
165
166         return getMappedObject();
167     }
168
169     // Implement the content hander methods that
170
// will delegate SAX events to the tag tracker network.
171

172     public void startElement(
173         String JavaDoc namespaceURI,
174         String JavaDoc localName,
175         String JavaDoc qName,
176         Attributes attr)
177         throws SAXException
178     {
179
180         // delegate the event handling to the tag tracker
181
// network.
182
TagTracker activeTracker = (TagTracker) tagStack.peek();
183         activeTracker.startElement(context.getCurrentInfoset(), tagStack);
184
185     }
186
187     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName)
188         throws SAXException
189     {
190
191         // delegate the event handling to the tag tracker
192
// network.
193
TagTracker activeTracker = (TagTracker) tagStack.peek();
194         activeTracker.endElement(context.getCurrentInfoset(), tagStack);
195
196     }
197 }
198
Popular Tags