KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > importer > JCRImportHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.importer;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.alfresco.jcr.dictionary.JCRNamespace;
25 import org.alfresco.jcr.session.SessionImpl;
26 import org.alfresco.repo.importer.ImportContentHandler;
27 import org.alfresco.repo.importer.Importer;
28 import org.alfresco.service.cmr.view.ImporterException;
29 import org.alfresco.service.namespace.NamespacePrefixResolver;
30 import org.alfresco.service.namespace.NamespaceService;
31 import org.xml.sax.Attributes JavaDoc;
32 import org.xml.sax.Locator JavaDoc;
33 import org.xml.sax.SAXException JavaDoc;
34 import org.xml.sax.SAXParseException JavaDoc;
35 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
36
37
38 /**
39  * Import Handler that is sensitive to Document and System View XML schemas.
40  *
41  * @author David Caruana
42  */

43 public class JCRImportHandler implements ImportContentHandler
44 {
45     private Importer importer;
46     private SessionImpl session;
47     private NamespaceContext namespaceContext;
48     private ImportContentHandler targetHandler = null;
49     
50     
51     /**
52      * Construct
53      *
54      * @param session
55      */

56     public JCRImportHandler(SessionImpl session)
57     {
58         this.session = session;
59         this.namespaceContext = new NamespaceContext();
60     }
61     
62     /*
63      * (non-Javadoc)
64      * @see org.alfresco.repo.importer.ImportContentHandler#setImporter(org.alfresco.repo.importer.Importer)
65      */

66     public void setImporter(Importer importer)
67     {
68         this.importer = importer;
69     }
70
71     /*
72      * (non-Javadoc)
73      * @see org.alfresco.repo.importer.ImportContentHandler#importStream(java.lang.String)
74      */

75     public InputStream JavaDoc importStream(String JavaDoc content)
76     {
77         return targetHandler.importStream(content);
78     }
79
80     /*
81      * (non-Javadoc)
82      * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
83      */

84     public void setDocumentLocator(Locator JavaDoc locator)
85     {
86         // NOOP
87
}
88
89     /*
90      * (non-Javadoc)
91      * @see org.xml.sax.ContentHandler#startDocument()
92      */

93     public void startDocument() throws SAXException JavaDoc
94     {
95         namespaceContext.reset();
96     }
97
98     /*
99      * (non-Javadoc)
100      * @see org.xml.sax.ContentHandler#endDocument()
101      */

102     public void endDocument() throws SAXException JavaDoc
103     {
104         targetHandler.endDocument();
105     }
106
107     /*
108      * (non-Javadoc)
109      * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
110      */

111     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc
112     {
113         // ensure uri has been registered
114
NamespacePrefixResolver resolver = session.getNamespaceResolver();
115         Collection JavaDoc<String JavaDoc> uris = resolver.getURIs();
116         if (!uris.contains(uri))
117         {
118             throw new ImporterException("Namespace URI " + uri + " has not been registered with the repository");
119         }
120         
121         // register prefix within this namespace context
122
namespaceContext.registerPrefix(prefix, uri);
123     }
124
125     /*
126      * (non-Javadoc)
127      * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
128      */

129     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc
130     {
131     }
132
133     /*
134      * (non-Javadoc)
135      * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
136      */

137     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc
138     {
139         namespaceContext.pushContext();
140
141         // determine content handler based on first element of document
142
if (targetHandler == null)
143         {
144             if (JCRNamespace.SV_URI.equals(uri))
145             {
146                 targetHandler = new JCRSystemXMLHandler(session, namespaceContext);
147             }
148             else
149             {
150                 targetHandler = new JCRDocXMLHandler(session, namespaceContext);
151             }
152             targetHandler.setImporter(importer);
153             targetHandler.startDocument();
154         }
155         
156         targetHandler.startElement(uri, localName, qName, atts);
157     }
158
159     /*
160      * (non-Javadoc)
161      * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
162      */

163     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc
164     {
165         targetHandler.endElement(uri, localName, qName);
166         namespaceContext.popContext();
167     }
168
169     /*
170      * (non-Javadoc)
171      * @see org.xml.sax.ContentHandler#characters(char[], int, int)
172      */

173     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc
174     {
175         targetHandler.characters(ch, start, length);
176     }
177
178     /*
179      * (non-Javadoc)
180      * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
181      */

182     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException JavaDoc
183     {
184         targetHandler.characters(ch, start, length);
185     }
186
187     /*
188      * (non-Javadoc)
189      * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
190      */

191     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc
192     {
193         targetHandler.processingInstruction(target, data);
194     }
195
196     /*
197      * (non-Javadoc)
198      * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
199      */

200     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc
201     {
202         targetHandler.skippedEntity(name);
203     }
204
205     /*
206      * (non-Javadoc)
207      * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
208      */

209     public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc
210     {
211         targetHandler.warning(exception);
212     }
213
214     /*
215      * (non-Javadoc)
216      * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
217      */

218     public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc
219     {
220         targetHandler.error(exception);
221     }
222
223     /*
224      * (non-Javadoc)
225      * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
226      */

227     public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc
228     {
229         targetHandler.fatalError(exception);
230     }
231
232     
233     /**
234      * Namespace Context
235      *
236      * Implementation supported by NamespaceSupport which itself does not
237      * handle empty uri registration.
238      */

239     private static class NamespaceContext implements NamespacePrefixResolver
240     {
241         private final NamespaceSupport JavaDoc context;
242         private static final String JavaDoc REMAPPED_DEFAULT_URI = " ";
243
244
245         /**
246          * Construct
247          */

248         private NamespaceContext()
249         {
250             context = new NamespaceSupport JavaDoc();
251         }
252
253         /**
254          * Clear namespace declarations
255          */

256         private void reset()
257         {
258             context.reset();
259         }
260
261         /**
262          * Push a new Namespace Context
263          */

264         private void pushContext()
265         {
266             context.pushContext();
267         }
268
269         /**
270          * Pop a Namespace Context
271          */

272         private void popContext()
273         {
274             context.popContext();
275         }
276
277         /**
278          * Register a namespace prefix
279          *
280          * @param prefix
281          * @param uri
282          * @return true => legal prefix; false => illegal prefix
283          */

284         private boolean registerPrefix(String JavaDoc prefix, String JavaDoc uri)
285         {
286             if (NamespaceService.DEFAULT_URI.equals(uri))
287             {
288                 uri = REMAPPED_DEFAULT_URI;
289             }
290             return context.declarePrefix(prefix, uri);
291         }
292
293         /*
294          * (non-Javadoc)
295          * @see org.alfresco.service.namespace.NamespacePrefixResolver#getNamespaceURI(java.lang.String)
296          */

297         public String JavaDoc getNamespaceURI(String JavaDoc prefix) throws org.alfresco.service.namespace.NamespaceException
298         {
299             String JavaDoc uri = context.getURI(prefix);
300             if (uri == null)
301             {
302                 throw new org.alfresco.service.namespace.NamespaceException("Namespace prefix " + prefix + " not registered.");
303             }
304             if (REMAPPED_DEFAULT_URI.equals(uri))
305             {
306                 return NamespaceService.DEFAULT_URI;
307             }
308             return uri;
309         }
310
311         /*
312          * (non-Javadoc)
313          * @see org.alfresco.service.namespace.NamespacePrefixResolver#getPrefixes(java.lang.String)
314          */

315         public Collection JavaDoc<String JavaDoc> getPrefixes(String JavaDoc namespaceURI) throws org.alfresco.service.namespace.NamespaceException
316         {
317             if (NamespaceService.DEFAULT_URI.equals(namespaceURI))
318             {
319                 namespaceURI = REMAPPED_DEFAULT_URI;
320             }
321             String JavaDoc prefix = context.getPrefix(namespaceURI);
322             if (prefix == null)
323             {
324                 if (namespaceURI.equals(context.getURI(NamespaceService.DEFAULT_PREFIX)))
325                 {
326                     prefix = NamespaceService.DEFAULT_PREFIX;
327                 }
328                 else
329                 {
330                     throw new org.alfresco.service.namespace.NamespaceException("Namespace URI " + namespaceURI + " not registered.");
331                 }
332             }
333             List JavaDoc<String JavaDoc> prefixes = new ArrayList JavaDoc<String JavaDoc>(1);
334             prefixes.add(prefix);
335             return prefixes;
336         }
337
338         /*
339          * (non-Javadoc)
340          * @see org.alfresco.service.namespace.NamespacePrefixResolver#getPrefixes()
341          */

342         public Collection JavaDoc<String JavaDoc> getPrefixes()
343         {
344             // NOTE: not required in this context
345
return null;
346         }
347
348         /*
349          * (non-Javadoc)
350          * @see org.alfresco.service.namespace.NamespacePrefixResolver#getURIs()
351          */

352         public Collection JavaDoc<String JavaDoc> getURIs()
353         {
354             // NOTE: not required in this context
355
return null;
356         }
357     }
358     
359 }
360
Popular Tags