Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   * 
  *   http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.    
  */
 
 package org.apache.tuscany.sca.assembly.xml;
 
 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
 import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
 import static org.apache.tuscany.sca.assembly.xml.Constants.CALLBACK;
 import static org.apache.tuscany.sca.assembly.xml.Constants.CALLBACK_QNAME;
 import static org.apache.tuscany.sca.assembly.xml.Constants.COMPONENT_TYPE;
 import static org.apache.tuscany.sca.assembly.xml.Constants.COMPONENT_TYPE_QNAME;
 import static org.apache.tuscany.sca.assembly.xml.Constants.ELEMENT;
 import static org.apache.tuscany.sca.assembly.xml.Constants.IMPLEMENTATION;
 import static org.apache.tuscany.sca.assembly.xml.Constants.MANY;
 import static org.apache.tuscany.sca.assembly.xml.Constants.MUST_SUPPLY;
 import static org.apache.tuscany.sca.assembly.xml.Constants.NAME;
 import static org.apache.tuscany.sca.assembly.xml.Constants.OPERATION_QNAME;
 import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY;
 import static org.apache.tuscany.sca.assembly.xml.Constants.PROPERTY_QNAME;
 import static org.apache.tuscany.sca.assembly.xml.Constants.REFERENCE;
 import static org.apache.tuscany.sca.assembly.xml.Constants.REFERENCE_QNAME;
 import static org.apache.tuscany.sca.assembly.xml.Constants.SERVICE;
 import static org.apache.tuscany.sca.assembly.xml.Constants.SERVICE_QNAME;
 import static org.apache.tuscany.sca.assembly.xml.Constants.TYPE;
 import static org.apache.tuscany.sca.assembly.xml.Constants.EXTENSION;
 import static org.apache.tuscany.sca.assembly.xml.Constants.EXTENSION_QNAME;
 
 
A componentType processor.

Version:
$Rev: 979205 $ $Date: 2010-07-26 12:06:29 +0300 (Mon, 26 Jul 2010) $
 
 	
     private StAXHelper staxHelper;
    
    
Constructs a new componentType processor.

Parameters:
modelFactories
extensionProcessor
extensionAttributeProcessor
monitor
 
     public ComponentTypeProcessor(ExtensionPointRegistry extensionPoints,
     //public ComponentTypeProcessor(FactoryExtensionPoint modelFactories,
                                   StAXArtifactProcessor extensionProcessor,
                                   StAXAttributeProcessor extensionAttributeProcessor) {
         super(modelFactories(extensionPoints), extensionProcessor);
         
         // 
         = StAXHelper.getInstance(extensionPoints);
    }
    
    public ComponentType read(XMLStreamReader readerProcessorContext contextthrows ContributionReadException {
        ComponentType componentType = null;
        Service service = null;
        Reference reference = null;
        Contract contract = null;
        Property property = null;
        Callback callback = null;
        QName name = null;
        
        try {
            // Read the componentType document
            while (reader.hasNext()) {
                int event = reader.getEventType();
                switch (event) {
                    case :
                        name = reader.getName();
    
                        if (..equals(name)) {
    
                            // Read a <componentType>
                            componentType = .createComponentType();
    
                        } else if (..equals(name)) {
    
                            // Read a <service>
                            service = .createService();
                            contract = service;
                            service.setName(getString(reader.));
                            componentType.getServices().add(service);
                            .readPolicies(servicereader);
    
                        } else if (..equals(name)) {
    
                            // Read a <reference>
                            reference = .createReference();
                            contract = reference;
                            reference.setName(getString(reader.));
                            reference.setWiredByImpl(getBoolean(reader.));
                            readMultiplicity(referencereader);
                            readTargets(referencereader);
                            componentType.getReferences().add(reference);
                            .readPolicies(referencereader);
    
                        } else if (..equals(name)) {
    
                            // Read a <property>
                            property = .createProperty();
                            readAbstractProperty(propertyreadercontext);
                            .readPolicies(propertyreader);
                            
                            // Read the property value
                            Document value = readPropertyValue(property.getXSDElement(), property.getXSDType(), property.isMany(), readercontext);
                            property.setValue(value);
                            
                            componentType.getProperties().add(property);
                            
                        } else if (..equals(name)) {
                            
                            // Read an <implementation> element
                            .readPolicies(componentTypereader);
                            
                        } else if (..equals(name)) {
    
                            // Read a <callback>
                            callback = .createCallback();
                            contract.setCallback(callback);
                            .readPolicies(callbackreader);
    
                        } else if (.equals(name)) {
    
                            // Read an <operation>
                            Operation operation = new OperationImpl();
                            operation.setName(getString(reader));
                            operation.setUnresolved(true);
                            if (callback != null) {
                                .readPolicies(callbackoperationreader);
                            } else {
                                .readPolicies(contractoperationreader);
                            }
                        } else if(.equals(name)) {
                            // Handle <extension>
                            //ignore element as this is a wrapper for extensibility
                            break;
                        } else {
                        
    
                            // Read an extension element
                            Object extension = .read(readercontext);
                            if (extension != null) {
                                if (extension instanceof InterfaceContract) {
    
                                    // <service><interface> and <reference><interface>
                                    contract.setInterfaceContract((InterfaceContract)extension);
    
                                } else if (extension instanceof Binding) {
    
                                    // <service><binding> and <reference><binding>
                                    if (callback != null) {
                                        callback.getBindings().add((Binding)extension);
                                    } else {
                                        contract.getBindings().add((Binding)extension);
                                    }
                                } else {
                                    
                                    // Add the extension element to the current element
                                    if (callback != null) {
                                        callback.getExtensions().add(extension);
                                    } else if (contract != null) {
                                        contract.getExtensions().add(extension);
                                    } else if (property != null) {
                                        property.getExtensions().add(extension);
                                    } else {
                                        if (componentType instanceof Extensible) {
                                            ((Extensible)componentType).getExtensions().add(extension);
                                        }
                                    }
                                }
                            }
                        }
                        break;
    
                    case :
                        name = reader.getName();
    
                        // Clear current state when reading reaching end element
                        if (.equals(name)) {
                            service = null;
                            contract = null;
                        } else if (.equals(name)) {
                            reference = null;
                            contract = null;
                        } else if (.equals(name)) {
                            property = null;
                        } else if (.equals(name)) {
                            callback = null;
                        }
                        break;
                }
                
                // Read the next element
                if (reader.hasNext()) {
                    reader.next();
                }
            }
        }
        catch (XMLStreamException e) {
            ContributionReadException ex = new ContributionReadException(e);
            error(context.getMonitor(), "XMLStreamException"readerex);
        }
        
        return componentType;
    }
    
    public void write(ComponentType componentTypeXMLStreamWriter writerProcessorContext contextthrows ContributionWriteExceptionXMLStreamException {
        
        // Write <componentType> element
        writeStartDocument(writer);
        // Write <service> elements
        for (Service service : componentType.getServices()) {
            writeStart(writernew XAttr(service.getName()),
                       .writePolicies(service));
            if (service.getInterfaceContract() != null) {
                .write(service.getInterfaceContract(), writercontext);
            }
            
            for (Binding bindingservice.getBindings()) {
                .write(bindingwritercontext);
            }
            
            if (service.getCallback() != null) {
                Callback callback = service.getCallback();
                writeStart(writer.writePolicies(callback));
                for (Binding bindingcallback.getBindings()) {
                    .write(bindingwritercontext);
                }
                for (Object extensioncallback.getExtensions()) {
                    .write(extensionwritercontext);
                }
                
                writeEnd(writer);
            }
            
            this.writeExtendedElements(writerservicecontext);
            
            writeEnd(writer);
        }
        // Write <reference> elements
        for (Reference reference : componentType.getReferences()) {
            
            writeStart(writer,
                  new XAttr(reference.getName()),
                  writeMultiplicity(reference),
                  writeTargets(reference),
                  .writePolicies(reference));
            .write(reference.getInterfaceContract(), writercontext);
            
            for (Binding bindingreference.getBindings()) {
                .write(bindingwritercontext);
            }
            
            if (reference.getCallback() != null) {
                Callback callback = reference.getCallback();
                writeStart(writer,
                           .writePolicies(callback));
                for (Binding bindingcallback.getBindings()) {
                    .write(bindingwritercontext);
                }
                for (Object extensioncallback.getExtensions()) {
                    .write(extensionwritercontext);
                }
                
                writeEnd(writer);
            }
            this.writeExtendedElements(writerreferencecontext);
            
            writeEnd(writer);
        }
        // Write <property> elements
        for (Property property : componentType.getProperties()) {
            writeStart(writer,
                       ,
                       new XAttr(property.getName()),
                       new XAttr(property.isMustSupply()),
                       new XAttr(property.isMany()),
                       new XAttr(property.getXSDType()),
                       new XAttr(property.getXSDElement()),
                       .writePolicies(property));
            // Write property value
            writePropertyValue(property.getValue(), property.getXSDElement(), property.getXSDType(), writer);
            // Write extensions
            for (Object extension : property.getExtensions()) {
                .write(extensionwritercontext);
            }
            writeEnd(writer);
        }
        // Write extension elements
        if (componentType instanceof Extensible) {
            for (Object extension: ((Extensible)componentType).getExtensions()) {
                .write(extensionwritercontext);
            }
        }
        
        // Write <implementation> elements if the componentType has
        // any intents or policySets
        boolean writeImplementation = false;
        if (componentType instanceof PolicySubject) {
            if (!((PolicySubject)componentType).getRequiredIntents().isEmpty()) {
                writeImplementation = true;
            }
        }
        if (componentType instanceof PolicySubject) {
            if (!((PolicySubject)componentType).getPolicySets().isEmpty()) {
                writeImplementation = true;
            }
        }
        if (writeImplementation) {
            writeStart(writer,
                       .writePolicies(componentType));
        }
        
        writeEndDocument(writer);
    }
    
    
Write the value of a property - override to use correct method of creating an XMLStreamReader

Parameters:
document
element
type
writer
Throws:
javax.xml.stream.XMLStreamException
    protected void writePropertyValue(Object propertyValueQName elementQName typeXMLStreamWriter writer)
        throws XMLStreamException {
        if (propertyValue instanceof Document) {
            Document document = (Document)propertyValue;
            NodeList nodeList = document.getDocumentElement().getChildNodes();
            for (int item = 0; item < nodeList.getLength(); ++item) {
                Node node = nodeList.item(item);
                int nodeType = node.getNodeType();
                if (nodeType == .) {
                	// Correct way to create a reader for a node object...
                	XMLStreamReader reader = .createXMLStreamReader(node);
                    while (reader.hasNext()) {
                        switch (reader.next()) {
                            case .:
                                QName name = reader.getName();
                                writer.writeStartElement(name.getPrefix(), name.getLocalPart(), name.getNamespaceURI());
                                int namespaces = reader.getNamespaceCount();
                                for (int i = 0; i < namespacesi++) {
                                    String prefix = reader.getNamespacePrefix(i);
                                    String ns = reader.getNamespaceURI(i);
                                    writer.writeNamespace(prefixns);
                                }
                                if (!"".equals(name.getNamespaceURI())) {
                                    writer.writeNamespace(name.getPrefix(), name.getNamespaceURI());
                                }
                                // add the attributes for this element
                                namespaces = reader.getAttributeCount();
                                for (int i = 0; i < namespacesi++) {
                                    String ns = reader.getAttributeNamespace(i);
                                    String prefix = reader.getAttributePrefix(i);
                                    String qname = reader.getAttributeLocalName(i);
                                    String value = reader.getAttributeValue(i);
                                    writer.writeAttribute(prefixnsqnamevalue);
                                }
                                break;
                            case .:
                                writer.writeCData(reader.getText());
                                break;
                            case .:
                                writer.writeCharacters(reader.getText());
                                break;
                            case .:
                                writer.writeEndElement();
                                break;
                        }
                    }
                } else {
                    writer.writeCharacters(node.getTextContent());
                }
            }
        }
    } // end method writePropertyValue
    
    public void resolve(ComponentType componentTypeModelResolver resolverProcessorContext contextthrows ContributionResolveException {
        // Resolve component type services and references
        resolveContracts(componentType.getServices(), resolvercontext);
        resolveContracts(componentType.getReferences(), resolvercontext);
    }
    
    public QName getArtifactType() {
        return ;
    }
    
    public Class<ComponentTypegetModelType() {
        return ComponentType.class;
    }
    
    
Returns the model factory extension point to use.

Parameters:
extensionPoints
Returns:
    private static FactoryExtensionPoint modelFactories(ExtensionPointRegistry extensionPoints) {
        return extensionPoints.getExtensionPoint(FactoryExtensionPoint.class);
    }
New to GrepCode? Check out our FAQ X