KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > bridge > MapperImpl


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.bridge;
24
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 import org.w3c.dom.Document JavaDoc;
29 import org.xml.sax.*;
30 import org.xml.sax.ext.LexicalHandler JavaDoc;
31 import org.xquark.mapper.dbms.AbstractConnection;
32 import org.xquark.mapper.storage.SAXHandler;
33 import org.xquark.mapper.storage.XDBCMapperAdapter;
34 import org.xquark.schema.validation.ValidatingSchemaHandler;
35 import org.xquark.xml.xdbc.XMLDBCException;
36
37 /**
38  * Objects implementing this class are used to store XML data
39  * into a database according to the parent mapping.<BR>
40  * An object of this class cannot be used in multiple threads concurrently.
41  * Different Mappers can be used concurrently by different threads.<BR>
42  * A Mapper may be used multiple times, to store several documents based
43  * on the same mapping.<BR>
44  * A Mapper provides three different interfaces to store XML data: the most
45  * efficient is based on SAX2, as the Mapper can be plugged directly as a
46  * org.xml.sax.ContentHandler into any XML parser implementing the org.xml.sax.XMLReader
47  * interface. For convenience, a DOM2-based and a string-based interfaces are also
48  * provided.<BR>
49  * A Mapper object must be explicitly closed after use, in order to free its associated database
50  * resources.
51  * @see org.xml.sax.XMLReader
52  */

53 class MapperImpl extends XDBCMapperAdapter implements Mapper
54 {
55     private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
56     private static final String JavaDoc RCSName = "$Name: $";
57     
58     private Mapping mapping = null;
59     private AbstractConnection connection;
60     private ValidatingSchemaHandler validatingContentHandler = null;
61     
62     /**
63      * Package-private constructor
64      * @param mapping the parent mapping
65      */

66     MapperImpl(Mapping mapping, AbstractConnection connection, ValidatingSchemaHandler validatingContentHandler,
67                LexicalHandler JavaDoc lexicalHandler, ErrorHandler errorHandler, SAXHandler handler)
68         throws XMLDBCException
69     {
70         super(validatingContentHandler, lexicalHandler, errorHandler, handler);
71         this.mapping = mapping;
72         this.connection = connection;
73         this.validatingContentHandler = validatingContentHandler;
74     }
75     
76     /**
77      * Returns the mapping used by this object
78      * @return the parent mapping
79      */

80     public Mapping getMapping()
81     {
82         return mapping;
83     }
84     
85     /**
86      * Closes the object and frees its associated resources.
87      * @throws XMLDBCException if a data source access error occurs.
88      */

89     public void close() throws XMLDBCException
90     {
91         super.close();
92         try {
93             connection.close();
94         } catch (SQLException JavaDoc ex) {
95             throw new XMLDBCException(ex.getMessage(), ex);
96         }
97         mapping.remove(this);
98     }
99     
100     ///////////////////////////////////////////////////////////////////////////
101
// Mapper implementation
102
///////////////////////////////////////////////////////////////////////////
103
public void insertDocument(InputSource input)
104     throws XMLDBCException, SAXException
105     {
106         insertXMLDocument(input);
107     }
108     
109     public void insertDocument(XMLReader parser, InputSource input)
110     throws XMLDBCException, SAXException
111     {
112         insertXMLDocument(parser, input);
113     }
114     
115     public void insertDocument(String JavaDoc doc) throws XMLDBCException, org.xml.sax.SAXException JavaDoc
116     {
117         insertXMLDocument(doc);
118     }
119     
120     public void insertDocument(Document JavaDoc document) throws XMLDBCException
121     {
122         insertXMLDocument(document);
123     }
124     
125     /**
126      * Retrieves the current document base used by this object.
127      *
128      * @return an URI.
129      * @see #setDocumentBase(String)
130      */

131     public String JavaDoc getDocumentBase()
132     {
133         return validatingContentHandler.getDocumentBase();
134     }
135     
136     /**
137      * This method is to be used when the document stored contains an 'xsi:schemaLocation'
138      * attribute (or any element or attribute with the XML Schema type 'anyURI')
139      * that uses relatives URIs and when the API used to store documents does not
140      * use an InputSource with the SystemID set (e.g. DOM or String ones). The document
141      * base provided is then used to resolve the relative URI into an absolute one.
142      *
143      * @param URI The URI to be used as base during document storage.
144      * @see #getDocumentBase
145      */

146     public void setDocumentBase(String JavaDoc URI)
147     {
148         validatingContentHandler.setDocumentBase(URI);
149     }
150
151     /**
152      * Returns the JDBC connection used by this object.
153      * @return a JDBC connection.
154      */

155     public Connection JavaDoc getConnection()
156     {
157         return connection.getConnection();
158     }
159     
160 }
161
162
163
Popular Tags