View Javadoc

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 java.util.Locale;
19  
20  import org.jdom.Document;
21  import org.jdom.Element;
22  import org.jdom.Namespace;
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import de.smartics.properties.api.core.domain.PropertiesContext;
27  import de.smartics.properties.api.core.domain.PropertyDescriptor;
28  import de.smartics.properties.api.core.domain.PropertySetProjectdoc;
29  import de.smartics.properties.spi.core.metadata.projectdoc.ProjectdocAnnotationCollector.Defaults;
30  
31  /**
32   * Parsed projectdoc's property set documents.
33   */
34  public final class PropertySetProjectdocParser extends ProjectdocMetaDataParser
35  {
36    // ********************************* Fields *********************************
37  
38    // --- constants ------------------------------------------------------------
39  
40    /**
41     * Reference to the logger for this class.
42     */
43    private static final Logger LOG = LoggerFactory
44        .getLogger(PropertySetProjectdocParser.class);
45  
46    /**
47     * The namespace of the XML files this parser can parse.
48     */
49    protected static final Namespace NS =
50        Namespace
51            .getNamespace("http://www.smartics.de/schema/projectdoc/doctype/property-set/1");
52  
53    // --- members --------------------------------------------------------------
54  
55    // ****************************** Initializer *******************************
56  
57    // ****************************** Constructors ******************************
58  
59    /**
60     * Convenience constructor without defaults.
61     *
62     * @param context the properties context to fetch information from the
63     *          META-INF folder.
64     * @throws NullPointerException if {@code context} is <code>null</code>.
65     */
66    public PropertySetProjectdocParser(final PropertiesContext context)
67      throws NullPointerException
68    {
69      super(context);
70    }
71  
72    /**
73     * Default constructor.
74     *
75     * @param context the properties context to fetch information from the
76     *          META-INF folder.
77     * @param defaults the metadata defaults to use.
78     * @throws NullPointerException if {@code context} is <code>null</code>.
79     */
80    public PropertySetProjectdocParser(final PropertiesContext context,
81        final Defaults defaults) throws NullPointerException
82    {
83      super(context, defaults);
84    }
85  
86    // ****************************** Inner Classes *****************************
87  
88    // ********************************* Methods ********************************
89  
90    // --- init -----------------------------------------------------------------
91  
92    // --- get&set --------------------------------------------------------------
93  
94    // --- business -------------------------------------------------------------
95  
96    @Override
97    protected String calcPath(final PropertyDescriptor descriptor,
98        final Locale locale)
99    {
100     return context.createMetaInfPathPropertySet(descriptor, locale);
101   }
102 
103   @Override
104   public PropertySetProjectdoc parse(final PropertyDescriptor descriptor,
105       final Locale locale)
106   {
107     try
108     {
109       final PropertySetProjectdoc projectdoc = new PropertySetProjectdoc();
110       parseBase(descriptor, locale, projectdoc);
111       return projectdoc;
112     }
113     catch (final MetaDataException e)
114     {
115       LOG.warn("Cannot parse property set document for property '{}': {}",
116           descriptor, e);
117       return null;
118     }
119   }
120 
121   @Override
122   protected void parseAdditional(final ParserContext context)
123   {
124     final PropertySetProjectdoc projectdoc =
125         (PropertySetProjectdoc) context.getMetaData();
126 
127     addPropertySetComment(projectdoc, context);
128   }
129 
130   private void addPropertySetComment(final PropertySetProjectdoc projectdoc,
131       final ParserContext context)
132   {
133     final Document document = context.getDocument();
134     final Element rootNode = document.getRootElement();
135 
136     final String specification =
137         rootNode.getChildTextNormalize("specification", NS);
138 
139     projectdoc.setComment(specification);
140   }
141 
142   @Override
143   protected Namespace getNs()
144   {
145     return NS;
146   }
147 
148   // --- object basics --------------------------------------------------------
149 
150 }