1 17 package org.alfresco.jcr.importer; 18 19 import java.io.InputStream ; 20 import java.util.ArrayList ; 21 import java.util.Collection ; 22 import java.util.List ; 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 ; 32 import org.xml.sax.Locator ; 33 import org.xml.sax.SAXException ; 34 import org.xml.sax.SAXParseException ; 35 import org.xml.sax.helpers.NamespaceSupport ; 36 37 38 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 56 public JCRImportHandler(SessionImpl session) 57 { 58 this.session = session; 59 this.namespaceContext = new NamespaceContext(); 60 } 61 62 66 public void setImporter(Importer importer) 67 { 68 this.importer = importer; 69 } 70 71 75 public InputStream importStream(String content) 76 { 77 return targetHandler.importStream(content); 78 } 79 80 84 public void setDocumentLocator(Locator locator) 85 { 86 } 88 89 93 public void startDocument() throws SAXException  94 { 95 namespaceContext.reset(); 96 } 97 98 102 public void endDocument() throws SAXException  103 { 104 targetHandler.endDocument(); 105 } 106 107 111 public void startPrefixMapping(String prefix, String uri) throws SAXException  112 { 113 NamespacePrefixResolver resolver = session.getNamespaceResolver(); 115 Collection <String > 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 namespaceContext.registerPrefix(prefix, uri); 123 } 124 125 129 public void endPrefixMapping(String prefix) throws SAXException  130 { 131 } 132 133 137 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException  138 { 139 namespaceContext.pushContext(); 140 141 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 163 public void endElement(String uri, String localName, String qName) throws SAXException  164 { 165 targetHandler.endElement(uri, localName, qName); 166 namespaceContext.popContext(); 167 } 168 169 173 public void characters(char[] ch, int start, int length) throws SAXException  174 { 175 targetHandler.characters(ch, start, length); 176 } 177 178 182 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException  183 { 184 targetHandler.characters(ch, start, length); 185 } 186 187 191 public void processingInstruction(String target, String data) throws SAXException  192 { 193 targetHandler.processingInstruction(target, data); 194 } 195 196 200 public void skippedEntity(String name) throws SAXException  201 { 202 targetHandler.skippedEntity(name); 203 } 204 205 209 public void warning(SAXParseException exception) throws SAXException  210 { 211 targetHandler.warning(exception); 212 } 213 214 218 public void error(SAXParseException exception) throws SAXException  219 { 220 targetHandler.error(exception); 221 } 222 223 227 public void fatalError(SAXParseException exception) throws SAXException  228 { 229 targetHandler.fatalError(exception); 230 } 231 232 233 239 private static class NamespaceContext implements NamespacePrefixResolver 240 { 241 private final NamespaceSupport context; 242 private static final String REMAPPED_DEFAULT_URI = " "; 243 244 245 248 private NamespaceContext() 249 { 250 context = new NamespaceSupport (); 251 } 252 253 256 private void reset() 257 { 258 context.reset(); 259 } 260 261 264 private void pushContext() 265 { 266 context.pushContext(); 267 } 268 269 272 private void popContext() 273 { 274 context.popContext(); 275 } 276 277 284 private boolean registerPrefix(String prefix, String 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 297 public String getNamespaceURI(String prefix) throws org.alfresco.service.namespace.NamespaceException 298 { 299 String 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 315 public Collection <String > getPrefixes(String namespaceURI) throws org.alfresco.service.namespace.NamespaceException 316 { 317 if (NamespaceService.DEFAULT_URI.equals(namespaceURI)) 318 { 319 namespaceURI = REMAPPED_DEFAULT_URI; 320 } 321 String 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 <String > prefixes = new ArrayList <String >(1); 334 prefixes.add(prefix); 335 return prefixes; 336 } 337 338 342 public Collection <String > getPrefixes() 343 { 344 return null; 346 } 347 348 352 public Collection <String > getURIs() 353 { 354 return null; 356 } 357 } 358 359 } 360 | Popular Tags |