| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.doclet; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.Serializable; |
| 37 | | | import java.net.MalformedURLException; |
| 38 | | | import java.net.URL; |
| 39 | | | import java.util.ArrayList; |
| 40 | | | import java.util.Collections; |
| 41 | | | import java.util.HashMap; |
| 42 | | | import java.util.List; |
| 43 | | | import java.util.Map; |
| 44 | | | import java.util.logging.Level; |
| 45 | | | import java.util.logging.Logger; |
| 46 | | | |
| 47 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 48 | | | import org.jcoderz.commons.util.ArraysUtil; |
| 49 | | | import org.jcoderz.commons.util.Assert; |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | |
| 55 | | | @author |
| 56 | | | |
| 57 | 0 | | public 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 | | | |
| 64 | 0 | | private static final String CLASSNAME = XmlDocletConfig.class.getName(); |
| 65 | | | |
| 66 | | | |
| 67 | 0 | | 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 | 0 | | final Map tempMap = new HashMap(); |
| 76 | | | |
| 77 | 0 | | final XmlDocletOption outputDirectory |
| 78 | | | = new XmlDocletOption("-d", 2, "Output Directory") |
| 79 | 0 | | { |
| 80 | | | public boolean validOption (XmlDocletConfig cfg, String[] args) |
| 81 | | | throws ArgumentMalformedException |
| 82 | | | { |
| 83 | 0 | | return cfg.parseOutputDirectory(args); |
| 84 | | | } |
| 85 | | | }; |
| 86 | 0 | | tempMap.put(outputDirectory.getOption(), outputDirectory); |
| 87 | 0 | | final XmlDocletOption link |
| 88 | | | = new XmlDocletOption("-link", 2, "Link reference") |
| 89 | 0 | | { |
| 90 | | | public boolean validOption (XmlDocletConfig cfg, String[] args) |
| 91 | | | throws ArgumentMalformedException |
| 92 | | | { |
| 93 | 0 | | return cfg.parseLink(args); |
| 94 | | | } |
| 95 | | | }; |
| 96 | 0 | | tempMap.put(link.getOption(), link); |
| 97 | | | |
| 98 | 0 | | OPTION_MAP = Collections.unmodifiableMap(tempMap); |
| 99 | 0 | | } |
| 100 | | | |
| 101 | | | |
| 102 | | | private File mOutputDirectory; |
| 103 | | | |
| 104 | | | |
| 105 | 0 | | private final List mLinkUrls = new ArrayList(); |
| 106 | | | |
| 107 | | | |
| 108 | | | {@inheritDoc} |
| 109 | | | public Object clone () |
| 110 | | | throws CloneNotSupportedException |
| 111 | | | { |
| 112 | 0 | | return super.clone(); |
| 113 | | | } |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | |
| 118 | | | @param |
| 119 | | | @param |
| 120 | | | @throws |
| 121 | | | |
| 122 | | | @see |
| 123 | | | |
| 124 | | | public void parseOption (String optionValue, String[] args) |
| 125 | | | throws ArgumentMalformedException |
| 126 | | | { |
| 127 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 128 | | | { |
| 129 | 0 | | logger.entering(CLASSNAME, "parseOption(String, String[])", |
| 130 | | | new Object [] {optionValue, ArraysUtil.toString(args)}); |
| 131 | | | } |
| 132 | 0 | | final XmlDocletOption option |
| 133 | | | = (XmlDocletOption) OPTION_MAP.get(optionValue); |
| 134 | | | |
| 135 | 0 | | if (option == null) |
| 136 | | | { |
| 137 | 0 | | logger.fine("Gracefully ignoring unknown option " + optionValue |
| 138 | | | + " with value " + ArraysUtil.toString(args) + "."); |
| 139 | | | } |
| 140 | | | else |
| 141 | | | { |
| 142 | 0 | | if (args.length != (option.getNumberOfArguments() - 1)) |
| 143 | | | { |
| 144 | 0 | | throw new ArgumentMalformedException("number of arguments", |
| 145 | | | String.valueOf(args.length - 1), "The option '" + optionValue |
| 146 | | | + "' expects " + (option.getNumberOfArguments() - 1) |
| 147 | | | + " arguments."); |
| 148 | | | } |
| 149 | 0 | | option.validOption(this, args); |
| 150 | | | } |
| 151 | 0 | | if (logger.isLoggable(Level.FINER)) |
| 152 | | | { |
| 153 | 0 | | logger.exiting(CLASSNAME, "parseOption(String, String[])"); |
| 154 | | | } |
| 155 | 0 | | } |
| 156 | | | |
| 157 | | | |
| 158 | | | |
| 159 | | | |
| 160 | | | {@link } |
| 161 | | | {@link } |
| 162 | | | @param |
| 163 | | | @return |
| 164 | | | @see |
| 165 | | | |
| 166 | | | public int optionLength (String optionValue) |
| 167 | | | { |
| 168 | 0 | | final XmlDocletOption option |
| 169 | | | = (XmlDocletOption) OPTION_MAP.get(optionValue); |
| 170 | | | |
| 171 | | | final int result; |
| 172 | 0 | | if (option == null) |
| 173 | | | { |
| 174 | 0 | | result = OPTION_NOT_KNOWN; |
| 175 | | | } |
| 176 | | | else |
| 177 | | | { |
| 178 | 0 | | result = option.getNumberOfArguments(); |
| 179 | | | } |
| 180 | 0 | | return result; |
| 181 | | | } |
| 182 | | | |
| 183 | | | |
| 184 | | | |
| 185 | | | @return |
| 186 | | | |
| 187 | | | public File getOutputDirectory () |
| 188 | | | { |
| 189 | 0 | | return mOutputDirectory; |
| 190 | | | } |
| 191 | | | |
| 192 | | | private boolean parseLink (String[] args) |
| 193 | | | { |
| 194 | | | URL url; |
| 195 | | | try |
| 196 | | | { |
| 197 | 0 | | url = new URL(args[0]); |
| 198 | 0 | | mLinkUrls.add(url); |
| 199 | | | } |
| 200 | 0 | | catch (MalformedURLException ex) |
| 201 | | | { |
| 202 | 0 | | throw new ArgumentMalformedException("link", args[0], ex.getMessage(), |
| 203 | | | ex); |
| 204 | 0 | | } |
| 205 | | | |
| 206 | | (1) | |
| 207 | 0 | | return false; |
| 208 | | | } |
| 209 | | | |
| 210 | | | |
| 211 | | | private boolean parseOutputDirectory (String[] args) |
| 212 | | | throws ArgumentMalformedException |
| 213 | | | { |
| 214 | 0 | | Assert.notNull(args[0], "d"); |
| 215 | 0 | | mOutputDirectory = new File(args[0]); |
| 216 | 0 | | if (!mOutputDirectory.isDirectory()) |
| 217 | | | { |
| 218 | 0 | (2) | throw new ArgumentMalformedException("outputDirectory", args[0], |
| 219 | | | "Output directory must be set to a directory."); |
| 220 | | | } |
| 221 | 0 | | return true; |
| 222 | | | } |
| 223 | | | |
| 224 | | | |
| 225 | 0 | | 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 | 0 | | { |
| 233 | 0 | | mOption = option; |
| 234 | 0 | | mNumberOfArguments = numberOfArguments; |
| 235 | 0 | | mShortDescription = description; |
| 236 | 0 | | } |
| 237 | | | |
| 238 | | | abstract boolean validOption (XmlDocletConfig cfg, String [] args) |
| 239 | | | throws ArgumentMalformedException; |
| 240 | | | |
| 241 | | | int getNumberOfArguments () |
| 242 | | | { |
| 243 | 0 | | return mNumberOfArguments; |
| 244 | | | } |
| 245 | | | |
| 246 | | | String getOption () |
| 247 | | | { |
| 248 | 0 | | return mOption; |
| 249 | | | } |
| 250 | | | |
| 251 | | | String getShortDescription () |
| 252 | | | { |
| 253 | 0 | | return mShortDescription; |
| 254 | | | } |
| 255 | | | } |
| 256 | | | } |