View Javadoc

1   /*
2    * Copyright 2008-2010 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package de.smartics.maven.issues.cache;
18  
19  import java.io.Closeable;
20  import java.util.Map;
21  
22  import javax.xml.stream.XMLStreamException;
23  import javax.xml.stream.XMLStreamWriter;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
28  import org.eclipse.mylyn.tasks.core.data.TaskAttributeMetaData;
29  import org.eclipse.mylyn.tasks.core.data.TaskData;
30  
31  /**
32   * Writes a single task to a stream.
33   *
34   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
35   * @version $Revision:591 $
36   */
37  public class XmlTaskWriter implements TaskWriter, Closeable
38  {
39    // ********************************* Fields *********************************
40  
41    // --- constants ------------------------------------------------------------
42  
43    // --- members --------------------------------------------------------------
44  
45    /**
46     * Reference to the logger for this class.
47     */
48    private final Log log = LogFactory.getLog(XmlTaskWriter.class);
49  
50    /**
51     * The writer to write the single tasks to.
52     */
53    protected final XMLStreamWriter xmlWriter;
54  
55    // ****************************** Initializer *******************************
56  
57    // ****************************** Constructors ******************************
58  
59    /**
60     * Default constructor.
61     *
62     * @param xmlWriter the writer to write the single tasks to.
63     * @throws NullPointerException if the <code>xmlWriter</code> is
64     *           <code>null</code>.
65     */
66    public XmlTaskWriter(final XMLStreamWriter xmlWriter)
67      throws NullPointerException
68    {
69      if (xmlWriter == null)
70      {
71        throw new NullPointerException("XML writer must not be 'null'.");
72      }
73  
74      this.xmlWriter = xmlWriter;
75    }
76  
77    // ****************************** Inner Classes *****************************
78  
79    // ********************************* Methods ********************************
80  
81    // --- init -----------------------------------------------------------------
82  
83    // --- get&set --------------------------------------------------------------
84  
85    // --- business -------------------------------------------------------------
86  
87    /**
88     * {@inheritDoc}
89     *
90     * @see de.smartics.maven.issues.cache.TaskWriter#writeTask(org.eclipse.mylyn.tasks.core.data.TaskData)
91     */
92    public void writeTask(final TaskData task) throws XMLStreamException
93    {
94      xmlWriter.writeStartElement(TaskInfoKey.TASK_DATA);
95      writeValueAsAttributeIfNotNull(TaskInfoKey.TASK_ID, task.getTaskId());
96      writeValueAsAttributeIfNotNull(TaskInfoKey.CONNECTOR_KIND,
97          task.getConnectorKind());
98      writeValueAsAttributeIfNotNull(TaskInfoKey.VERSION, task.getVersion());
99      writeValueAsAttributeIfNotNull(TaskInfoKey.REPOSITORY_URL,
100         task.getRepositoryUrl());
101     writeAttribute(task.getRoot());
102     xmlWriter.writeEndElement();
103   }
104 
105   /**
106    * Writes the given value as XML attribute value if this value is not
107    * <code>null</code>.
108    *
109    * @param name the name of the XML attribute to write.
110    * @param value the value for the XML attribute to write.
111    * @throws XMLStreamException if the writing of the XML attribute failed.
112    */
113   private void writeValueAsAttributeIfNotNull(final String name,
114       final String value) throws XMLStreamException
115   {
116     if (value != null)
117     {
118       xmlWriter.writeAttribute(name, value);
119     }
120   }
121 
122   /**
123    * {@inheritDoc}
124    *
125    * @see de.smartics.maven.issues.cache.TaskWriter#writeAttribute(org.eclipse.mylyn.tasks.core.data.TaskAttribute)
126    */
127   public void writeAttribute(final TaskAttribute attribute)
128     throws XMLStreamException
129   {
130     if (attribute != null)
131     {
132       xmlWriter.writeStartElement(TaskInfoKey.ATTRIBUTE);
133       xmlWriter.writeAttribute(TaskInfoKey.ATTRIBUTE_ID, attribute.getId());
134 
135       writeAttributeValues(attribute);
136       writeAttributes(attribute.getAttributes());
137       final TaskAttributeMetaData metaData = attribute.getMetaData();
138       if (metaData != null)
139       {
140         writeMap(TaskInfoKey.META_DATA, metaData.getValues());
141       }
142       writeMap(TaskInfoKey.OPTIONS, attribute.getOptions());
143 
144       xmlWriter.writeEndElement();
145     }
146   }
147 
148   /**
149    * {@inheritDoc}
150    *
151    * @see de.smartics.maven.issues.cache.TaskWriter#writeAttributes(java.util.Map)
152    */
153   public void writeAttributes(final Map<String, TaskAttribute> attributes)
154     throws XMLStreamException
155   {
156     if (attributes != null && !attributes.values().isEmpty())
157     {
158       xmlWriter.writeStartElement(TaskInfoKey.ATTRIBUTES);
159       for (TaskAttribute attribute : attributes.values())
160       {
161         writeAttribute(attribute);
162       }
163       xmlWriter.writeEndElement();
164     }
165   }
166 
167   /**
168    * {@inheritDoc}
169    *
170    * @see de.smartics.maven.issues.cache.TaskWriter#writeAttributeValues(org.eclipse.mylyn.tasks.core.data.TaskAttribute)
171    */
172   public void writeAttributeValues(final TaskAttribute attribute)
173     throws XMLStreamException
174   {
175     if (attribute != null && !attribute.getValues().isEmpty())
176     {
177       xmlWriter.writeStartElement(TaskInfoKey.VALUES);
178       for (String value : attribute.getValues())
179       {
180         writeSimpleElement(TaskInfoKey.VALUE, value);
181       }
182       xmlWriter.writeEndElement();
183     }
184   }
185 
186   /**
187    * {@inheritDoc}
188    *
189    * @see de.smartics.maven.issues.cache.TaskWriter#writeMap(java.lang.String,
190    *      java.util.Map)
191    */
192   public void writeMap(final String gi, final Map<String, String> map)
193     throws XMLStreamException
194   {
195     if (map != null && !map.isEmpty())
196     {
197       xmlWriter.writeStartElement(gi);
198       for (Map.Entry<String, String> entry : map.entrySet())
199       {
200         xmlWriter.writeStartElement(TaskInfoKey.ENTRY);
201         writeSimpleElement(TaskInfoKey.KEY, entry.getKey());
202         writeSimpleElement(TaskInfoKey.VALUE, entry.getValue());
203         xmlWriter.writeEndElement();
204       }
205       xmlWriter.writeEndElement();
206     }
207   }
208 
209   /**
210    * {@inheritDoc}
211    *
212    * @see de.smartics.maven.issues.cache.TaskWriter#writeSimpleElement(java.lang.String,
213    *      java.lang.String)
214    */
215   public void writeSimpleElement(final String gi, final String content)
216     throws XMLStreamException
217   {
218     xmlWriter.writeStartElement(gi);
219     xmlWriter.writeCharacters(content);
220     xmlWriter.writeEndElement();
221   }
222 
223   /**
224    * Closes this stream and releases any system resources associated with it. If
225    * the stream is already closed then invoking this method has no effect.
226    * <p>
227    * The stream is closed quietly, logging any failure at error level.
228    * </p>
229    *
230    * @see java.io.Closeable#close()
231    */
232   public void close()
233   {
234     try
235     {
236       this.xmlWriter.close();
237     }
238     catch (final XMLStreamException e)
239     {
240       if (log.isErrorEnabled())
241       {
242         log.error("Cannot close XML stream of task writer.");
243       }
244     }
245   }
246 
247   // --- object basics --------------------------------------------------------
248 
249 }