View Javadoc

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