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.contribution.processor;
 
 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
 import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
 
 
 import java.util.List;
 
 
A base class with utility methods for the other artifact processors in this module.

Version:
$Rev: 937321 $ $Date: 2010-04-23 16:01:54 +0100 (Fri, 23 Apr 2010) $
Tuscany.spi.extension.inheritfrom:
 
 public abstract class BaseStAXArtifactProcessor {
     private static String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912";
     private static String SCA11_TUSCANY_NS = "http://tuscany.apache.org/xmlns/sca/1.1";

    
The StAXHelper without states
 
     private static final StAXHelper helper = new StAXHelper(nullnullnull);
    
Returns a QName from a string.

Parameters:
reader
value
Returns:
 
     protected QName getQNameValue(XMLStreamReader readerString value) {
         return StAXHelper.getValueAsQName(readervalue);
     }

    
Returns the boolean value of an attribute.

Parameters:
reader
name
Returns:
 
     protected boolean getBoolean(XMLStreamReader readerString name) {
         Boolean attr = StAXHelper.getAttributeAsBoolean(readername);
         if (attr == null) {
             return false;
         } else {
             return attr.booleanValue();
         }
     }

    
Returns the QName value of an attribute.

Parameters:
reader
name
Returns:
 
     protected QName getQName(XMLStreamReader readerString name) {
         return StAXHelper.getAttributeAsQName(readername);
     }

    
Returns the value of an attribute as a list of QNames.

Parameters:
reader
name
Returns:
 
     protected List<QNamegetQNames(XMLStreamReader readerString name) {
         return StAXHelper.getAttributeAsQNames(readername);
     }

    
Returns the string value of an attribute.

Parameters:
reader
name
Returns:
    protected String getString(XMLStreamReader readerString name) {
        return StAXHelper.getAttributeAsString(readername);
    }
    
    
Returns the string value of an attribute.

Parameters:
reader
name
Returns:
    protected String getURIString(XMLStreamReader readerString name) {
        return StAXHelper.getAttributeAsURIString(readername);
    }    

    
Test if an attribute is explicitly set

Parameters:
reader
name
Returns:
    protected boolean isSet(XMLStreamReader readerString name) {
        return StAXHelper.isAttributePresent(readername);
    }

    
Returns the value of xsi:type attribute

Parameters:
reader The XML stream reader
Returns:
The QName of the type, if the attribute is not present, null is returned.
    protected QName getXSIType(XMLStreamReader reader) {
        return StAXHelper.getXSIType(reader);
    }

    
Parse the next child element.

Parameters:
reader
Returns:
Throws:
javax.xml.stream.XMLStreamException
    protected boolean nextChildElement(XMLStreamReader readerthrows XMLStreamException {
        while (reader.hasNext()) {
            int event = reader.next();
            if (event == ) {
                return false;
            }
            if (event == ) {
                return true;
            }
        }
        return false;
    }

    
Advance the stream to the next END_ELEMENT event skipping any nested content.

Parameters:
reader the reader to advance
Throws:
javax.xml.stream.XMLStreamException if there was a problem reading the stream
    protected void skipToEndElement(XMLStreamReader readerthrows XMLStreamException {
        StAXHelper.skipToEndElement(reader);
    }

    

Parameters:
writer
uri
Throws:
javax.xml.stream.XMLStreamException
    private String setPrefix(XMLStreamWriter writerString urithrows XMLStreamException {
        if (uri == null) {
            return null;
        }
        String prefix = writer.getPrefix(uri);
        if (prefix != null) {
            return null;
        } else {
            // Find an available prefix and bind it to the given URI
            NamespaceContext nsc = writer.getNamespaceContext();
            for (int i=1; ; i++) {
                prefix = "ns" + i;
                if (nsc.getNamespaceURI(prefix) == null) {
                    break;
                }
            }
            writer.setPrefix(prefixuri);
            return prefix;
        }
    }

    
Start an element.

Parameters:
uri
name
attrs
Throws:
javax.xml.stream.XMLStreamException
    protected void writeStart(XMLStreamWriter writerString uriString nameXAttr... attrs)
        throws XMLStreamException {
        .writeStartElement(writer""nameuri);
        writeAttributes(writerattrs);
    }

    
Start an element.

Parameters:
qname
attrs
Throws:
javax.xml.stream.XMLStreamException
    protected void writeStart(XMLStreamWriter writerQName qnameXAttr... attrsthrows XMLStreamException {
        writeStart(writerqname.getNamespaceURI(), qname.getLocalPart(), attrs);
    }

    
End an element.

Parameters:
writer
Throws:
javax.xml.stream.XMLStreamException
    protected void writeEnd(XMLStreamWriter writerthrows XMLStreamException {
        writer.writeEndElement();
    }

    
Start a document.

Parameters:
writer
Throws:
javax.xml.stream.XMLStreamException
    protected void writeStartDocument(XMLStreamWriter writerString uriString nameXAttr... attrsthrows XMLStreamException {
        writer.writeStartDocument();
        writer.setDefaultNamespace(uri);
        writeStart(writerurinameattrs);
        // writer.writeDefaultNamespace(uri);
    }

    
Start a document.

Parameters:
writer
qname
attrs
Throws:
javax.xml.stream.XMLStreamException
    protected void writeStartDocument(XMLStreamWriter writerQName qnameXAttr... attrsthrows XMLStreamException {
        writeStartDocument(writerqname.getNamespaceURI(), qname.getLocalPart(), attrs);
    }

    
End a document.

Parameters:
writer
Throws:
javax.xml.stream.XMLStreamException
    protected void writeEndDocument(XMLStreamWriter writerthrows XMLStreamException {
        writer.writeEndDocument();
    }

    
Write attributes to the current element.

Parameters:
writer
attrs
Throws:
javax.xml.stream.XMLStreamException
    protected void writeAttributes(XMLStreamWriter writerXAttr... attrsthrows XMLStreamException {
        for (XAttr attr : attrs) {
            if (attr != null)
                attr.write(writer);
        }
    }

    

Parameters:
reader
extensible
extensionAttributeProcessor
extensionAttributeProcessor
extensionFactory
context TODO
elementName
Throws:
ContributionReadException
javax.xml.stream.XMLStreamException
    protected void readExtendedAttributes(XMLStreamReader reader,
                                          Extensible extensible,
                                          StAXAttributeProcessor extensionAttributeProcessor,
                                          AssemblyFactory extensionFactoryProcessorContext contextthrows ContributionReadException,
        XMLStreamException {
        QName elementName = reader.getName();
        for (int a = 0; a < reader.getAttributeCount(); a++) {
            QName attributeName = reader.getAttributeName(a);
            if (attributeName.getNamespaceURI() != null && attributeName.getNamespaceURI().length() > 0) {
                if (!elementName.getNamespaceURI().equals(attributeName.getNamespaceURI())) {
                    Object attributeValue = extensionAttributeProcessor.read(attributeNamereadercontext);
                    Extension attributeExtension;
                    if (attributeValue instanceof Extension) {
                        attributeExtension = (Extension)attributeValue;
                    } else {
                        attributeExtension = extensionFactory.createExtension();
                        attributeExtension.setQName(attributeName);
                        attributeExtension.setValue(attributeValue);
                        attributeExtension.setAttribute(true);
                    }
                    extensible.getAttributeExtensions().add(attributeExtension);
                }
            }
        }
    }

    

Parameters:
writer
extensibleElement
extensionAttributeProcessor
context TODO
attributeModel
Throws:
ContributionWriteException
javax.xml.stream.XMLStreamException
    protected void writeExtendedAttributes(XMLStreamWriter writer,
                                           Extensible extensibleElement,
                                           StAXAttributeProcessor extensionAttributeProcessorProcessorContext context)
        
        for (Extension extension : extensibleElement.getAttributeExtensions()) {
            if (extension.isAttribute()) {
                extensionAttributeProcessor.write(extensionwritercontext);
            }
        }
    }
    protected void readExtendedElement(XMLStreamReader reader,
                                       Extensible extensible,
                                       StAXArtifactProcessor extensionProcessorProcessorContext context
        
        Object ext = extensionProcessor.read(readercontext);
        if (extensible != null) {
            extensible.getExtensions().add(ext);
        }
    }
    protected void writeExtendedElements(XMLStreamWriter writer,
                                         Extensible extensible,
                                         StAXArtifactProcessor extensionProcessorProcessorContext context
        
        List <ObjectotherExtensions = new ArrayList<Object>();
        
        // write all generic extensions as elements 
        // to produce semanticaly equal xml output 
        for (Object ext : extensible.getExtensions()) {
            if (ext instanceof Extension) {
                extensionProcessor.write(extwritercontext);
            } else {
                otherExtensions.add(ext);
            }
        }
        
        //wrap xml extensibility into a extension wrapper element
        if(otherExtensions.size() > 0) {
            
            writeStart(writer"extensions");
            
            for (Object extension : otherExtensions) {
                extensionProcessor.write(extensionwritercontext);
            }
            
            writeEnd(writer);
        }
        
    }

    
Represents an XML attribute that needs to be written to a document.
    public static class XAttr {
        private static final String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200912";
        private String uri = ;
        private String name;
        private Object value;
        public XAttr(String uriString nameString value) {
            this. = uri;
            this. = name;
            this. = value;
        }
        public XAttr(String nameString value) {
            this(nullnamevalue);
        }
        public XAttr(String uriString nameList<?> values) {
            this. = uri;
            this. = name;
            this. = values;
        }
        public XAttr(String nameList<?> values) {
            this(nullnamevalues);
        }
        public XAttr(String uriString nameBoolean value) {
            this. = uri;
            this. = name;
            this. = value;
        }
        public XAttr(String nameBoolean value) {
            this(nullnamevalue);
        }
        public XAttr(String uriString nameInteger value) {
            this. = uri;
            this. = name;
            this. = value;
        }
        public XAttr(String nameInteger value) {
            this(nullnamevalue);
        }
        public XAttr(String uriString nameDouble value) {
            this. = uri;
            this. = name;
            this. = value;
        }
        public XAttr(String nameDouble value) {
            this(nullnamevalue);
        }
        public XAttr(String uriString nameQName value) {
            this. = uri;
            this. = name;
            this. = value;
        }
        public XAttr(String nameQName value) {
            this(nullnamevalue);
        }
        
        public String toString() {
            return  == null ?  + "=\"" +  + "\"" : "{" +  + "}" +  + "=\"" +  + "\"";
        }

        
Writes a string from a QName and registers a prefix for its namespace.

Parameters:
reader
value
Returns:
        private String writeQNameValue(XMLStreamWriter writerQName qnamethrows XMLStreamException {
            if (qname != null) {
                String prefix = .writeNamespace(writerqname.getPrefix(), qname.getNamespaceURI());
                if ("".equals(prefix)) {
                    return qname.getLocalPart();
                } else {
                    return prefix + ":" + qname.getLocalPart();
                }
            }
            return null;
        }

        
Write to document

Parameters:
writer
Throws:
javax.xml.stream.XMLStreamException
        public void write(XMLStreamWriter writerthrows XMLStreamException {
            String str;
            if ( instanceof QName) {
                // Write a QName
                str = writeQNameValue(writer, (QName));
            } else if ( instanceof Collection) {
                // Write a list of values
                Collection<?> values = (Collection<?>);
                if (values.isEmpty()) {
                    return;
                }
                StringBuffer buffer = new StringBuffer();
                for (Object vvalues) {
                    if (v == null) {
                        // Skip null values
                        continue;
                    }
                    if (v instanceof XAttr) {
                        // Write an XAttr value
                        ((XAttr)v).write(writer);
                        continue;
                    }
                    if (buffer.length() != 0) {
                        buffer.append(' ');
                    }
                    if (v instanceof QName) {
                        // Write a QName value
                        buffer.append(writeQNameValue(writer, (QName)v));
                    } else {
                        // Write value as a string
                        buffer.append(String.valueOf(v));
                    }
                }
                str = buffer.toString();
            } else {
                // Write a string
                if ( == null) {
                    return;
                }
                str = String.valueOf();
            }
            if (str.length() == 0 && ( instanceof Collection)) {
                return;
            }
            .writeAttribute(writer""str);
        }
    }
New to GrepCode? Check out our FAQ X