KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xquark.mapper.RepositoryException;
31 import org.xquark.mapper.metadata.RepositoryConstants;
32 import org.xquark.mapper.metadata.StoragePathMetadata;
33 import org.xquark.mapper.util.DestructionToken;
34 import org.xquark.mapper.util.Functions;
35 import org.xquark.schema.validation.SchemaValidationContext;
36 import org.xquark.xml.xdbc.*;
37 import org.xquark.xpath.NodeKind;
38
39 /** Class using SAX2 event handlers for building the optimized XML logical structure.
40  *
41  * <ul>
42  * <li>{@link org.xml.sax.ContentHandler ContentHandler}</li>
43  * </ul>
44  *
45  * @see org.xml.sax.ContentHandler
46  */

47 public class CollectionFiler extends SAXHandler
48 {
49     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
50     private static final String JavaDoc RCSName = "$Name: $";
51
52     //
53
// XMLCollection (database) related data
54
//
55
protected _RepositoryCollection collection;
56     protected String JavaDoc docID;
57     private boolean IDSetByUser = false;
58     private DestructionToken destructor;
59     private boolean storePrefixes = true; // if false, prefix storage is not performed.
60
private List JavaDoc declaredPrefixes;
61
62     
63     /** Constructor
64      */

65     public CollectionFiler(_RepositoryCollection collection, SchemaValidationContext schemaContext, boolean storePrefixes, DestructionToken destructor) throws XMLDBCException
66     {
67         super(
68             collection.getMetadata().getPathSet().createIterator(),
69             collection.getRepositoryConnection().getConnection(),
70             new ModelBuilder(collection, schemaContext)
71             );
72         this.storePrefixes = storePrefixes;
73         if (storePrefixes)
74             declaredPrefixes = new ArrayList JavaDoc(3);
75         this.collection = collection;
76         this.destructor = destructor;
77         textData.ensureCapacity(collection.getInfo().getMaxTextLength());
78     }
79     
80     public void reset() throws RepositoryException
81     {
82         super.reset();
83         IDSetByUser = false;
84         declaredPrefixes.clear();
85     }
86     
87     public void close() throws XMLDBCException
88     {
89         super.close();
90         if (destructor != null)
91         {
92             declaredPrefixes = null;
93             destructor.destruct(this);
94             destructor = null;
95         }
96     }
97     
98     protected void processFatalError(Exception JavaDoc e) throws SAXException JavaDoc
99     {
100         super.processFatalError(e);
101         collection.restoreDID(builder.getDocumentOID());
102     }
103     
104     //////////////////////////////////////////////////////
105
// Implementation of LexicalHandler interface.
106
//////////////////////////////////////////////////////
107
public void comment(char ch[], int start, int length) throws SAXException JavaDoc
108     {
109         //Debug.print("comment");
110

111         // create a node in the model
112
try
113         {
114             ((ModelBuilder)builder).extraNode(NodeKind.COMMENT, context.getStoragePathMetadata(), new String JavaDoc(ch, start, length), locator);
115         }
116         catch(XMLDBCException e)
117         {
118             processFatalError(e);
119         }
120         catch(SQLException JavaDoc e)
121         {
122             processFatalError(e);
123         }
124     }
125     
126     public void startCDATA() throws SAXException JavaDoc
127     {
128         //Debug.print("startCDATA");
129

130         // create a node in the model
131
try
132         {
133             ((ModelBuilder)builder).extraNode(RepositoryConstants.START_CDATA_SECTION,context.getStoragePathMetadata(), null, locator);
134         }
135         catch(XMLDBCException e)
136         {
137             processFatalError(e);
138         }
139         catch(SQLException JavaDoc e)
140         {
141             processFatalError(e);
142         }
143     }
144
145     public void endCDATA() throws SAXException JavaDoc
146     {
147         //Debug.print("startCDATA");
148

149         // create a node in the model
150
try
151         {
152             ((ModelBuilder)builder).extraNode(RepositoryConstants.END_CDATA_SECTION,context.getStoragePathMetadata(), null, locator);
153         }
154         catch(XMLDBCException e)
155         {
156             processFatalError(e);
157         }
158         catch(SQLException JavaDoc e)
159         {
160             processFatalError(e);
161         }
162     }
163     ////////////////////////////////////////////////////////////////////
164
// Implementation of ContentHandler interface.
165
////////////////////////////////////////////////////////////////////
166
public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
167     throws SAXException JavaDoc
168     {
169         //Debug.print("startPrefixMapping");
170
// Store the prefix until the element is saved
171
declaredPrefixes.add("{" + uri + "}" + prefix);
172     }
173     
174     protected void processPrefixes()
175     throws XMLDBCException, SQLException JavaDoc
176     {
177         if (storePrefixes)
178         {
179             int prefixCount = declaredPrefixes.size();
180             String JavaDoc prefix;
181             for (int i = 0; i < prefixCount; i++)
182             {
183                 // create a model node for prefix definition
184
((ModelBuilder)builder).extraNode(
185                 NodeKind.NAMESPACE,
186                 context.getStoragePathMetadata(),
187                 (String JavaDoc)declaredPrefixes.get(i),
188                 locator
189                 );
190             }
191             declaredPrefixes.clear();
192         }
193     }
194     
195     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc
196     {
197         //Debug.print("processingInstruction");
198

199         // create a node in the model
200
try
201         {
202             ((ModelBuilder)builder).extraNode(NodeKind.PI, context.getStoragePathMetadata(), target + " " + data, locator);
203         }
204         catch(XMLDBCException e)
205         {
206             processFatalError(e);
207         }
208         catch(SQLException JavaDoc e)
209         {
210             processFatalError(e);
211         }
212     }
213     
214     protected boolean textBetweenTransitions()
215     {
216         boolean ret = textBetweenTransitions;
217         
218         // removes (if option set) white space data passed by the parser because there is no DTD
219
if (removeWhitespace)
220         {
221             int i, max = textData.length();
222             for (i = 0; i < max; i++)
223                 if (!Functions.isWhitespace(textData.charAt(i)))
224                     break;
225             if (i == max)// Only found WS : text ignored
226
{
227                 textData.setLength(0);
228                 ret = false;
229             }
230         }
231         return ret;
232     }
233           
234     ////////////////////////////////////////////////
235
// STORAGE METHODS
236
////////////////////////////////////////////////
237
protected StoragePathMetadata push(String JavaDoc namespaceURI, String JavaDoc localName)
238     throws SAXException JavaDoc, RepositoryException
239     {
240         StoragePathMetadata pathNode = super.push(namespaceURI, localName);
241         // Updating current path and send it to path set
242
if (pathNode == null)
243             throw new SAXException JavaDoc("Path {" + namespaceURI + "}" + localName
244             + " could not be written in the underlying database. Storage is cancelled to avoid metadata corruption.");
245         return pathNode;
246     }
247     
248     /** Saves an XML document in this repository from a SAX 2.0 source. The
249      * external SAX parser is responsible for parsing and validating documents.
250      * @param reader The SAX XMLReader used for parsing the document.
251      * The parser is assumed to be SAX 2.0 compliant.
252      * @param source the URL or stream where document can be found.
253      * @param ID The internal name used to identify the document in
254      * the repository. If null, the internal generated numeric handle is used.
255      * @return the ID of the document passed as a parameter.
256      * @throws RepositoryException Application exception.
257      */

258     protected void startParsing() throws RepositoryException, SQLException JavaDoc
259     {
260         super.startParsing();
261         
262         // remove old user value
263
if (!IDSetByUser)
264             docID = null;
265         
266         // transmits document ID to model builder and get the OID
267
docID = ((ModelBuilder)builder).allocateDocOID(docID);
268     }
269     
270     ///////////////////////////////////////////////////////////////////////////
271
// XMLDocumentFiler-like implementation
272
///////////////////////////////////////////////////////////////////////////
273
public void setDocumentId(String JavaDoc ID) throws XMLDBCException
274     {
275         if ((ID != null) && (ID.length() > ID_LENGTH))
276             throw new RepositoryException(RepositoryException.ILLEGAL_EXPRESSION, "The maximum size authorized for document ID ("
277             + ID_LENGTH + ") is exceeded.");
278         
279         if (ID != null)
280         {
281             docID = ID.trim();
282             IDSetByUser = true;
283         }
284     }
285     
286     public XMLCollection getCollection()
287     {
288         return collection;
289     }
290     
291     public String JavaDoc getDocumentId()
292     {
293         return docID; // do not use the storage context value cos' it is reset at the end of parsing
294
}
295     
296     public void setXMLProperty(String JavaDoc propertyId, Object JavaDoc value) throws XMLDBCNotRecognizedException, XMLDBCNotSupportedException
297     {
298         throw new XMLDBCNotSupportedException("Not supported yet");
299         // TO ADD : batch size for instance
300
}
301     
302     public Object JavaDoc getXMLProperty(String JavaDoc propertyId) throws XMLDBCNotRecognizedException
303     {
304         throw new UnsupportedOperationException JavaDoc("Not supported yet");
305         // TO ADD : batch size for instance
306
}
307     
308     public String JavaDoc[] getXMLFeatureList()
309     {
310         throw new UnsupportedOperationException JavaDoc("Not supported yet");
311         // TO ADD : removeWS for instance
312
}
313     
314     public void setXMLFeature(String JavaDoc featureId, boolean state) throws XMLDBCNotRecognizedException, XMLDBCNotSupportedException
315     {
316         throw new XMLDBCNotSupportedException("Not supported yet");
317         // TO ADD : removeWS for instance
318
}
319     
320     public boolean getXMLFeature(String JavaDoc featureId) throws XMLDBCNotRecognizedException
321     {
322         throw new UnsupportedOperationException JavaDoc("Not supported yet");
323         // TO ADD : removeWS for instance
324
}
325     
326     public String JavaDoc[] getXMLPropertyList()
327     {
328         throw new UnsupportedOperationException JavaDoc("Not supported yet");
329         // TO ADD : batch size for instance
330
}
331     
332 }
333
334
335
Popular Tags