1 17 package org.alfresco.jcr.importer; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.FileNotFoundException ; 22 import java.io.InputStream ; 23 import java.io.Serializable ; 24 import java.util.List ; 25 import java.util.Stack ; 26 27 import javax.jcr.InvalidSerializedDataException; 28 29 import org.alfresco.jcr.dictionary.JCRNamespace; 30 import org.alfresco.jcr.item.property.JCRMixinTypesProperty; 31 import org.alfresco.jcr.item.property.JCRPrimaryTypeProperty; 32 import org.alfresco.jcr.item.property.JCRUUIDProperty; 33 import org.alfresco.jcr.session.SessionImpl; 34 import org.alfresco.repo.importer.ImportContentHandler; 35 import org.alfresco.repo.importer.Importer; 36 import org.alfresco.repo.importer.view.ElementContext; 37 import org.alfresco.repo.importer.view.NodeContext; 38 import org.alfresco.repo.importer.view.ParentContext; 39 import org.alfresco.service.cmr.dictionary.AspectDefinition; 40 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 41 import org.alfresco.service.cmr.dictionary.DictionaryService; 42 import org.alfresco.service.cmr.dictionary.InvalidTypeException; 43 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 44 import org.alfresco.service.cmr.dictionary.TypeDefinition; 45 import org.alfresco.service.cmr.repository.NodeRef; 46 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 47 import org.alfresco.service.cmr.view.ImporterException; 48 import org.alfresco.service.namespace.NamespacePrefixResolver; 49 import org.alfresco.service.namespace.QName; 50 import org.alfresco.util.Base64; 51 import org.alfresco.util.ISO9075; 52 import org.xml.sax.Attributes ; 53 import org.xml.sax.Locator ; 54 import org.xml.sax.SAXException ; 55 import org.xml.sax.SAXParseException ; 56 57 58 63 public class JCRDocXMLHandler implements ImportContentHandler 64 { 65 private Importer importer; 66 private SessionImpl session; 67 private DictionaryService dictionaryService; 68 private NamespacePrefixResolver importResolver; 69 private Stack <ElementContext> contextStack = new Stack <ElementContext>(); 70 71 72 78 public JCRDocXMLHandler(SessionImpl session, NamespacePrefixResolver importResolver) 79 { 80 this.session = session; 81 this.importResolver = importResolver; 82 this.dictionaryService = session.getRepositoryImpl().getServiceRegistry().getDictionaryService(); 83 } 84 85 89 public void setImporter(Importer importer) 90 { 91 this.importer = importer; 92 } 93 94 98 public InputStream importStream(String content) 99 { 100 File contentFile = new File (content); 101 try 102 { 103 FileInputStream contentStream = new FileInputStream (contentFile); 104 return new Base64.InputStream(contentStream, Base64.DECODE | Base64.DONT_BREAK_LINES); 105 } 106 catch (FileNotFoundException e) 107 { 108 throw new ImporterException("Failed to retrieve import input stream on temporary content file " + content); 109 } 110 } 111 112 116 public void setDocumentLocator(Locator locator) 117 { 118 } 120 121 125 public void startDocument() throws SAXException 126 { 127 } 129 130 134 public void endDocument() throws SAXException 135 { 136 } 138 139 143 public void startPrefixMapping(String prefix, String uri) throws SAXException 144 { 145 } 147 148 152 public void endPrefixMapping(String prefix) throws SAXException 153 { 154 } 156 157 161 public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException 162 { 163 try 164 { 165 QName elementName = decodeQName(QName.createQName(qName, importResolver)); 167 168 ParentContext parentContext = null; 170 if (contextStack.empty()) 171 { 172 parentContext = new ParentContext(elementName, dictionaryService, importer); 174 } 175 else 176 { 177 NodeContext parentNode = (NodeContext)contextStack.peek(); 179 parentContext = new ParentContext(elementName, parentNode); 180 } 181 182 NodeContext node = new NodeContext(elementName, parentContext, null); 184 node.setChildName(elementName.toPrefixString(importResolver)); 185 contextStack.push(node); 186 187 for (int i = 0; i < atts.getLength(); i++) 189 { 190 QName propName = decodeQName(QName.createQName(atts.getURI(i), atts.getLocalName(i))); 191 String value = atts.getValue(i); 192 193 197 if (propName.equals(JCRPrimaryTypeProperty.PROPERTY_NAME)) 198 { 199 QName primaryTypeQName = QName.createQName(value, importResolver); 201 TypeDefinition typeDef = dictionaryService.getType(primaryTypeQName); 202 if (typeDef == null) 203 { 204 throw new InvalidTypeException(primaryTypeQName); 205 } 206 node.setTypeDefinition(typeDef); 207 } 208 else if (propName.equals(JCRMixinTypesProperty.PROPERTY_NAME)) 209 { 210 String [] aspects = value.split(" "); 212 for (String aspect : aspects) 213 { 214 QName aspectQName = QName.createQName(aspect, importResolver); 216 if (!(JCRNamespace.JCR_URI.equals(aspectQName.getNamespaceURI()) || 217 JCRNamespace.MIX_URI.equals(aspectQName.getNamespaceURI()))) 218 { 219 AspectDefinition aspectDef = dictionaryService.getAspect(aspectQName); 220 if (aspectDef == null) 221 { 222 throw new InvalidTypeException(aspectQName); 223 } 224 node.addAspect(aspectDef); 225 } 226 } 227 } 228 else if (JCRUUIDProperty.PROPERTY_NAME.equals(propName)) 229 { 230 node.setUUID(value); 231 } 232 233 237 else if (JCRNamespace.JCR_URI.equals(propName.getNamespaceURI())) 238 { 239 } 240 241 245 else 246 { 247 PropertyDefinition propDef = dictionaryService.getProperty(propName); 249 if (propDef == null) 250 { 251 throw new ImporterException("Property " + propName + " is not known to the repository data dictionary"); 252 } 253 DataTypeDefinition dataTypeDef = propDef.getDataType(); 254 255 String [] propValues = null; 257 PropertyContext propertyContext = new PropertyContext(elementName, node, propName, dataTypeDef.getName()); 258 if (dataTypeDef.getName().equals(DataTypeDefinition.CONTENT)) 259 { 260 propValues = new String [] { value }; 262 } 263 else 264 { 265 propValues = value.split(" "); 267 } 268 269 for (String propValue : propValues) 271 { 272 propertyContext.startValue(); 273 propertyContext.appendCharacters(propValue.toCharArray(), 0, propValue.length()); 274 propertyContext.endValue(); 275 } 276 277 if (propertyContext.isMultiValue()) 279 { 280 node.addPropertyCollection(propName); 281 } 282 List <StringBuffer > nodeValues = propertyContext.getValues(); 283 for (StringBuffer nodeValue : nodeValues) 284 { 285 Serializable objVal = (Serializable )session.getTypeConverter().convert(dataTypeDef, nodeValue.toString()); 287 String strValue = DefaultTypeConverter.INSTANCE.convert(String .class, objVal); 288 node.addProperty(propName, strValue); 289 } 290 } 291 } 292 293 NodeRef nodeRef = node.getImporter().importNode(node); 295 node.setNodeRef(nodeRef); 296 } 297 catch(Exception e) 298 { 299 throw new SAXException ("Failed to process element " + qName, e); 300 } 301 } 302 303 307 public void endElement(String uri, String localName, String qName) throws SAXException 308 { 309 try 310 { 311 ElementContext context = (ElementContext)contextStack.pop(); 313 QName elementName = QName.createQName(qName, importResolver); 314 if (!context.getElementName().equals(elementName)) 315 { 316 throw new InvalidSerializedDataException("Expected element " + context.getElementName() + " but was " + elementName); 317 } 318 319 NodeContext nodeContext = (NodeContext)context; 321 nodeContext.getImporter().childrenImported(nodeContext.getNodeRef()); 322 } 323 catch(Exception e) 324 { 325 throw new SAXException ("Failed to process element " + qName, e); 326 } 327 } 328 329 333 public void characters(char[] ch, int start, int length) throws SAXException 334 { 335 } 336 337 341 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException 342 { 343 } 344 345 349 public void processingInstruction(String target, String data) throws SAXException 350 { 351 } 352 353 357 public void skippedEntity(String name) throws SAXException 358 { 359 } 360 361 365 public void warning(SAXParseException exception) throws SAXException 366 { 367 } 368 369 373 public void error(SAXParseException exception) throws SAXException 374 { 375 } 376 377 381 public void fatalError(SAXParseException exception) throws SAXException 382 { 383 } 384 385 391 private QName decodeQName(QName name) 392 { 393 return QName.createQName(name.getNamespaceURI(), ISO9075.decode(name.getLocalName())); 394 } 395 396 } 397 | Popular Tags |