KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > XDBCMapperAdapter


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.mapper.storage;
24
25 import java.io.IOException JavaDoc;
26 import java.io.StringReader JavaDoc;
27
28 import javax.xml.parsers.SAXParserFactory JavaDoc;
29
30 import org.w3c.dom.Document JavaDoc;
31 import org.xml.sax.*;
32 import org.xml.sax.ext.LexicalHandler JavaDoc;
33 import org.xquark.mapper.RepositoryException;
34 import org.xquark.util.DOMReader;
35 import org.xquark.util.HandlerDecorator;
36 import org.xquark.util.SAXConstants;
37 import org.xquark.xml.xdbc.XMLDBCException;
38 import org.xquark.xml.xdbc.XMLErrorHandler;
39
40 /**
41  * Class used to convert Mapper calls to basic internal calls.
42  */

43 public class XDBCMapperAdapter extends HandlerDecorator
44 implements SAXConstants
45 {
46     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
47     private static final String JavaDoc RCSName = "$Name: $";
48     private static SAXParserFactory JavaDoc parserFactory;
49     
50     static
51     {
52         parserFactory = SAXParserFactory.newInstance();
53         parserFactory.setNamespaceAware(true);
54     }
55     
56     private SAXHandler handler;
57     private DOMReader DOMReader;
58     
59     public XDBCMapperAdapter(ContentHandler contentHandler, LexicalHandler JavaDoc lexicalHandler,
60                                 ErrorHandler errorHandler, SAXHandler handler)
61     {
62         super(contentHandler, lexicalHandler);
63         super.setErrorHandler(errorHandler);
64         this.handler = handler;
65     }
66     
67     public void flushBuffer() throws XMLDBCException
68     {
69         handler.flushBuffer();
70     }
71     
72     public void setErrorHandler(XMLErrorHandler handler)
73     {
74         this.handler.setErrorHandler(handler);
75     }
76     
77     public XMLErrorHandler getErrorHandler()
78     {
79         return handler.getXMLErrorHandler();
80     }
81     
82     public void clearBuffer() throws XMLDBCException
83     {
84         handler.clearBuffer();
85     }
86     
87     public void close() throws XMLDBCException
88     {
89         handler.close();
90     }
91     
92     public boolean getAutoFlush()
93     {
94         return handler.getAutoFlush();
95     }
96     
97     public void setAutoFlush(boolean mode) throws XMLDBCException
98     {
99         handler.setAutoFlush(mode);
100     }
101     
102     ///////////////////////////////////////////////////////////////////////////
103
// Mapper implementation
104
///////////////////////////////////////////////////////////////////////////
105
public void insertXMLDocument(org.xml.sax.InputSource JavaDoc input)
106     throws XMLDBCException, SAXException
107     {
108         insertXMLDocument(createXMLReader(), input);
109     }
110     
111     public void insertXMLDocument(org.xml.sax.XMLReader JavaDoc parser, org.xml.sax.InputSource JavaDoc input)
112     throws XMLDBCException, SAXException
113     {
114         configureXMLReader(parser);
115         try
116         {
117             parser.parse(input);
118         }
119         catch (SAXException e)
120         {
121             if (e.getException() instanceof XMLDBCException)
122                 throw (XMLDBCException)e.getException();
123             else
124                 throw e;
125         }
126         catch (IOException JavaDoc e)
127         {
128             throw new RepositoryException(RepositoryException.PARSER_ERROR,
129                 "IO error during SAX parsing.", e);
130         }
131     }
132     
133     public void insertXMLDocument(String JavaDoc doc) throws XMLDBCException, org.xml.sax.SAXException JavaDoc
134     {
135         // remove first blanks (facility)
136
int index = doc.indexOf("<?xml");
137         if (index > 0)
138             doc = doc.substring(index);
139         insertXMLDocument(new InputSource(new StringReader JavaDoc(doc)));
140     }
141     
142     public void insertXMLDocument(Document JavaDoc document) throws XMLDBCException
143     {
144         if (DOMReader == null)
145             DOMReader = new DOMReader(document);
146         else
147             DOMReader.setDocument(document);
148         XMLReader myReader = configureXMLReader(DOMReader);
149         try
150         {
151             myReader.parse("Fake");
152         }
153         catch (IOException JavaDoc e)
154         {
155             throw new RepositoryException(RepositoryException.PARSER_ERROR,
156                 "Error during internal SAX parsing.", e);
157         }
158         catch (SAXException e)
159         {
160             if (e.getException() instanceof XMLDBCException)
161                 throw (XMLDBCException)e.getException();
162             else
163                 throw new RepositoryException(RepositoryException.PARSER_ERROR,
164                         e.getMessage(), e.getException());
165         }
166     }
167     
168     ///////////////////////////////////////////////////////////////////////////
169
// PRIVATE UTILITIES
170
///////////////////////////////////////////////////////////////////////////
171
protected XMLReader createXMLReader() throws RepositoryException
172     {
173         try
174         {
175             return parserFactory.newSAXParser().getXMLReader();
176         }
177         catch (Exception JavaDoc e)
178         {
179             throw new RepositoryException(RepositoryException.XQUARK_CONF, "Could not instantiate an XMLReader using the property in Repository configuration.", e);
180         }
181     }
182     
183     protected XMLReader configureXMLReader() throws RepositoryException
184     {
185         return configureXMLReader(createXMLReader());
186     }
187     
188     protected XMLReader configureXMLReader(XMLReader reader)
189     throws RepositoryException
190     {
191         try
192         {
193             reader.setContentHandler(this);
194             reader.setErrorHandler(this);
195             reader.setProperty(SAX_LEXICAL_PROPERTY, this);
196         }
197         catch(SAXException e)
198         {
199             throw new RepositoryException(RepositoryException.PARSER_ERROR, "The XML SAX Parser is not SAX 2.0 compliant");
200         }
201         return reader;
202     }
203     
204 }
205
Popular Tags