View Javadoc

1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package net.sf.maven.plugins.eclipseformat;
15  
16  import java.io.File;
17  import java.io.FileInputStream;
18  import java.io.InputStream;
19  import java.util.Properties;
20  
21  import javax.xml.parsers.SAXParser;
22  import javax.xml.parsers.SAXParserFactory;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.logging.Log;
26  
27  import org.codehaus.plexus.util.IOUtil;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  import org.xml.sax.Attributes;
31  import org.xml.sax.helpers.DefaultHandler;
32  
33  /**
34   * Reader for exported Eclipse profile preference (in xml format).
35   * 
36   * @version $Revision: 7 $
37   */
38  class ConfigReader extends DefaultHandler {
39      // constants in xml
40      private static final String SETTING_NODE_NAME = "setting";
41      private static final String ID_ATTR_NAME = "id";
42      private static final String VALUE_ATTR_NAME = "value";
43      private final Properties properties;
44      private final Log log;
45      private final String defaultConfig;
46      private final String alternativeConfig;
47      private final boolean failOnError;
48  
49      /**
50       * Creates a new <code>ConfigReader</code> instance.
51       * 
52       * @param log
53       *            Maven logger.
54       * @param defaultConfig
55       *            default config path.
56       * @param alternativeConfig
57       *            alternative config path.
58       * @param failOnError
59       *            if the build should stop if an error occurred.
60       */
61      public ConfigReader(final Log log, final String defaultConfig, final String alternativeConfig,
62              final boolean failOnError) {
63          super();
64          this.properties = new Properties();
65          this.log = log;
66          this.defaultConfig = defaultConfig;
67          this.alternativeConfig = alternativeConfig;
68          this.failOnError = failOnError;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) {
75          if (SETTING_NODE_NAME.equals(qName)) {
76              String key = attributes.getValue(ID_ATTR_NAME);
77  
78              if (StringUtils.isNotBlank(key)) {
79                  String value = attributes.getValue(VALUE_ATTR_NAME);
80  
81                  if (StringUtils.isNotBlank(value)) {
82                      properties.put(key.trim(), value);
83                  }
84              }
85          }
86      }
87  
88      /**
89       * Gets the preferences.
90       * 
91       * @return the preferences as properties object.
92       * @throws MojoExecutionException
93       *             if reading the config failed.
94       */
95      public Properties getProperties() throws MojoExecutionException {
96          synchronized (properties) {
97  
98              InputStream inputStream = null;
99  
100             try {
101 
102                 // look for user supplied config
103                 if (StringUtils.isNotBlank(alternativeConfig)) {
104                     File configFile = new File(alternativeConfig);
105                     if (configFile.exists()) {
106                         inputStream = new FileInputStream(configFile);
107                     } else {
108                         inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(
109                                 alternativeConfig);
110                     }
111                 }
112 
113                 // get default
114                 if (inputStream == null) {
115                     inputStream = getClass().getResourceAsStream(defaultConfig);
116                 }
117 
118                 try {
119                     SAXParser sparser = SAXParserFactory.newInstance().newSAXParser();
120                     sparser.parse(inputStream, this);
121                 } finally {
122                     IOUtil.close(inputStream);
123                 }
124 
125             } catch (Exception e) {
126                 log.error("Error reading XML config", e);
127                 if (failOnError) {
128                     throw new MojoExecutionException("Error reading XML config", e);
129                 }
130             }
131         }
132 
133         return properties;
134     }
135 }