felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r565653 - in /felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin: ./ om/ om/metatype/ tags/ xml/
Date Tue, 14 Aug 2007 08:13:48 GMT
Author: cziegeler
Date: Tue Aug 14 01:13:47 2007
New Revision: 565653

URL: http://svn.apache.org/viewvc?view=rev&rev=565653
Log:
Generate xml by hand.

Modified:
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/metatype/AttributeDefinition.java
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/tags/JavaClassDescriptorManager.java
    felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java?view=diff&rev=565653&r1=565652&r2=565653
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java
(original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/SCRDescriptorMojo.java
Tue Aug 14 01:13:47 2007
@@ -160,8 +160,7 @@
         XMLWriter xw = null;
         try {
             if ( testNewOM ) {
-                final ComponentDescriptorIO io = new ComponentDescriptorIO();
-                io.write(descriptorFile, components);
+                ComponentDescriptorIO.write(components, descriptorFile);
             } else {
                 descriptorStream = new FileOutputStream(descriptorFile);
                 xw = new XMLWriter(descriptorStream);
@@ -555,8 +554,25 @@
             org.apache.felix.sandbox.scrplugin.om.Property prop = new org.apache.felix.sandbox.scrplugin.om.Property(property);
             prop.setName(name);
             prop.setType(property.getNamedParameter(SCRDescriptor.PROPERTY_TYPE));
-            prop.setValue(property.getNamedParameter(SCRDescriptor.PROPERTY_VALUE));
-            prop.setValues(property.getNamedParameterMap());
+            final String value = property.getNamedParameter(SCRDescriptor.PROPERTY_VALUE);
+            if ( value != null ) {
+                prop.setValue(value);
+            } else {
+                // check for multivalue
+                final List values = new ArrayList();
+                final Map valueMap = property.getNamedParameterMap();
+                for (Iterator vi = valueMap.entrySet().iterator(); vi.hasNext();) {
+                    final Map.Entry entry = (Map.Entry) vi.next();
+                    final String key = (String) entry.getKey();
+                    if (key.startsWith("values")) {
+                        values.add(entry.getValue());
+                    }
+                }
+                if ( values.size() > 0 ) {
+                    prop.setMultiValue((String[])values.toArray(new String[values.size()]));
+                }
+            }
+
             final boolean isPrivate = this.getBoolean(property, SCRDescriptor.PROPERTY_PRIVATE,
false);
             // if this is a public property and the component is generating metatype info
             // store the information!
@@ -578,24 +594,24 @@
                 }
                 ad.setDescription(adDesc);
                 // set optional multivalues, cardinality might be overwritten by setValues
!!
-                final String value = property.getNamedParameter(SCRDescriptor.PROPERTY_CARDINALITY);
-                if (value != null) {
-                    if ("-".equals(value)) {
+                final String cValue = property.getNamedParameter(SCRDescriptor.PROPERTY_CARDINALITY);
+                if (cValue != null) {
+                    if ("-".equals(cValue)) {
                         // unlimited vector
                         ad.setCardinality(new Integer(Integer.MIN_VALUE));
-                    } else if ("+".equals(value)) {
+                    } else if ("+".equals(cValue)) {
                        // unlimited array
                         ad.setCardinality(new Integer(Integer.MAX_VALUE));
                     } else {
                         try {
-                            ad.setCardinality(Integer.valueOf(value));
+                            ad.setCardinality(Integer.valueOf(cValue));
                         } catch (NumberFormatException nfe) {
                             // default to scalar in case of conversion problem
                         }
                     }
                 }
                 ad.setDefaultValue(prop.getValue());
-                ad.setText(prop.getText());
+                ad.setDefaultMultiValue(prop.getMultiValue());
 
                 // check options
                 String[] parameters = property.getParameters();

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java?view=diff&rev=565653&r1=565652&r2=565653
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java
(original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/Property.java
Tue Aug 14 01:13:47 2007
@@ -18,9 +18,7 @@
  */
 package org.apache.felix.sandbox.scrplugin.om;
 
-import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.felix.sandbox.scrplugin.tags.JavaTag;
 
@@ -33,7 +31,7 @@
     protected String name;
     protected String value;
     protected String type;
-    protected String text;
+    protected String[] multiValue;
 
     /**
      * Default constructor.
@@ -63,7 +61,7 @@
 
     public void setValue(String value) {
         this.value = value;
-        this.text = null;
+        this.multiValue = null;
     }
 
     public String getType() {
@@ -74,25 +72,13 @@
         this.type = type;
     }
 
-    public String getText() {
-        return this.text;
+    public String[] getMultiValue() {
+        return this.multiValue;
     }
 
-    public void setText(String text) {
-        this.text = text;
+    public void setMultiValue(String[] values) {
+        this.multiValue = values;
         this.value = null;
-    }
-
-    public void setValues(Map valueMap) {
-        this.value = null;
-        this.text = "";
-        for (Iterator vi = valueMap.entrySet().iterator(); vi.hasNext();) {
-            Map.Entry entry = (Map.Entry) vi.next();
-            String key = (String) entry.getKey();
-            if (key.startsWith("values")) {
-                this.text = this.text + entry.getValue() + "\n";
-            }
-        }
     }
 
     /**

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/metatype/AttributeDefinition.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/metatype/AttributeDefinition.java?view=diff&rev=565653&r1=565652&r2=565653
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/metatype/AttributeDefinition.java
(original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/om/metatype/AttributeDefinition.java
Tue Aug 14 01:13:47 2007
@@ -30,7 +30,7 @@
 
     protected String name;
 
-    protected String text;
+    protected String[] defaultMultiValue;
 
     protected String description;
 
@@ -60,19 +60,19 @@
 
     public void setDefaultValue(String defaultValue) {
         this.defaultValue = defaultValue;
-        this.text = null;
+        this.defaultMultiValue = null;
     }
 
-    public void setText(String v) {
+    public void setDefaultMultiValue(String[] values) {
         this.defaultValue = null;
-        this.text = v;
-        if (v != null && v.length() > 0 && this.cardinality == null )
{
+        this.defaultMultiValue = values;
+        if (values != null && values.length > 0 && this.cardinality ==
null ) {
             this.cardinality = new Integer(Integer.MAX_VALUE);
         }
     }
 
-    public String getText() {
-        return this.text;
+    public String[] getDefaultMultiValue() {
+        return this.defaultMultiValue;
     }
 
     public String getName() {

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/tags/JavaClassDescriptorManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/tags/JavaClassDescriptorManager.java?view=diff&rev=565653&r1=565652&r2=565653
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/tags/JavaClassDescriptorManager.java
(original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/tags/JavaClassDescriptorManager.java
Tue Aug 14 01:13:47 2007
@@ -295,23 +295,18 @@
      */
     public void writeAbstractDescriptorFile(org.apache.felix.sandbox.scrplugin.om.Components
components, File outputDirectory)
     throws MojoExecutionException {
-        try {
-            // if we have abstract descriptors, write them
-            final File adFile = new File(outputDirectory, ABSTRACT_DESCRIPTOR_RELATIVE_PATH);
-            if ( !components.getComponents().isEmpty() ) {
-                this.getLog().info("Writing abstract service descriptor " + adFile + " with
" + components.getComponents().size() + " entries.");
-                adFile.getParentFile().mkdirs();
-                ComponentDescriptorIO io = new ComponentDescriptorIO();
-                io.write(adFile, components);
-            } else {
-                // remove file
-                if ( adFile.exists() ) {
-                    this.getLog().debug("Removing obsolete abstract service descriptor "
+ adFile);
-                    adFile.delete();
-                }
+        // if we have abstract descriptors, write them
+        final File adFile = new File(outputDirectory, ABSTRACT_DESCRIPTOR_RELATIVE_PATH);
+        if ( !components.getComponents().isEmpty() ) {
+            this.getLog().info("Writing abstract service descriptor " + adFile + " with "
+ components.getComponents().size() + " entries.");
+            adFile.getParentFile().mkdirs();
+            ComponentDescriptorIO.write(components, adFile);
+        } else {
+            // remove file
+            if ( adFile.exists() ) {
+                this.getLog().debug("Removing obsolete abstract service descriptor " + adFile);
+                adFile.delete();
             }
-        } catch (IOException ioe) {
-            throw new MojoExecutionException("Failed to write scr-plugin scrinfo.xml", ioe);
         }
     }
 

Modified: felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java?view=diff&rev=565653&r1=565652&r2=565653
==============================================================================
--- felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java
(original)
+++ felix/sandbox/cziegeler/maven-scr-plugin/src/main/java/org/apache/felix/sandbox/scrplugin/xml/ComponentDescriptorIO.java
Tue Aug 14 01:13:47 2007
@@ -25,16 +25,26 @@
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.util.Iterator;
 
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
 import javax.xml.transform.stream.StreamResult;
 import javax.xml.transform.stream.StreamSource;
 
 import org.apache.commons.io.IOUtils;
+import org.apache.felix.sandbox.scrplugin.om.Component;
+import org.apache.felix.sandbox.scrplugin.om.Components;
+import org.apache.felix.sandbox.scrplugin.om.Implementation;
+import org.apache.felix.sandbox.scrplugin.om.Property;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
 
 import com.thoughtworks.xstream.XStream;
 
@@ -46,6 +56,38 @@
  */
 public class ComponentDescriptorIO {
 
+    public static final String NAMESPACE_URI = "http://www.osgi.org/xmlns/scr/v1.0.0";
+
+    private static final String PREFIX = "scr";
+
+    private static final String COMPONENTS = "components";
+
+    private static final String COMPONENT = "component";
+
+    private static final String COMPONENT_QNAME = PREFIX + ':' + COMPONENT;
+
+    private static final String IMPLEMENTATION = "implementation";
+
+    private static final String IMPLEMENTATION_QNAME = PREFIX + ':' + IMPLEMENTATION;
+
+    private static final String SERVICE = "service";
+
+    private static final String SERVICE_QNAME = PREFIX + ':' + SERVICE;
+
+    private static final String PROPERTY = "property";
+
+    private static final String PROPERTY_QNAME = PREFIX + ':' + PROPERTY;
+
+    private static final String REFERENCE = "reference";
+
+    private static final String REFERENCE_QNAME = PREFIX + ':' + REFERENCE;
+
+    private static final String INTERFACE = "provide";
+
+    private static final String INTERFACE_QNAME = PREFIX + ':' + INTERFACE;
+
+    private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) TransformerFactory.newInstance();
+
     protected final XStream xstream;
 
     public ComponentDescriptorIO() {
@@ -54,10 +96,10 @@
 
         this.xstream.omitField(org.apache.felix.sandbox.scrplugin.om.AbstractObject.class,
"tag");
 
-        this.xstream.alias("components", org.apache.felix.sandbox.scrplugin.om.Components.class);
-        this.xstream.addImplicitCollection(org.apache.felix.sandbox.scrplugin.om.Components.class,
"components");
+        this.xstream.alias(ComponentDescriptorIO.COMPONENTS, org.apache.felix.sandbox.scrplugin.om.Components.class);
+        this.xstream.addImplicitCollection(org.apache.felix.sandbox.scrplugin.om.Components.class,
ComponentDescriptorIO.COMPONENTS);
 
-        this.xstream.alias("component", org.apache.felix.sandbox.scrplugin.om.Component.class);
+        this.xstream.alias(ComponentDescriptorIO.COMPONENT, org.apache.felix.sandbox.scrplugin.om.Component.class);
         this.xstream.addImplicitCollection(org.apache.felix.sandbox.scrplugin.om.Component.class,
"references");
         this.xstream.addImplicitCollection(org.apache.felix.sandbox.scrplugin.om.Component.class,
"properties");
         this.xstream.useAttributeFor(org.apache.felix.sandbox.scrplugin.om.Component.class,
"name");
@@ -114,20 +156,213 @@
         }
     }
 
-    public void write(File file, org.apache.felix.sandbox.scrplugin.om.Components components)
-    throws IOException, MojoExecutionException {
-        Writer buffer = new StringWriter();
-        this.xstream.toXML(components, buffer);
-
-        final TransformerFactory factory = TransformerFactory.newInstance();
-        Transformer transformer;
+    /**
+     * Write the component descriptors to the file.
+     * @param components
+     * @param file
+     * @throws MojoExecutionException
+     */
+    public static void write(org.apache.felix.sandbox.scrplugin.om.Components components,
File file)
+    throws MojoExecutionException {
         try {
-            transformer = factory.newTransformer(new StreamSource(this.getClass().getResourceAsStream("/org/apache/felix/sandbox/scrplugin/xml/write.xsl")));
-            transformer.setOutputProperty(OutputKeys.INDENT, "no");
+            FileWriter writer = new FileWriter(file);
+            final TransformerHandler transformerHandler = FACTORY.newTransformerHandler();
+            final Transformer transformer = transformerHandler.getTransformer();
+            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
+            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+            transformerHandler.setResult(new StreamResult(writer));
 
-            transformer.transform(new StreamSource(new StringReader(buffer.toString())),
new StreamResult(new FileWriter(file)));
+            generateXML(components, transformerHandler);
         } catch (TransformerException e) {
-            throw new MojoExecutionException("Unable to write xml.", e);
+            throw new MojoExecutionException("Unable to write xml to " + file, e);
+        } catch (SAXException e) {
+            throw new MojoExecutionException("Unable to generate xml for " + file, e);
+        } catch (IOException e) {
+            throw new MojoExecutionException("Unable to write xml to " + file, e);
+        }
+    }
+
+    /**
+     * Generate the xml top level element and start streaming
+     * the components.
+     * @param components
+     * @param contentHandler
+     * @throws SAXException
+     */
+    protected static void generateXML(Components components, ContentHandler contentHandler)
+    throws SAXException {
+        contentHandler.startDocument();
+        contentHandler.startPrefixMapping(PREFIX, NAMESPACE_URI);
+
+        // wrapper element to generate well formed xml
+        contentHandler.startElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS,
new AttributesImpl());
+
+        final Iterator i = components.getComponents().iterator();
+        while ( i.hasNext() ) {
+            final Component component = (Component)i.next();
+            generateXML(component, contentHandler);
+        }
+        // end wrapper element
+        contentHandler.endElement("", ComponentDescriptorIO.COMPONENTS, ComponentDescriptorIO.COMPONENTS);
+        contentHandler.endPrefixMapping(PREFIX);
+        contentHandler.endDocument();
+    }
+
+    /**
+     * Write the xml for a {@link Component}.
+     * @param component
+     * @param contentHandler
+     * @throws SAXException
+     */
+    protected static void generateXML(Component component, ContentHandler contentHandler)
+    throws SAXException {
+        final AttributesImpl ai = new AttributesImpl();
+        addAttribute(ai, "enabled", component.isEnabled());
+        addAttribute(ai, "immediate",component.isImmediate());
+        addAttribute(ai, "name", component.getName());
+        addAttribute(ai, "factory", component.getFactory());
+
+        contentHandler.startElement(NAMESPACE_URI, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME,
ai);
+        generateXML(component.getImplementation(), contentHandler);
+        if ( component.getService() != null ) {
+            generateXML(component.getService(), contentHandler);
+        }
+        if ( component.getProperties() != null ) {
+            final Iterator i = component.getProperties().iterator();
+            while ( i.hasNext() ) {
+                final Property property = (Property)i.next();
+                generateXML(property, contentHandler);
+            }
+        }
+        if ( component.getReferences() != null ) {
+            final Iterator i = component.getReferences().iterator();
+            while ( i.hasNext() ) {
+                final Reference reference = (Reference)i.next();
+                generateXML(reference, contentHandler);
+            }
+        }
+        contentHandler.endElement(NAMESPACE_URI, ComponentDescriptorIO.COMPONENT, ComponentDescriptorIO.COMPONENT_QNAME);
+    }
+
+    /**
+     * Write the xml for a {@link Implementation}.
+     * @param implementation
+     * @param contentHandler
+     * @throws SAXException
+     */
+    protected static void generateXML(Implementation implementation, ContentHandler contentHandler)
+    throws SAXException {
+        final AttributesImpl ai = new AttributesImpl();
+        addAttribute(ai, "class", implementation.getClassame());
+        contentHandler.startElement(NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION,
ComponentDescriptorIO.IMPLEMENTATION_QNAME, ai);
+        contentHandler.endElement(NAMESPACE_URI, ComponentDescriptorIO.IMPLEMENTATION, ComponentDescriptorIO.IMPLEMENTATION_QNAME);
+    }
+
+    /**
+     * Write the xml for a {@link Service}.
+     * @param service
+     * @param contentHandler
+     * @throws SAXException
+     */
+    protected static void generateXML(org.apache.felix.sandbox.scrplugin.om.Service service,
ContentHandler contentHandler)
+    throws SAXException {
+        final AttributesImpl ai = new AttributesImpl();
+        addAttribute(ai, "servicefactory", service.getServicefactory());
+        contentHandler.startElement(NAMESPACE_URI, ComponentDescriptorIO.SERVICE, ComponentDescriptorIO.SERVICE_QNAME,
ai);
+        if ( service.getInterfaces() != null ) {
+            final Iterator i = service.getInterfaces().iterator();
+            while ( i.hasNext() ) {
+                final Interface interf = (Interface)i.next();
+                generateXML(interf, contentHandler);
+            }
+        }
+        contentHandler.endElement(NAMESPACE_URI, ComponentDescriptorIO.SERVICE, ComponentDescriptorIO.SERVICE_QNAME);
+    }
+
+    /**
+     * Write the xml for a {@link Interface}.
+     * @param interface
+     * @param contentHandler
+     * @throws SAXException
+     */
+    protected static void generateXML(Interface interf, ContentHandler contentHandler)
+    throws SAXException {
+        final AttributesImpl ai = new AttributesImpl();
+        addAttribute(ai, "interface", interf.getInterfaceame());
+        contentHandler.startElement(NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME,
ai);
+        contentHandler.endElement(NAMESPACE_URI, ComponentDescriptorIO.INTERFACE, ComponentDescriptorIO.INTERFACE_QNAME);
+    }
+
+    /**
+     * Write the xml for a {@link Property}.
+     * @param property
+     * @param contentHandler
+     * @throws SAXException
+     */
+    protected static void generateXML(Property property, ContentHandler contentHandler)
+    throws SAXException {
+        final AttributesImpl ai = new AttributesImpl();
+        addAttribute(ai, "name", property.getName());
+        addAttribute(ai, "type", property.getType());
+        addAttribute(ai, "value", property.getValue());
+        contentHandler.startElement(NAMESPACE_URI, ComponentDescriptorIO.PROPERTY, ComponentDescriptorIO.PROPERTY_QNAME,
ai);
+        if ( property.getMultiValue() != null && property.getMultiValue().length
> 0 ) {
+            for(int i=0; i<property.getMultiValue().length; i++) {
+                text(contentHandler, "    ");
+                text(contentHandler, property.getMultiValue()[i]);
+                text(contentHandler, "\n");
+            }
+        }
+        contentHandler.endElement(NAMESPACE_URI, ComponentDescriptorIO.PROPERTY, ComponentDescriptorIO.PROPERTY_QNAME);
+    }
+
+    /**
+     * Write the xml for a {@link Reference}.
+     * @param reference
+     * @param contentHandler
+     * @throws SAXException
+     */
+    protected static void generateXML(Reference reference, ContentHandler contentHandler)
+    throws SAXException {
+        final AttributesImpl ai = new AttributesImpl();
+        addAttribute(ai, "name", reference.getName());
+        addAttribute(ai, "interface", reference.getInterfacename());
+        addAttribute(ai, "cardinality", reference.getCardinality());
+        addAttribute(ai, "policy", reference.getPolicy());
+        addAttribute(ai, "target", reference.getTarget());
+        addAttribute(ai, "bind", reference.getBind());
+        addAttribute(ai, "unbind", reference.getUnbind());
+        contentHandler.startElement(NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME,
ai);
+        contentHandler.endElement(NAMESPACE_URI, ComponentDescriptorIO.REFERENCE, ComponentDescriptorIO.REFERENCE_QNAME);
+    }
+
+    /**
+     * Helper method to add an attribute.
+     * This implementation adds a new attribute with the given name
+     * and value. Before adding the value is checked for non-null.
+     * @param ai    The attributes impl receiving the additional attribute.
+     * @param name  The name of the attribute.
+     * @param value The value of the attribute.
+     */
+    protected static void addAttribute(AttributesImpl ai, String name, Object value) {
+        if ( value != null ) {
+            ai.addAttribute("", name, name, "CDATA", value.toString());
+        }
+    }
+
+    /**
+     * Helper method writing out a string.
+     * @param ch
+     * @param text
+     * @throws SAXException
+     */
+    protected static void text(ContentHandler ch, String text)
+    throws SAXException {
+        if ( text != null ) {
+            final char[] c = text.toCharArray();
+            ch.characters(c, 0, c.length);
         }
     }
 }



Mime
View raw message