Coverage Report - de.smartics.properties.spi.core.metadata.projectdoc.ProjectdocMetaDataParser
 
Classes in this File Line Coverage Branch Coverage Complexity
ProjectdocMetaDataParser
0%
0/107
0%
0/26
2,667
 
 1  
 /*
 2  
  * Copyright 2012-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.properties.spi.core.metadata.projectdoc;
 17  
 
 18  
 import static de.smartics.util.lang.StaticAnalysis.UNCHECKED;
 19  
 import java.io.BufferedInputStream;
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.util.List;
 23  
 import java.util.Locale;
 24  
 
 25  
 import org.apache.commons.io.IOUtils;
 26  
 import org.jdom.Document;
 27  
 import org.jdom.Element;
 28  
 import org.jdom.JDOMException;
 29  
 import org.jdom.Namespace;
 30  
 import org.jdom.input.SAXBuilder;
 31  
 import org.jdom.output.XMLOutputter;
 32  
 import org.slf4j.Logger;
 33  
 import org.slf4j.LoggerFactory;
 34  
 
 35  
 import de.smartics.properties.api.core.domain.DocumentMetaData;
 36  
 import de.smartics.properties.api.core.domain.PropertiesContext;
 37  
 import de.smartics.properties.api.core.domain.PropertyDescriptor;
 38  
 import de.smartics.properties.spi.core.metadata.projectdoc.ProjectdocAnnotationCollector.Defaults;
 39  
 import de.smartics.util.lang.Arguments;
 40  
 import de.smartics.util.lang.NullArgumentException;
 41  
 
 42  
 /**
 43  
  * Parses projectdoc information from property XML descriptors.
 44  
  */
 45  
 public final class ProjectdocMetaDataParser
 46  
 {
 47  
   // ********************************* Fields *********************************
 48  
 
 49  
   // --- constants ------------------------------------------------------------
 50  
 
 51  
   /**
 52  
    * Reference to the logger for this class.
 53  
    */
 54  0
   private static final Logger LOG = LoggerFactory
 55  
       .getLogger(ProjectdocMetaDataParser.class);
 56  
 
 57  
   /**
 58  
    * The namespace of the XML files this parser can parse.
 59  
    */
 60  0
   private static final Namespace PROPERTY_NS =
 61  
       Namespace
 62  
           .getNamespace("http://www.smartics.de/schema/projectdoc/doctype/property/1");
 63  
 
 64  
   // --- members --------------------------------------------------------------
 65  
 
 66  
   /**
 67  
    * The properties context to fetch information from the META-INF folder.
 68  
    */
 69  
   private final PropertiesContext context;
 70  
 
 71  
   /**
 72  
    * The metadata defaults to use.
 73  
    */
 74  
   private final Defaults defaults;
 75  
 
 76  
   // ****************************** Initializer *******************************
 77  
 
 78  
   // ****************************** Constructors ******************************
 79  
 
 80  
   /**
 81  
    * Default constructor without defaults.
 82  
    *
 83  
    * @param context the properties context to fetch information from the
 84  
    *          META-INF folder.
 85  
    * @throws NullArgumentException if {@code context} is <code>null</code>.
 86  
    */
 87  
   public ProjectdocMetaDataParser(final PropertiesContext context)
 88  
     throws NullArgumentException
 89  
   {
 90  0
     this(context, null);
 91  0
   }
 92  
 
 93  
   /**
 94  
    * Default constructor.
 95  
    *
 96  
    * @param context the properties context to fetch information from the
 97  
    *          META-INF folder.
 98  
    * @param defaults the metadata defaults to use.
 99  
    * @throws NullArgumentException if {@code context} is <code>null</code>.
 100  
    */
 101  
   public ProjectdocMetaDataParser(final PropertiesContext context,
 102  
       final Defaults defaults) throws NullArgumentException
 103  0
   {
 104  0
     Arguments.checkNotNull("context", context);
 105  
 
 106  0
     this.context = context;
 107  0
     this.defaults = defaults;
 108  0
   }
 109  
 
 110  
   // ****************************** Inner Classes *****************************
 111  
 
 112  
   // ********************************* Methods ********************************
 113  
 
 114  
   // --- init -----------------------------------------------------------------
 115  
 
 116  
   // --- get&set --------------------------------------------------------------
 117  
 
 118  
   // --- business -------------------------------------------------------------
 119  
 
 120  
   /**
 121  
    * Parses the comments for the given descriptor.
 122  
    *
 123  
    * @param descriptor the descriptor whose comments are requested to be parsed.
 124  
    * @param locale the locale to select the comments.
 125  
    * @return the comments for the given locale or the default comments, if the
 126  
    *         locale is not supported.
 127  
    */
 128  
   public DocumentMetaData parse(final PropertyDescriptor descriptor,
 129  
       final Locale locale)
 130  
   {
 131  0
     final String path = context.createMetaInfPath(descriptor, locale);
 132  0
     final ClassLoader loader = descriptor.getClass().getClassLoader(); // NOPMD
 133  
 
 134  0
     InputStream input = null;
 135  
     try
 136  
     {
 137  0
       final InputStream resource = loader.getResourceAsStream(path);
 138  0
       if (resource != null)
 139  
       {
 140  0
         input = new BufferedInputStream(resource);
 141  0
         final DocumentMetaData metaData = parse(path, input);
 142  0
         return metaData;
 143  
       }
 144  
       else
 145  
       {
 146  0
         LOG.warn("No meta data for property descriptor '{}' found: {}",
 147  
             descriptor, path);
 148  
       }
 149  
     }
 150  0
     catch (final MetaDataException e)
 151  
     {
 152  0
       LOG.warn("Cannot parse meta data for property descriptor '{}': {}",
 153  
           descriptor, e);
 154  
     }
 155  
     finally
 156  
     {
 157  0
       IOUtils.closeQuietly(input);
 158  0
     }
 159  
 
 160  0
     return null;
 161  
   }
 162  
 
 163  
   private DocumentMetaData parse(final String systemId, final InputStream input)
 164  
     throws MetaDataException
 165  
   {
 166  
     try
 167  
     {
 168  0
       final SAXBuilder parser = new SAXBuilder();
 169  0
       final Document document = parser.build(input, systemId);
 170  
 
 171  0
       final ProjectdocMetaData metaData = new ProjectdocMetaData();
 172  
 
 173  0
       final Element rootNode = document.getRootElement();
 174  0
       addIdInfo(metaData, rootNode);
 175  0
       addFiling(metaData, rootNode);
 176  0
       addDescription(metaData, rootNode);
 177  
 
 178  0
       return metaData;
 179  
     }
 180  0
     catch (final JDOMException e)
 181  
     {
 182  0
       throw new MetaDataException(systemId, e);
 183  
     }
 184  0
     catch (final IOException e)
 185  
     {
 186  0
       throw new MetaDataException(systemId, e);
 187  
     }
 188  
   }
 189  
 
 190  
   private void addIdInfo(final ProjectdocMetaData metaData,
 191  
       final Element rootNode)
 192  
   {
 193  0
     if (defaults != null)
 194  
     {
 195  0
       metaData.setName(defaults.getName());
 196  0
       metaData.setSpace(defaults.getSpace());
 197  0
       metaData.setTitle(defaults.getTitle());
 198  
     }
 199  
 
 200  0
     final Element identification =
 201  
         rootNode.getChild("identification", PROPERTY_NS);
 202  0
     if (identification != null)
 203  
     {
 204  0
       final String space =
 205  
           identification.getChildTextNormalize("space", PROPERTY_NS);
 206  0
       metaData.setSpace(space);
 207  0
       final String title =
 208  
           identification.getChildTextNormalize("title", PROPERTY_NS);
 209  0
       metaData.setTitle(title);
 210  
     }
 211  
 
 212  0
     final String name = rootNode.getChildTextNormalize("name", PROPERTY_NS);
 213  0
     metaData.setName(name);
 214  0
   }
 215  
 
 216  
   private void addFiling(final ProjectdocMetaData metaData,
 217  
       final Element rootNode)
 218  
   {
 219  0
     final Element filing = rootNode.getChild("filing", PROPERTY_NS);
 220  0
     if (filing != null)
 221  
     {
 222  0
       addParents(metaData, filing);
 223  0
       addCategories(metaData, filing);
 224  0
       addTags(metaData, filing);
 225  0
       setSortKey(metaData, filing);
 226  
     }
 227  0
   }
 228  
 
 229  
   @SuppressWarnings(UNCHECKED)
 230  
   private void addParents(final ProjectdocMetaData metaData,
 231  
       final Element filing)
 232  
   {
 233  0
     final Element parentsElement = filing.getChild("parents", PROPERTY_NS);
 234  0
     if (parentsElement != null)
 235  
     {
 236  0
       final List<Element> parents =
 237  
           parentsElement.getChildren("parent", PROPERTY_NS);
 238  0
       for (final Element parent : parents)
 239  
       {
 240  0
         final String parentName = parent.getTextNormalize();
 241  0
         metaData.addParent(parentName);
 242  0
       }
 243  
     }
 244  0
   }
 245  
 
 246  
   @SuppressWarnings(UNCHECKED)
 247  
   private void addCategories(final ProjectdocMetaData metaData,
 248  
       final Element filing)
 249  
   {
 250  0
     final Element categoriesElement =
 251  
         filing.getChild("categories", PROPERTY_NS);
 252  0
     if (categoriesElement != null)
 253  
     {
 254  0
       final List<Element> categories =
 255  
           categoriesElement.getChildren("category", PROPERTY_NS);
 256  0
       for (final Element category : categories)
 257  
       {
 258  0
         final String categoryName = category.getTextNormalize();
 259  0
         metaData.addCategory(categoryName);
 260  0
       }
 261  
     }
 262  0
   }
 263  
 
 264  
   @SuppressWarnings(UNCHECKED)
 265  
   private void addTags(final ProjectdocMetaData metaData, final Element filing)
 266  
   {
 267  0
     final Element tagsElement = filing.getChild("tags", PROPERTY_NS);
 268  0
     if (tagsElement != null)
 269  
     {
 270  0
       final List<Element> tags = tagsElement.getChildren("tag", PROPERTY_NS);
 271  0
       for (final Element tag : tags)
 272  
       {
 273  0
         final String tagName = tag.getTextNormalize();
 274  0
         metaData.addTag(tagName);
 275  0
       }
 276  
     }
 277  0
   }
 278  
 
 279  
   private void setSortKey(final ProjectdocMetaData metaData,
 280  
       final Element filing)
 281  
   {
 282  0
     final String sortKey = filing.getChildTextNormalize("sortKey", PROPERTY_NS);
 283  0
     metaData.setSortKey(sortKey);
 284  0
   }
 285  
 
 286  
   @SuppressWarnings(UNCHECKED)
 287  
   private void addDescription(final ProjectdocMetaData metaData,
 288  
       final Element rootNode)
 289  
   {
 290  0
     final Element description = rootNode.getChild("description", PROPERTY_NS);
 291  0
     if (description != null)
 292  
     {
 293  0
       final Element audienceElement =
 294  
           description.getChild("audience", PROPERTY_NS);
 295  0
       if (audienceElement != null)
 296  
       {
 297  0
         final List<Element> members =
 298  
             audienceElement.getChildren("member", PROPERTY_NS);
 299  0
         for (final Element member : members)
 300  
         {
 301  0
           final String memberName = member.getTextNormalize();
 302  0
           metaData.addAudience(memberName);
 303  0
         }
 304  
       }
 305  
 
 306  0
       final Element shortDescriptionElement =
 307  
           description.getChild("shortDescription", PROPERTY_NS);
 308  0
       final String shortDescription = toString(shortDescriptionElement);
 309  0
       metaData.setShortDescription(shortDescription);
 310  
 
 311  0
       final Element summaryElement =
 312  
           description.getChild("summary", PROPERTY_NS);
 313  0
       final String summary = toString(summaryElement);
 314  0
       metaData.setSummary(summary);
 315  
 
 316  0
       final Element notesElement = description.getChild("notes", PROPERTY_NS);
 317  0
       final String notes = toString(notesElement);
 318  0
       metaData.addNote(notes);
 319  
     }
 320  0
   }
 321  
 
 322  
   private static String toString(final Element element)
 323  
   {
 324  0
     final XMLOutputter outp = new XMLOutputter();
 325  0
     final StringBuilder buffer = new StringBuilder(1024);
 326  
 
 327  0
     final List<?> children = element.getContent();
 328  0
     final String string = outp.outputString(children);
 329  0
     buffer.append(string);
 330  
 
 331  0
     return buffer.toString().trim();
 332  
   }
 333  
 
 334  
   // --- object basics --------------------------------------------------------
 335  
 
 336  
 }