コード例 #1
0
  /*
   *  (non-Javadoc)
   * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
   */
  public void endElement(String uri, String localName, String qName) throws SAXException {
    try {
      // ensure context matches parse
      ElementContext context = (ElementContext) contextStack.pop();
      QName elementName = decodeQName(QName.createQName(qName, importResolver));
      if (!context.getElementName().equals(elementName)) {
        throw new InvalidSerializedDataException(
            "Expected element " + context.getElementName() + " but was " + elementName);
      }

      // signal end of node
      NodeContext nodeContext = (NodeContext) context;
      nodeContext.getImporter().childrenImported(nodeContext.getNodeRef());
    } catch (Exception e) {
      throw new SAXException("Failed to process element " + qName, e);
    }
  }
コード例 #2
0
  /*
   *  (non-Javadoc)
   * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
   */
  public void startElement(String uri, String localName, String qName, Attributes atts)
      throws SAXException {
    try {
      // construct qname for element
      QName elementName = decodeQName(QName.createQName(qName, importResolver));

      // setup parent context
      ParentContext parentContext = null;
      if (contextStack.empty()) {
        // create root parent context
        parentContext = new ParentContext(elementName, dictionaryService, importer);
      } else {
        // create parent context
        NodeContext parentNode = (NodeContext) contextStack.peek();
        parentContext = new ParentContext(elementName, parentNode);
      }

      // create node context
      NodeContext node = new NodeContext(elementName, parentContext, null);
      node.setChildName(elementName.toPrefixString(importResolver));
      contextStack.push(node);

      // process node properties
      for (int i = 0; i < atts.getLength(); i++) {
        QName propName = decodeQName(QName.createQName(atts.getURI(i), atts.getLocalName(i)));
        String value = atts.getValue(i);

        //
        // process "well-known" properties
        //

        if (propName.equals(JCRPrimaryTypeProperty.PROPERTY_NAME)) {
          // primary type
          QName primaryTypeQName = QName.createQName(value, importResolver);
          TypeDefinition typeDef = dictionaryService.getType(primaryTypeQName);
          if (typeDef == null) {
            throw new InvalidTypeException(primaryTypeQName);
          }
          node.setTypeDefinition(typeDef);
        } else if (propName.equals(JCRMixinTypesProperty.PROPERTY_NAME)) {
          // aspects
          String[] aspects = value.split(" ");
          for (String aspect : aspects) {
            // ignore JCR specific aspects
            QName aspectQName = QName.createQName(aspect, importResolver);
            if (!(JCRNamespace.JCR_URI.equals(aspectQName.getNamespaceURI())
                || JCRNamespace.MIX_URI.equals(aspectQName.getNamespaceURI()))) {
              AspectDefinition aspectDef = dictionaryService.getAspect(aspectQName);
              if (aspectDef == null) {
                throw new InvalidTypeException(aspectQName);
              }
              node.addAspect(aspectDef);
            }
          }
        } else if (JCRUUIDProperty.PROPERTY_NAME.equals(propName)) {
          node.setUUID(value);
        }

        //
        // Note: ignore JCR specific properties
        //

        else if (JCRNamespace.JCR_URI.equals(propName.getNamespaceURI())) {
        }

        //
        // process all other properties
        //

        else {
          // determine type of property
          PropertyDefinition propDef = dictionaryService.getProperty(propName);
          if (propDef == null) {
            throw new ImporterException(
                "Property " + propName + " is not known to the repository data dictionary");
          }
          DataTypeDefinition dataTypeDef = propDef.getDataType();

          // extract values from node xml attribute
          String[] propValues = null;
          PropertyContext propertyContext =
              new PropertyContext(elementName, node, propName, dataTypeDef.getName());
          if (dataTypeDef.getName().equals(DataTypeDefinition.CONTENT)) {
            // Note: we only support single valued content properties
            propValues = new String[] {value};
          } else {
            // attempt to split multi-value properties
            propValues = value.split(" ");
          }

          // extract values appropriately
          for (String propValue : propValues) {
            propertyContext.startValue();
            propertyContext.appendCharacters(propValue.toCharArray(), 0, propValue.length());
            propertyContext.endValue();
          }

          // add each value to the node
          if (propertyContext.isMultiValue()) {
            node.addPropertyCollection(propName);
          }
          List<StringBuffer> nodeValues = propertyContext.getValues();
          for (StringBuffer nodeValue : nodeValues) {
            // first, cast value to appropriate type (using JCR converters)
            Serializable objVal =
                (Serializable)
                    session.getTypeConverter().convert(dataTypeDef, nodeValue.toString());
            String strValue = DefaultTypeConverter.INSTANCE.convert(String.class, objVal);
            node.addProperty(propName, strValue);
          }
        }
      }

      // import node
      NodeRef nodeRef = node.getImporter().importNode(node);
      node.setNodeRef(nodeRef);
    } catch (Exception e) {
      throw new SAXException("Failed to process element " + qName, e);
    }
  }