root/trunk/src/java/org/jcoderz/commons/doclet/XmlDocletConfig.java

Revision 1011, 7.8 kB (checked in by amandel, 4 years ago)

Aligned svn keyword settings.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 *
4 * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 *    * Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *    * Redistributions in binary form must reproduce the above
13 *      copyright notice, this list of conditions and the following
14 *      disclaimer in the documentation and/or other materials
15 *      provided with the distribution.
16 *    * Neither the name of the jCoderZ.org Project nor the names of
17 *      its contributors may be used to endorse or promote products
18 *      derived from this software without specific prior written
19 *      permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33package org.jcoderz.commons.doclet;
34
35import java.io.File;
36import java.io.Serializable;
37import java.net.MalformedURLException;
38import java.net.URL;
39import java.util.ArrayList;
40import java.util.Collections;
41import java.util.HashMap;
42import java.util.List;
43import java.util.Map;
44import java.util.logging.Level;
45import java.util.logging.Logger;
46
47import org.jcoderz.commons.ArgumentMalformedException;
48import org.jcoderz.commons.util.ArraysUtil;
49import org.jcoderz.commons.util.Assert;
50
51
52/**
53 * Value object for the XmlDoclet configuration values.
54 *
55 * @author Andreas Mandel
56 */
57public final class XmlDocletConfig
58      implements Serializable, Cloneable
59{
60   static final int OPTION_NOT_KNOWN = 0;
61   static final int ERROR_WITH_OPTION = -1;
62
63   /** The full qualified name of this class. */
64   private static final String CLASSNAME = XmlDocletConfig.class.getName();
65
66   /** The logger to use. */
67   private static final Logger logger = Logger.getLogger(CLASSNAME);
68
69   private static final long serialVersionUID = -202900277699915981L;
70
71   private static final Map OPTION_MAP;
72
73   static
74   {
75      final Map tempMap = new HashMap();
76
77      final XmlDocletOption outputDirectory
78            = new XmlDocletOption("-d", 2, "Output Directory")
79            {
80               public boolean validOption (XmlDocletConfig cfg, String[] args)
81                     throws ArgumentMalformedException
82               {
83                  return cfg.parseOutputDirectory(args);
84               }
85            };
86      tempMap.put(outputDirectory.getOption(), outputDirectory);
87      final XmlDocletOption link
88            = new XmlDocletOption("-link", 2, "Link reference")
89            {
90               public boolean validOption (XmlDocletConfig cfg, String[] args)
91                     throws ArgumentMalformedException
92               {
93                  return cfg.parseLink(args);
94               }
95            };
96      tempMap.put(link.getOption(), link);
97
98      OPTION_MAP = Collections.unmodifiableMap(tempMap);
99   }
100
101   /** Output directory, to generate the output to. */
102   private File mOutputDirectory;
103
104   /** List of link urls. */
105   private final List mLinkUrls = new ArrayList();
106
107
108   /** {@inheritDoc} */
109   public Object clone ()
110         throws CloneNotSupportedException
111   {
112      return super.clone();
113   }
114
115   /**
116    * Parses the given option with the arguments and updates this
117    * option object accordingly.
118    * @param optionValue the option to be set.
119    * @param args the arguments that came with this option.
120    * @throws ArgumentMalformedException if the option could not be
121    *       parsed.
122    * @see com.sun.javadoc.Doclet#validOptions(java.lang.String[][], com.sun.javadoc.DocErrorReporter)
123    */
124   public void parseOption (String optionValue, String[] args)
125         throws ArgumentMalformedException
126   {
127      if (logger.isLoggable(Level.FINER))
128      {
129         logger.entering(CLASSNAME, "parseOption(String, String[])",
130               new Object [] {optionValue, ArraysUtil.toString(args)});
131      }
132      final XmlDocletOption option
133            = (XmlDocletOption) OPTION_MAP.get(optionValue);
134
135      if (option == null)
136      {
137          logger.fine("Gracefully ignoring unknown option " + optionValue
138              + " with value " + ArraysUtil.toString(args) + ".");
139      }
140      else
141      {
142          if (args.length != (option.getNumberOfArguments() - 1))
143          {
144             throw new ArgumentMalformedException("number of arguments",
145                   String.valueOf(args.length - 1), "The option '" + optionValue
146                   + "' expects " + (option.getNumberOfArguments() - 1)
147                   + " arguments.");
148          }
149          option.validOption(this, args);
150      }
151      if (logger.isLoggable(Level.FINER))
152      {
153         logger.exiting(CLASSNAME, "parseOption(String, String[])");
154      }
155   }
156
157   /**
158    * Returns the option length for the given option. A value of 2
159    * implies that the argument takes one option. The value 0
160    * {@link #OPTION_NOT_KNOWN} means that the option is unknown and a
161    * value of -1 {@link #ERROR_WITH_OPTION} denotes an error.
162    * @param optionValue the option to be checked.
163    * @return the option length for the given option.
164    * @see com.sun.javadoc.Doclet#optionLength(java.lang.String)
165    */
166   public int optionLength (String optionValue)
167   {
168      final XmlDocletOption option
169            = (XmlDocletOption) OPTION_MAP.get(optionValue);
170
171      final int result;
172      if (option == null)
173      {
174         result = OPTION_NOT_KNOWN;
175      }
176      else
177      {
178         result = option.getNumberOfArguments();
179      }
180      return result;
181   }
182
183   /**
184    * Returns the output directory set.
185    * @return the output directory set.
186    */
187   public File getOutputDirectory ()
188   {
189      return mOutputDirectory;
190   }
191
192   private boolean parseLink (String[] args)
193   {
194      URL url;
195      try
196      {
197         url = new URL(args[0]);
198         mLinkUrls.add(url);
199      }
200      catch (MalformedURLException ex)
201      {
202         throw new ArgumentMalformedException("link", args[0], ex.getMessage(),
203               ex);
204      }
205
206      // TODO Auto-generated method stub
207      return false;
208   }
209
210
211   private boolean parseOutputDirectory (String[] args)
212         throws ArgumentMalformedException
213   {
214      Assert.notNull(args[0], "d");
215      mOutputDirectory = new File(args[0]);
216      if (!mOutputDirectory.isDirectory())
217      {
218         throw new ArgumentMalformedException("outputDirectory", args[0],
219               "Output directory must be set to a directory.");
220      }
221      return true;
222   }
223
224
225   private abstract static class XmlDocletOption
226   {
227      private final String mOption;
228      private final int mNumberOfArguments;
229      private final String mShortDescription;
230
231      XmlDocletOption (String option, int numberOfArguments, String description)
232      {
233         mOption = option;
234         mNumberOfArguments = numberOfArguments;
235         mShortDescription = description;
236      }
237
238      abstract boolean validOption (XmlDocletConfig cfg, String [] args)
239            throws ArgumentMalformedException;
240
241      int getNumberOfArguments ()
242      {
243         return mNumberOfArguments;
244      }
245
246      String getOption ()
247      {
248         return mOption;
249      }
250
251      String getShortDescription ()
252      {
253         return mShortDescription;
254      }
255   }
256}
Note: See TracBrowser for help on using the browser.