code
stringlengths
25
201k
docstring
stringlengths
19
96.2k
func_name
stringlengths
0
235
language
stringclasses
1 value
repo
stringlengths
8
51
path
stringlengths
11
314
url
stringlengths
62
377
license
stringclasses
7 values
public void addClassPath() { String path = null; try { path = System.getProperty ("java.class.path"); } catch (Exception ex) { path= ""; log.error ("Unable to get class path", ex); } if (path != null) { StringTokenizer tok = new StringTokenizer (path, File.pathSeparator); while (tok.hasMoreTokens()) add (new File (tok.nextToken())); } }
Add the contents of the classpath.
addClassPath
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
MIT
public void clear() { urlList.clear(); }
Clear the stored files in this object.
clear
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
MIT
public ClassLoader createClassLoader() throws SecurityException { return new URLClassLoader (urlList.toArray (new URL[urlList.size()]), getClass().getClassLoader()); }
Create and return a class loader that will search the additional places defined in this builder. The resulting class loader uses the default delegation parent <tt>ClassLoader</tt>. @return a new class loader @throws SecurityException if a security manager exists and its <tt>checkCreateClassLoader()</tt> method does not allow creation of a class loader
createClassLoader
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
MIT
public ClassLoader createClassLoader (ClassLoader parentLoader) throws SecurityException { return new URLClassLoader (urlList.toArray (new URL[urlList.size()]), parentLoader); }
Create and return a class loader that will search the additional places defined in this builder. The resulting class loader uses the specified parent <tt>ClassLoader</tt>. @param parentLoader the desired parent class loader @return a new class loader @throws SecurityException if a security manager exists and its <tt>checkCreateClassLoader()</tt> method does not allow creation of a class loader
createClassLoader
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassLoaderBuilder.java
MIT
public boolean accept (ClassInfo classInfo, ClassFinder classFinder) { return ((classInfo.getModifier() & modifiers) != 0); }
Tests whether a class name should be included in a class name list. @param classInfo the loaded information about the class @param classFinder the {@link ClassFinder} that called this filter (mostly for access to <tt>ClassFinder</tt> utility methods) @return <tt>true</tt> if and only if the name should be included in the list; <tt>false</tt> otherwise
accept
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassModifiersClassFilter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassModifiersClassFilter.java
MIT
public static boolean fileCanContainClasses (File file) { boolean can = false; String fileName = file.getPath(); if (file.exists()) { can = ((fileName.toLowerCase().endsWith (".jar")) || (fileName.toLowerCase().endsWith (".zip")) || (file.isDirectory())); } return can; }
Determine whether a file is a jar file, zip file or directory (i.e., represents places that can be searched for classes). @param file the file to check @return <tt>true</tt> if the file represents a place that can be searched for classes, <tt>false</tt> if not
fileCanContainClasses
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassUtil.java
MIT
public static String getShortClassName (String fullClassName) { String shortClassName = fullClassName; int i = shortClassName.lastIndexOf ('.'); if ( (i != -1) && (++i < shortClassName.length()) ) shortClassName = shortClassName.substring (i); return shortClassName; }
Strip the package name from a fully-qualified class name and return just the short class name. @param fullClassName the full class name @return the short name
getShortClassName
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassUtil.java
MIT
public static String getShortClassName (Class cls) { return getShortClassName (cls.getName()); }
Strip the package name from a fully-qualified class name and return just the short class name. @param cls the <tt>Class</tt> object whose name is to be trimmed @return the short name
getShortClassName
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassUtil.java
MIT
public static Object instantiateClass(String className) throws ClassUtilException { try { Class cls = Class.forName(className); return cls.newInstance(); } catch (ClassNotFoundException ex) { throw new ClassUtilException("Can't load class " + className, ex); } catch (ClassCastException ex) { throw new ClassUtilException("Can't load class " + className, ex); } catch (IllegalAccessException ex) { throw new ClassUtilException("Can't load class " + className, ex); } catch (InstantiationException ex) { throw new ClassUtilException("Can't load class " + className, ex); } }
Convenience method that loads a class and attempts to instantiate it via its default constructor. This method catches all the explicit, checked exceptions that can occur and wraps them in a {@link ClassUtilException}, reducing the number of lines of code necessary to instantiate a class given its name. Note that this method <i>only</i> catches and wraps checked exceptions. Unchecked exceptions, such as <tt>ExceptionInInitializerError</tt>, are propagated directly to the caller. @param className the fully-qualified class name @return the instantiated object @throws ClassUtilException on error
instantiateClass
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/ClassUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/ClassUtil.java
MIT
public int getAccess() { return access; }
Get the access modifiers for this field @return the access modifiers, or 0 if none are set.
getAccess
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public String getName() { return name; }
Get the field name. @return the field name
getName
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public String getDescription() { return description; }
Get the field description, if any. @return the field description, or null
getDescription
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public String getSignature() { return signature; }
Get the field's signature, if any. @return the field signature, or null.
getSignature
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public Object getValue() { return value; }
Get the field value, if any. @return the field value, or null.
getValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public int hashCode() { return signature.hashCode(); }
Get the hash code. The hash code is based on the field's name. @return the hash code
hashCode
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public int compareTo(FieldInfo other) { return this.signature.compareTo(other.signature); }
Compare this object and another <tt>FieldInfo</tt> object. The two objects are compared by their signature fields. @param other the other object @return a negative integer, zero, or a positive integer, as this object is less than, equal to, or greater than the specified object.
compareTo
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public boolean equals(Object other) { boolean result; if (other instanceof FieldInfo) result = compareTo((FieldInfo) other) == 0; else result = false; return result; }
Compare this object to another one for equality. If the other object is a <tt>FieldInfo</tt> instance, the two will be compared by signature. @param other the other object @return <tt>true</tt> if <tt>other</tt> is a <tt>FieldInfo</tt> object and it has the same signature as this object, <tt>false</tt> otherwise.
equals
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/FieldInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/FieldInfo.java
MIT
public String[] getExceptions() { return exceptions; }
Get the class names of the thrown exceptions @return the names of the thrown exceptions, or null.
getExceptions
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/MethodInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/MethodInfo.java
MIT
@Override public int hashCode() { return signature.hashCode(); }
Get the hash code. The hash code is based on the field's signature. @return the hash code
hashCode
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/MethodInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/MethodInfo.java
MIT
public int compareTo(MethodInfo other) { return this.signature.compareTo(other.signature); }
Compare this object and another <tt>MethodInfo</tt> object. The two objects are compared by their signature fields. @param other the other object @return a negative integer, zero, or a positive integer, as this object is less than, equal to, or greater than the specified object.
compareTo
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/MethodInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/MethodInfo.java
MIT
public boolean equals(Object other) { boolean result; if (other instanceof MethodInfo) result = compareTo((MethodInfo) other) == 0; else result = false; return result; }
Compare this object to another one for equality. If the other object is a <tt>MethodInfo</tt> instance, the two will be compared by signature. @param other the other object @return <tt>true</tt> if <tt>other</tt> is a <tt>MethodInfo</tt> object and it has the same signature as this object, <tt>false</tt> otherwise.
equals
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/MethodInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/MethodInfo.java
MIT
public boolean accept (ClassInfo classInfo, ClassFinder classFinder) { return ! this.filter.accept (classInfo, classFinder); }
Tests whether a class name should be included in a class name list. @param classInfo the {@link ClassInfo} object to test @param classFinder the invoking {@link ClassFinder} object @return <tt>true</tt> if and only if the name should be included in the list; <tt>false</tt> otherwise
accept
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/NotClassFilter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/NotClassFilter.java
MIT
public boolean accept (ClassInfo classInfo, ClassFinder classFinder) { boolean accepted = false; if (filters.size() == 0) accepted = true; else { for (ClassFilter filter : filters) { accepted = filter.accept (classInfo, classFinder); if (accepted) break; } } return accepted; }
<p>Determine whether a class name is to be accepted or not, based on the contained filters. The class name name is accepted if any one of the contained filters accepts it. This method stops looping over the contained filters as soon as it encounters one whose {@link ClassFilter#accept accept()} method returns <tt>true</tt> (implementing a "short-circuited OR" operation.)</p> <p>If the set of contained filters is empty, then this method returns <tt>true</tt>.</p> @param classInfo the {@link ClassInfo} object to test @param classFinder the invoking {@link ClassFinder} object @return <tt>true</tt> if the name matches, <tt>false</tt> if it doesn't
accept
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/OrClassFilter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/OrClassFilter.java
MIT
public boolean accept (ClassInfo classInfo, ClassFinder classFinder) { return pattern.matcher (classInfo.getClassName()).find(); }
Determine whether a class name is to be accepted or not, based on the regular expression specified to the constructor. @param classInfo the {@link ClassInfo} object to test @param classFinder the invoking {@link ClassFinder} object @return <tt>true</tt> if the class name matches, <tt>false</tt> if it doesn't
accept
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/RegexClassFilter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/RegexClassFilter.java
MIT
public boolean accept (ClassInfo classInfo, ClassFinder classFinder) { Map<String,ClassInfo> superClasses = new HashMap<String,ClassInfo>(); if (baseClass.isInterface()) classFinder.findAllInterfaces (classInfo, superClasses); else classFinder.findAllSuperClasses (classInfo, superClasses); return superClasses.keySet().contains (baseClass.getName()); }
Perform the acceptance test on the loaded <tt>Class</tt> object. @param classInfo the {@link ClassInfo} object to test @param classFinder the invoking {@link ClassFinder} object @return <tt>true</tt> if the class name matches, <tt>false</tt> if it doesn't
accept
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/classutil/SubclassClassFilter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/classutil/SubclassClassFilter.java
MIT
public final void execute (String[] args) throws CommandLineException { try { paramParser = new ParameterParser(getUsageInfo()); parseParams (args); runCommand(); } catch (CommandLineUsageException ex) { usage (ex.getMessage()); throw ex; } catch (CommandLineException ex) { throw ex; } catch (Exception ex) { throw new CommandLineException (ex); } }
Called to initiate execution of the command line utility. This method <ul> <li>parse the parameters <li>call the {@link #runCommand()} method, which must be provided by the subclass, to initiate processing. </ul> @param args The command-line parameters @throws CommandLineException command failed
execute
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected void parseCustomOption (char shortOption, String longOption, Iterator<String> it) throws CommandLineUsageException, NoSuchElementException { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.parseCustomOption", "(BUG) custom option found, but class {0} provides no " + "parseCustomOption method.", new Object[] {this.getClass().getName()}); }
Called by <tt>parseParams()</tt> to handle any option it doesn't recognize. If the option takes any parameters, the overridden method must extract the parameter by advancing the supplied <tt>Iterator</tt> (which returns <tt>String</tt> objects). This default method simply throws an exception. @param shortOption the short option character, or {@link UsageInfo#NO_SHORT_OPTION} if there isn't one (i.e., if this is a long-only option). @param longOption the long option string, without any leading "-" characters, or null if this is a short-only option @param it the <tt>Iterator</tt> for the remainder of the command line, for extracting parameters. @throws CommandLineUsageException on error @throws NoSuchElementException overran the iterator (i.e., missing parameter)
parseCustomOption
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected void processPostOptionCommandLine (Iterator<String> it) throws CommandLineUsageException, NoSuchElementException { if (it.hasNext()) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.extraParams", "Extra command line parameter(s)."); } }
<p>Called by <tt>parseParams()</tt> once option parsing is complete, this method must handle any additional parameters on the command line. It's not necessary for the method to ensure that the iterator has the right number of strings left in it. If you attempt to pull too many parameters from the iterator, it'll throw a <tt>NoSuchElementException</tt>, which <tt>parseParams()</tt> traps and converts into a suitable error message. Similarly, if there are any parameters left in the iterator when this method returns, <tt>parseParams()</tt> throws an exception indicating that there are too many parameters on the command line.</p> <p>This method is called unconditionally, even if there are no parameters left on the command line, so it's a useful place to do post-option consistency checks, as well.</p> @param it the <tt>Iterator</tt> for the remainder of the command line @throws CommandLineUsageException on error @throws NoSuchElementException attempt to iterate past end of args; <tt>parseParams()</tt> automatically handles this exception, so it's safe for subclass implementations of this method not to handle it
processPostOptionCommandLine
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected void getCustomUsageInfo (UsageInfo info) { // Default: does nothing }
Called by <tt>parseParams()</tt> to get the custom command-line options and parameters handled by the subclass. This list is used solely to build a usage message. The overridden method must fill the supplied <tt>UsageInfo</tt> object: <ul> <li> Each parameter must be added to the object, via the {@link UsageInfo#addParameter UsageInfo.addParameter()} method. <li> Each option must be added to the object, via the {@link UsageInfo#addOption UsageInfo.addOption()} method. </ul> That information will be combined with the common options supported by the base class, and used to build a usage message. @param info The <tt>UsageInfo</tt> object to fill. @see UsageInfo#addOption @see UsageInfo#addParameter
getCustomUsageInfo
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected int parseIntParameter (String value) throws CommandLineUsageException { try { return Integer.parseInt (value); } catch (NumberFormatException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.badIntegerParam", "Bad integer command line parameter \"{0}\"", new Object[] {value}); } }
Convenience method that parses an integer value, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. @param value the string value to parse @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseIntParameter(String,int,int) @see #parseIntOptionArgument(char,String,String) @see #parseDoubleParameter(String) @see #parseFloatParameter(String)
parseIntParameter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected int parseIntParameter (String value, int min, int max) throws CommandLineUsageException { int result = parseIntParameter (value); if (result < min) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericParamTooSmall", "Numeric parameter {0} is less than the " + "minimum legal value of {1}", new Object[] { value, String.valueOf (min) }); } if (result > max) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericParamTooLarge", "Numeric parameter {0} is greater than the " + "maximum legal value of {1}", new Object[] { value, String.valueOf (max) }); } return result; }
Convenience method that parses an integer value, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. This version of the method also does range checking. @param value the string value to parse @param min the minimum legal value for the result @param max the maximum legal value for the result @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseIntParameter(String) @see #parseIntOptionArgument(char,String,String,int,int) @see #parseDoubleParameter(String,double,double) @see #parseFloatParameter(String,float,float)
parseIntParameter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected int parseIntOptionArgument (char shortOption, String longOption, String value) throws CommandLineUsageException { try { return Integer.parseInt (value); } catch (NumberFormatException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.badIntegerOption", "Bad integer command line parameter \"{0}\" " + "for option {1}", new Object[] { value, getOptionStringForError (shortOption, longOption) }); } }
Convenience method that parses an integer option argument, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. @param shortOption short option, as passed to {@link #parseCustomOption} @param longOption long option, as passed to {@link #parseCustomOption} @param value the string value to parse @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseIntOptionArgument(char,String,String,int,int) @see #parseDoubleOptionArgument(char,String,String) @see #parseFloatOptionArgument(char,String,String)
parseIntOptionArgument
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected int parseIntOptionArgument (char shortOption, String longOption, String value, int min, int max) throws CommandLineUsageException { int result = parseIntOptionArgument (shortOption, longOption, value); if (result < min) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericOptionParamTooSmall", "Numeric parameter {0} for option {1} is less " + "than the minimum legal value of {2}", new Object[] { value, getOptionStringForError (shortOption, longOption), String.valueOf (min) }); } if (result > max) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericOptionParamTooLarge", "Numeric parameter {0} for option {1} is " + "greater than the maximum legal value of {2}", new Object[] { value, getOptionStringForError (shortOption, longOption), String.valueOf (max) }); } return result; }
Convenience method that parses an integer option argument, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. This version of the method also does range checking. @param shortOption short option, as passed to {@link #parseCustomOption} @param longOption long option, as passed to {@link #parseCustomOption} @param value the string value to parse @param min the minimum legal value for the result @param max the maximum legal value for the result @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseIntOptionArgument(char,String,String,int,int) @see #parseDoubleOptionArgument(char,String,String) @see #parseFloatOptionArgument(char,String,String)
parseIntOptionArgument
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected float parseFloatParameter (String value) throws CommandLineUsageException { try { return Float.parseFloat (value); } catch (NumberFormatException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.badFloatParam", "Bad floating point command line parameter " + "\"{0}\"", new Object[] {value}); } }
Convenience method that parses a floating point value, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. @param value the string value to parse @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseFloatParameter(String,float,float) @see #parseFloatOptionArgument(char,String,String) @see #parseDoubleParameter(String) @see #parseIntParameter(String)
parseFloatParameter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected float parseFloatParameter (String value, float min, float max) throws CommandLineUsageException { float result = parseFloatParameter (value); if (result < min) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericParamTooSmall", "Numeric parameter {0} is less than the " + "minimum legal value of {1}", new Object[] { value, String.valueOf (min) }); } if (result > max) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericParamTooLarge", "Numeric parameter {0} is greater than the " + "maximum legal value of {1}", new Object[] { value, String.valueOf (max) }); } return result; }
Convenience method that parses a floating point value, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. This version of the method also does range checking. @param value the string value to parse @param min the minimum legal value for the result @param max the maximum legal value for the result @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseFloatParameter(String) @see #parseFloatOptionArgument(char,String,String,float,float) @see #parseDoubleParameter(String,double,double) @see #parseIntParameter(String,int,int)
parseFloatParameter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected float parseFloatOptionArgument (char shortOption, String longOption, String value) throws CommandLineUsageException { try { return Integer.parseInt (value); } catch (NumberFormatException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.badFloatParam", "Bad floating point command line parameter " + "\"{0}\" for option {1}", new Object[] { value, getOptionStringForError (shortOption, longOption) }); } }
Convenience method that parses a float point option argument, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. @param shortOption short option, as passed to {@link #parseCustomOption} @param longOption long option, as passed to {@link #parseCustomOption} @param value the string value to parse @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseFloatOptionArgument(char,String,String,float,float) @see #parseDoubleOptionArgument(char,String,String) @see #parseIntOptionArgument(char,String,String)
parseFloatOptionArgument
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected float parseFloatOptionArgument (char shortOption, String longOption, String value, float min, float max) throws CommandLineUsageException { float result = parseFloatOptionArgument (shortOption, longOption, value); if (result < min) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericOptionParamTooSmall", "Numeric parameter {0} for option {1} is less " + "than the minimum legal value of {2}", new Object[] { value, getOptionStringForError (shortOption, longOption), String.valueOf (min) }); } if (result > max) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericOptionParamTooLarge", "Numeric parameter {0} for option {1} is " + "greater than the maximum legal value of {2}", new Object[] { value, getOptionStringForError (shortOption, longOption), String.valueOf (max) }); } return result; }
Convenience method that parses a floating point option argument, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. This version of the method also does range checking. @param shortOption short option, as passed to {@link #parseCustomOption} @param longOption long option, as passed to {@link #parseCustomOption} @param value the string value to parse @param min the minimum legal value for the result @param max the maximum legal value for the result @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseFloatOptionArgument(char,String,String,float,float) @see #parseDoubleOptionArgument(char,String,String) @see #parseIntOptionArgument(char,String,String)
parseFloatOptionArgument
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected double parseDoubleParameter (String value) throws CommandLineUsageException { try { return Double.parseDouble (value); } catch (NumberFormatException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.badDoubleParam", "Bad double floating point command line " + "parameter \"{0}\"", new Object[] {value}); } }
Convenience method that parses a double value, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. @param value the string value to parse @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseDoubleParameter(String,double,double) @see #parseDoubleOptionArgument(char,String,String) @see #parseFloatParameter(String) @see #parseIntParameter(String)
parseDoubleParameter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected double parseDoubleParameter (String value, double min, double max) throws CommandLineUsageException { double result = parseDoubleParameter (value); if (result < min) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericParamTooSmall", "Numeric parameter {0} is less than the " + "minimum legal value of {1}", new Object[] { value, String.valueOf (min) }); } if (result > max) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericParamTooLarge", "Numeric parameter {0} is greater than the " + "maximum legal value of {1}", new Object[] { value, String.valueOf (max) }); } return result; }
Convenience method that parses a double value, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. This version of the method also does range checking. @param value the string value to parse @param min the minimum legal value for the result @param max the maximum legal value for the result @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseDoubleParameter(String) @see #parseDoubleOptionArgument(char,String,String,double,double) @see #parseFloatParameter(String,float,float) @see #parseIntParameter(String,int,int)
parseDoubleParameter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected double parseDoubleOptionArgument (char shortOption, String longOption, String value) throws CommandLineUsageException { try { return Double.parseDouble (value); } catch (NumberFormatException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.badDoubleOption", "Bad double command line parameter " + "\"{0}\" for option {1}", new Object[] { value, getOptionStringForError (shortOption, longOption) }); } }
Convenience method that parses a double option argument, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. @param shortOption short option, as passed to {@link #parseCustomOption} @param longOption long option, as passed to {@link #parseCustomOption} @param value the string value to parse @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseDoubleOptionArgument(char,String,String,double,double) @see #parseDoubleOptionArgument(char,String,String) @see #parseIntOptionArgument(char,String,String)
parseDoubleOptionArgument
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
protected double parseDoubleOptionArgument (char shortOption, String longOption, String value, double min, double max) throws CommandLineUsageException { double result = parseDoubleOptionArgument (shortOption, longOption, value); if (result < min) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericOptionParamTooSmall", "Numeric parameter {0} for option {1} is less " + "than the minimum legal value of {2}", new Object[] { value, getOptionStringForError (shortOption, longOption), String.valueOf (min) }); } if (result > max) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.numericOptionParamTooSmall", "Numeric parameter {0} for option {1} is " + "greater than the maximum legal value of {2}", new Object[] { value, getOptionStringForError (shortOption, longOption), String.valueOf (max) }); } return result; }
Convenience method that parses a double floating point option argument, throwing a <tt>CommandLineUsageException</tt>, not a <tt>NumberFormatException</tt>, on error. This version of the method also does range checking. @param shortOption short option, as passed to {@link #parseCustomOption} @param longOption long option, as passed to {@link #parseCustomOption} @param value the string value to parse @param min the minimum legal value for the result @param max the maximum legal value for the result @return the numeric value @throws CommandLineUsageException bad numeric value @see #parseDoubleOptionArgument(char,String,String,double,double) @see #parseDoubleOptionArgument(char,String,String) @see #parseIntOptionArgument(char,String,String)
parseDoubleOptionArgument
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/CommandLineUtility.java
MIT
public void parse(String[] params, ParameterHandler paramHandler) throws CommandLineUsageException { ArrayIterator<String> it = new ArrayIterator<String>(params); try { while (it.hasNext()) { String arg = it.next(); if (! (arg.charAt(0) == UsageInfo.SHORT_OPTION_PREFIX) ) { // Move iterator back, since we've already advanced // past the last option and retrieved the first // non-option. it.previous(); break; } // First, verify that the option is legal. OptionInfo optionInfo = null; if (arg.length() == 2) optionInfo = usageInfo.getOptionInfo(arg.charAt(1)); else { if (! arg.startsWith(UsageInfo.LONG_OPTION_PREFIX)) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.badLongOption", "Option \"{0}\" is not a single-character " + "short option, but it does not start with " + "\"{1}\", as long options must.", new Object[] {arg, UsageInfo.LONG_OPTION_PREFIX}); } optionInfo = usageInfo.getOptionInfo(arg.substring(2)); } if (optionInfo == null) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.unknownOption", "Unknown option: \"{0}\"", new Object[] {arg}); } // Okay, now handle the options. paramHandler.parseOption(optionInfo.shortOption, optionInfo.longOption, it); } paramHandler.parsePostOptionParameters(it); // Should be no parameters left now. if (it.hasNext()) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.tooManyParams", "Too many parameters."); } } catch (NoSuchElementException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.missingParams", "Missing command line parameter(s)."); } catch (ArrayIndexOutOfBoundsException ex) { throw new CommandLineUsageException (Package.BUNDLE_NAME, "CommandLineUtility.missingParams", "Missing command line parameter(s)."); } }
Parse a set of command-line parameters. The {@link UsageInfo} object dictates the arguments to be parsed. The {@link ParameterHandler} object handles the encountered parameters. @param params parameters to parse @param paramHandler handles the arguments @throws CommandLineUsageException on error
parse
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/ParameterParser.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/ParameterParser.java
MIT
public String getUsageMessage(String prefixMsg) { return getUsageMessage(prefixMsg, 0); }
Generate a usage message. @param prefixMsg prefix (e.g., error) message to use, or null @return the usage string, with embedded newlines
getUsageMessage
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/ParameterParser.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/ParameterParser.java
MIT
public String getCommandName() { return this.commandName; }
Get the command name to be used in the usage output. If not specified, <tt>java classname</tt> is used. @return the command name, or null if not set @see #setCommandName
getCommandName
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/UsageInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/UsageInfo.java
MIT
public void setCommandName (String commandName) { this.commandName = commandName; }
Set the command name to be used in the usage output. If not specified, <tt>java classname</tt> is used. @param commandName the command name @see #getCommandName
setCommandName
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/UsageInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/UsageInfo.java
MIT
public void addOption (char shortOption, String longOption, String explanation) { addOption (shortOption, longOption, null, explanation); }
Add an option and its explanation to the usage information. This method is shorthand for: <blockquote><pre>{@link #addOption(char,String,String,String) addOption(}(shortOption, longOption, null, explanation)</pre></blockquote> That is, it's useful for options that take no parameters. @param shortOption The single-character short option (e.g., 'a' for "-a"), or {@link #NO_SHORT_OPTION} to indicate that there is no short option. @param longOption The corresponding long option, if any, or null. The option should be specified without any leading "-" character (e.g., "logging", not "--logging"). @param explanation A one-line explanation for the option. The line can be as long as you want, and can contain multiple sentences, but it must not contain a newline. It will be automatically broken up into multiple lines as necessary. If the explanation is null, then the option is "hidden" (i.e., not displayed in the usage message). This is useful, for instance, when you've deprecated an option but are retaining it for backward compatibility.
addOption
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/UsageInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/UsageInfo.java
MIT
public void addOption (char shortOption, String longOption, String argToken, String explanation) { if ((longOption != null) && (longOption.charAt(0) == '-')) { throw new IllegalArgumentException ("(BUG) Long option \"" + longOption + "\", registered via UsageInfo.addOption(), starts " + "with \"-\"."); } if ((shortOption == NO_SHORT_OPTION) && (longOption == null)) { throw new IllegalArgumentException ("(BUG) shortOption parameter is NO_SHORT_OPTION, and " + "longOption parameter is null."); } OptionInfo optionInfo = new OptionInfo (shortOption, longOption, argToken, explanation); if (shortOption != NO_SHORT_OPTION) shortOptionMap.put (shortOption, optionInfo); if (longOption != null) longOptionMap.put (longOption, optionInfo); allOptions.add (optionInfo); }
Add an option and its explanation to the usage information. Examples: <blockquote><pre> usageInfo.addOption ('n', "total-images", "num", "Total number of images to generate"); usageInfo.addOption ('v', "verbose", null, "Enable verbose mode") </pre></blockquote> @param shortOption The single-character short option (e.g., 'a' for "-a"), or {@link #NO_SHORT_OPTION} to indicate that there is no short option. @param longOption The corresponding long option, if any, or null. The option should be specified without any leading "-" character (e.g., "logging", not "--logging"). @param argToken A token to represent the option's parameter, if any, in the generated usage message. null if the option takes no parameters. @param explanation A one-line explanation for the option. The line can be as long as you want, and can contain multiple sentences, but it must not contain a newline. It will be automatically broken up into multiple lines as necessary. If the explanation is null, then the option is "hidden" (i.e., not displayed in the usage message). This is useful, for instance, when you've deprecated an option but are retaining it for backward compatibility.
addOption
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/UsageInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/UsageInfo.java
MIT
public void addParameter (String param, String explanation, boolean required) { if (param.charAt(0) == '-') { throw new IllegalArgumentException ("(BUG) Option \"" + param + "\" registered via UsageInfo.addParameter(), instead of " + "via UsageInfo.addOption()."); } paramMap.put (param, explanation); paramNames.add (param); if (required) requiredParams.add (param); }
Add a positional parameter (i.e., one that follows the options) and its explanation to the usage information at the end of the list of positional parameters. @param param The parameter placeholder string @param explanation A one-line explanation for the parameter. The line can be as long as you want, and can contain multiple sentences, but it must not contain a newline. It will be automatically broken up into multiple lines as necessary. @param required <tt>true</tt> if the parameter is required, <tt>false</tt> if the parameter is optional
addParameter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/UsageInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/UsageInfo.java
MIT
public void addUsagePrologue (String prologue) { this.usagePrologue = prologue; }
Add a prologue to be displayed before the standard usage message. The prologue string should be one line; it'll be broken up, wrapped, and displayed as a paragraph that precedes the usage message. @param prologue the prologue string @see #addUsageTrailer
addUsagePrologue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/cmdline/UsageInfo.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/cmdline/UsageInfo.java
MIT
public void addSection (String sectionName) throws SectionExistsException { if (sectionsByName.get (sectionName) != null) throw new SectionExistsException (sectionName); makeNewSection (sectionName); }
Add a new section to this configuration data. @param sectionName the name of the new section @throws SectionExistsException a section by that name already exists @see #containsSection @see #getSectionNames @see #setVariable
addSection
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void clear() { sectionsInOrder.clear(); sectionsByName.clear(); configURL = null; }
Clear this object of all configuration data.
clear
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public final boolean containsSection (String sectionName) { return (sectionsByName.get (sectionName) != null); }
Determine whether this object contains a specified section. @param sectionName the section name @return <tt>true</tt> if the section exists in this configuration, <tt>false</tt> if not. @see #getSectionNames @see #addSection
containsSection
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public URL getConfigurationFileURL() { return configURL; }
Get the URL of the configuration file, if available. @return the URL of the configuration file, or null if the file was parsed from an <tt>InputStream</tt>
getConfigurationFileURL
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public Collection<String> getSectionNames (Collection<String> collection) { for (Section section : sectionsInOrder) collection.add (section.getName()); return collection; }
Get the names of the sections in this object, in the order they were parsed and/or added. @param collection the <tt>Collection</tt> to which to add the section names. The names are added in the order they were parsed and/or added to this object; of course, the <tt>Collection</tt> may reorder them. @return the <tt>collection</tt> parameter, for convenience @see #getVariableNames
getSectionNames
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public Collection<String> getSectionNames() { return getSectionNames (new ArrayList<String>()); }
Get the names of the sections in this object, in the order they were parsed and/or added. @return a new <tt>Collection</tt> of section names @see #getVariableNames
getSectionNames
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public Collection<String> getVariableNames (String sectionName, Collection<String> collection) throws NoSuchSectionException { Section section = sectionsByName.get (sectionName); if (section == null) throw new NoSuchSectionException (sectionName); collection.addAll (section.getVariableNames()); return collection; }
Get the names of the all the variables in a section, in the order they were parsed and/or added. @param sectionName the name of the section to access @param collection the <tt>Collection</tt> to which to add the variable names. The names are added in the order they were parsed and/or added to this object; of course, the <tt>Collection</tt> may reorder them. @return the <tt>collection</tt> parameter, for convenience @throws NoSuchSectionException no such section @see #getSectionNames @see #containsSection @see #getVariableValue
getVariableNames
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public Collection<String> getVariableNames (String sectionName) throws NoSuchSectionException { return getVariableNames (sectionName, new ArrayList<String>()); }
Get the names of the all the variables in a section, in the order they were parsed and/or added. @param sectionName the name of the section to access @return a new <tt>Collection</tt> of variable names @throws NoSuchSectionException no such section @see #getSectionNames @see #containsSection @see #getVariableValue
getVariableNames
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public String getConfigurationValue (String sectionName, String variableName) throws NoSuchSectionException, NoSuchVariableException { Section section = sectionsByName.get (sectionName); if (section == null) throw new NoSuchSectionException (sectionName); Variable variable = null; try { variable = section.getVariable (variableName); } catch (ConfigurationException ex) { } if (variable == null) throw new NoSuchVariableException (sectionName, variableName); return variable.getCookedValue(); }
Get the value for a variable. This method returns a "collapsed" value, with any quotes stripped. It's impossible to tell where quoted substrings appeared. If you need to know where individual tokens begin and end, use the {@link #getConfigurationTokens} method. For example, if the configuration line looks like this: <blockquote><pre>foo: abc "def ghi" jkl</pre></blockquote> this method will return the string <blockquote><pre>abc def ghi jkl</pre></blockquote> whereas {@link #getConfigurationTokens} will return the following individual tokens: <blockquote><pre> abc def ghi jkl </pre></blockquote> Getting the tokens preserves the white space-escaping properties of the double quotes. @param sectionName the name of the section containing the variable @param variableName the variable name @return the value for the variable (which may be the empty string) @throws NoSuchSectionException the named section does not exist @throws NoSuchVariableException the section has no such variable @see #getConfigurationTokens
getConfigurationValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public String getRawValue (String sectionName, String variableName) throws NoSuchSectionException, NoSuchVariableException { Section section = sectionsByName.get (sectionName); if (section == null) throw new NoSuchSectionException (sectionName); Variable variable = null; try { variable = section.getVariable (variableName); } catch (ConfigurationException ex) { } if (variable == null) throw new NoSuchVariableException (sectionName, variableName); return variable.getRawValue(); }
Get the raw value (i.e., without any substitutions) for a variable. @param sectionName the name of the section containing the variable @param variableName the variable name @return the value for the variable (which may be the empty string) @throws NoSuchSectionException the named section does not exist @throws NoSuchVariableException the section has no such variable @see #getConfigurationValue
getRawValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public String[] getConfigurationTokens (String sectionName, String variableName) throws ConfigurationException { Section section = sectionsByName.get (sectionName); if (section == null) throw new NoSuchSectionException (sectionName); Variable variable = null; try { variable = section.getVariable (variableName); } catch (ConfigurationException ex) { log.error ("Can't get value for variable \"" + variableName + "\" in section \"" + sectionName + "\"", ex); } if (variable == null) throw new NoSuchVariableException (sectionName, variableName); // The "cooked" tokens are split into literal (i.e., quoted) and // non-quoted pieces. This is almost never what a caller wants. // Instead, the caller wants individual tokens, with quoted parts // represented as a single token. For instance, given: // // abc def ghi // // the caller wants: // // abc // def // ghi // // Similarly, given: // // abc "def ghi" jkl // // the caller wants: // // abc // def ghi // jkl // // Fortunately, the parser keeps track of whether a segment was // literal or not, so this is easy to manufacture. ValueSegment[] cookedSegments = variable.getCookedSegments(); ArrayList<String> result = new ArrayList<String>(); if (cookedSegments != null) { for (ValueSegment segment : cookedSegments) { String cookedToken = segment.toString(); if (segment.isLiteral || segment.isWhiteSpaceEscaped) { // Was quoted. Use it as is. result.add(cookedToken); } else { // Break it into white space-delimited tokens. String[] tokens = TextUtil.split(cookedToken); for (String token : tokens) result.add(token); } } } return result.toArray(new String[result.size()]); }
Get the value for a variable as a series of tokens. This is the method to use if you need to retain any white space between quotes. With the {@link #getConfigurationValue} method, it's impossible to tell where quoted substrings appeared. For example, if the configuration line looks like this: <blockquote><pre>foo: abc "def ghi" jkl mno</pre></blockquote> {@link #getConfigurationValue} will return the string <blockquote><pre>abc def ghi jkl mno</pre></blockquote> whereas this method will return the following individual tokens: <blockquote><pre> abc def ghi jkl mno </pre></blockquote> <p>Getting the tokens preserves the white space-escaping properties of the double quotes.</p> @param sectionName the name of the section containing the variable @param variableName the variable name @return the value tokens for the variable, or null if the value exists but was empty @throws ConfigurationException some configuration error @see #getConfigurationValue @since Version 2.1.3
getConfigurationTokens
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public int getOptionalIntegerValue (String sectionName, String variableName, int defaultValue) throws ConfigurationException { int result = defaultValue; try { result = getRequiredIntegerValue(sectionName, variableName); } catch (NoSuchVariableException ex) { // Use default } return result; }
Convenience method to get and convert an optional integer parameter. The default value applies if the variable is missing or is there but has an empty value. @param sectionName section name @param variableName variable name @param defaultValue default value if not found @return the value, or the default value if not found @throws ConfigurationException some configuration error @see #getOptionalCardinalValue @see #getRequiredIntegerValue
getOptionalIntegerValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public int getRequiredIntegerValue (String sectionName, String variableName) throws ConfigurationException { String sNum = getConfigurationValue (sectionName, variableName); try { return Integer.parseInt (sNum); } catch (NumberFormatException ex) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.badNumericValue", "Bad numeric value \"{0}\" for variable \"{1}\" in section " + "\"{2}\"", new Object[] { sNum, variableName, sectionName }); } }
Convenience method to get and convert a required integer parameter. @param sectionName section name @param variableName variable name @return the value @throws ConfigurationException some configuration error, including bad numeric value @see #getRequiredCardinalValue @see #getOptionalIntegerValue
getRequiredIntegerValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public int getOptionalCardinalValue (String sectionName, String variableName, int defaultValue) throws ConfigurationException { assert (defaultValue >= 0); int result = defaultValue; try { result = getRequiredCardinalValue(sectionName, variableName); } catch (NoSuchVariableException ex) { // Accept default } return result; }
Convenience method to get and convert an optional non-negative integer parameter. The default value applies if the variable is missing or is there but has an empty value. (The term "cardinal" is borrowed from the Modula-3 language.) @param sectionName section name @param variableName variable name @param defaultValue default value if not found. Must be non-negative. @return the value, or the default value if not found @throws ConfigurationException bad numeric value, or other config error @see #getOptionalIntegerValue @see #getRequiredCardinalValue
getOptionalCardinalValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public int getRequiredCardinalValue (String sectionName, String variableName) throws ConfigurationException { String sNum = getConfigurationValue (sectionName, variableName); int i = getRequiredIntegerValue (sectionName, variableName); if (i < 0) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.negativeCardinalValue", "Bad negative numeric value \"{0}\" " + "for variable \"{1}\" in section \"{2}\"", new Object[] { sNum, variableName, sectionName }); } return i; }
Convenience method to get and convert a required integer parameter. @param sectionName section name @param variableName variable name @return the value @throws ConfigurationException bad numeric value, or other config error @see #getOptionalCardinalValue @see #getRequiredIntegerValue
getRequiredCardinalValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public double getOptionalDoubleValue (String sectionName, String variableName, double defaultValue) throws ConfigurationException { double result = defaultValue; try { result = getRequiredDoubleValue(sectionName, variableName); } catch (NoSuchVariableException ex) { // Accept default } return result; }
Convenience method to get and convert an optional floating point numeric parameter. The default value applies if the variable is missing or is there but has an empty value. @param sectionName section name @param variableName variable name @param defaultValue default value if not found @return the value, or the default value if not found @throws ConfigurationException bad numeric value or other config error
getOptionalDoubleValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public double getRequiredDoubleValue (String sectionName, String variableName) throws ConfigurationException { String sNum = getConfigurationValue (sectionName, variableName); try { return Double.parseDouble (sNum); } catch (NumberFormatException ex) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.badFloatValue", "Bad floating point value " + "\"{0}\" for variable \"{1}\" " + "in section \"{2}\"", new Object[] { sNum, variableName, sectionName }); } }
Convenience method to get and convert a required floating point numeric parameter. @param sectionName section name @param variableName variable name @return the value @throws ConfigurationException bad numeric value or other config error
getRequiredDoubleValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public boolean getOptionalBooleanValue (String sectionName, String variableName, boolean defaultValue) throws ConfigurationException { boolean result; try { String s = getConfigurationValue (sectionName, variableName); if (TextUtil.stringIsEmpty (s)) result = defaultValue; else result = TextUtil.booleanFromString (s); } catch (NoSuchVariableException ex) { result = defaultValue; } catch (IllegalArgumentException ex) { throw new ConfigurationException (ex.getMessage()); } return result; }
Convenience method to get and convert an optional boolean parameter. The default value applies if the variable is missing or is there but has an empty value. @param sectionName section name @param variableName variable name @param defaultValue default value if not found @return the value, or the default value if not found @throws ConfigurationException bad numeric value, or other config error
getOptionalBooleanValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public boolean getRequiredBooleanValue (String sectionName, String variableName) throws ConfigurationException { String val = getConfigurationValue(sectionName, variableName); try { return TextUtil.booleanFromString(val); } catch (IllegalArgumentException ex) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.badBooleanValue", "Bad boolean value \"{0}\" for variable \"{1}\" in " + "section \"{2}\".\n", new Object[] {val, variableName, sectionName}); } }
Convenience method to get and convert a required boolean parameter. @param sectionName section name @param variableName variable name @return the value @throws ConfigurationException bad numeric value, or other config error
getRequiredBooleanValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public String getOptionalStringValue (String sectionName, String variableName, String defaultValue) throws ConfigurationException { String result; try { result = getConfigurationValue (sectionName, variableName); if (TextUtil.stringIsEmpty (result)) result = defaultValue; } catch (NoSuchVariableException ex) { result = defaultValue; } return result; }
Convenience method to get an optional string value. The default value applies if the variable is missing or is there but has an empty value. @param sectionName section name @param variableName variable name @param defaultValue default value if not found @return the value, or the default value if not found @throws ConfigurationException bad numeric value or other config error
getOptionalStringValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public String getVariableValue (String varName, Object context) throws VariableSubstitutionException { int i; Section section; String sectionName; String value = null; SubstitutionContext substContext = (SubstitutionContext) context; Section variableParentSection; Variable currentVariable; try { checkVariableName (varName); } catch (ConfigurationException ex) { throw new VariableSubstitutionException (ex); } currentVariable = substContext.currentVariable; if (currentVariable.getName().equals (varName)) { throw new VariableSubstitutionException (Package.BUNDLE_NAME, "Configuration.recursiveSubst", "Attempt to substitute value for variable " + "\"{0}\" within itself.", new Object[] {varName}); } variableParentSection = substContext.currentVariable.getSection(); i = varName.indexOf (':'); if (i == -1) { // No section in the variable reference. Use the variable's // context. section = variableParentSection; sectionName = section.getName(); } else { sectionName = varName.substring (0, i); varName = varName.substring (i + 1); if (sectionName.equals (SYSTEM_SECTION_NAME)) section = systemSection; else if (sectionName.equals (PROGRAM_SECTION_NAME)) section = programSection; else if (sectionName.equals (ENV_SECTION_NAME)) section = envSection; else section = sectionsByName.get (sectionName); } if (section == null) { if (abortOnUndefinedVariable) { throw new VariableSubstitutionException (Package.BUNDLE_NAME, "Configuration.nonExistentSection", "Reference to variable \"{0}\" in nonexistent section " + "\"{1}\".", new Object[] {varName, sectionName}); } } else { if (variableParentSection.getID() < section.getID()) { String parentSectionName = variableParentSection.getName(); String thisSectionName = section.getName(); throw new VariableSubstitutionException (Package.BUNDLE_NAME, "Configuration.badSectionRef", "Variable \"{0}\" in section \"{1}\" cannot substitute " + "the value of variable \"{2}\" from section \"{3}\", " + "because section \"{3}\" appears after section \"{1}\" " + "in the configuration file.", new Object[] { substContext.currentVariable.getName(), parentSectionName, varName, thisSectionName, thisSectionName, parentSectionName }); } Variable varToSubst; try { varToSubst = section.getVariable (varName); } catch (ConfigurationException ex) { throw new VariableSubstitutionException (ex.getMessage()); } if (varToSubst != null) { value = varToSubst.getCookedValue(); } } substContext.totalSubstitutions++; return value; }
Get the value associated with a given variable. Required by the {@link VariableDereferencer} interface, this method is used during parsing to handle variable substitutions (but also potentially useful by other applications). See this class's documentation for details on variable references. @param varName The name of the variable for which the value is desired. @param context a context object, passed through from the caller to the dereferencer, or null if there isn't one. For this class, the context object is a SubstitutionContext variable. @return The variable's value. If the variable has no value, this method must return null. @throws VariableSubstitutionException variable references itself
getVariableValue
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public boolean legalVariableCharacter (char c) { return ! (UnixShellVariableSubstituter.isVariableMetacharacter (c)); }
Required by the {@link VariableNameChecker} interface, this method determines whether a character may legally be used in a variable name or not. @param c The character to test @return <tt>true</tt> if the character may be part of a variable name, <tt>false</tt> otherwise @see VariableSubstituter#substitute
legalVariableCharacter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(File file) throws IOException, ConfigurationException { load(file, null); }
Load configuration from a <tt>File</tt>. Any existing data is discarded. @param file the file @throws IOException read error @throws ConfigurationException parse error
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(File file, String encoding) throws IOException, ConfigurationException { clear(); URL url = file.toURI().toURL(); parse (new FileInputStream(file), encoding, url); this.configURL = url; }
Load configuration from a <tt>File</tt>. Any existing data is discarded. @param file the file @param encoding the encoding to use, or null for the default @throws IOException read error @throws ConfigurationException parse error
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(String path) throws IOException, ConfigurationException { load(path, null); }
Load configuration from a file specified as a pathname. Any existing data is discarded. @param path the path @throws FileNotFoundException specified file doesn't exist @throws IOException can't open or read file @throws ConfigurationException error in configuration data
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(String path, String encoding) throws IOException, ConfigurationException { clear(); URL url = new File(path).toURI().toURL(); parse(new FileInputStream(path), encoding, url); this.configURL = url; }
Load configuration from a file specified as a pathname. Any existing data is discarded. @param path the path @param encoding the encoding to use, or null for the default @throws FileNotFoundException specified file doesn't exist @throws IOException can't open or read file @throws ConfigurationException error in configuration data @throws UnsupportedEncodingException bad encoding
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(URL url) throws IOException, ConfigurationException { load(url, null); }
Load the configuration from a URL. @param url the URL @throws IOException on I/O error @throws ConfigurationException on configuration error
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(URL url, String encoding) throws IOException, ConfigurationException { clear(); parse(url.openStream(), encoding, url); this.configURL = url; }
Load the configuration from a URL. @param url the URL @param encoding the encoding, if known, or null @throws IOException on I/O error @throws ConfigurationException on configuration error
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(InputStream iStream) throws IOException, ConfigurationException { load(iStream, null); }
Load configuration from an <tt>InputStream</tt>. Any existing data is discarded. @param iStream the <tt>InputStream</tt> @throws IOException can't open or read URL @throws ConfigurationException error in configuration data
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void load(InputStream iStream, String encoding) throws IOException, ConfigurationException { clear(); parse(iStream, encoding, null); }
Load configuration from an <tt>InputStream</tt>. Any existing data is discarded. @param iStream the <tt>InputStream</tt> @param encoding the encoding to use, or null for the default @throws IOException can't open or read URL @throws ConfigurationException error in configuration data @throws UnsupportedEncodingException bad encoding
load
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void setVariable (String sectionName, String variableName, String value, boolean expand) throws NoSuchSectionException, VariableSubstitutionException { Section section = sectionsByName.get (sectionName); if (section == null) throw new NoSuchSectionException (sectionName); Variable variable; try { variable = section.getVariable (variableName); if (variable != null) variable.setValue(value); else variable = section.addVariable(variableName, value); } catch (ConfigurationException ex) { throw new VariableSubstitutionException (ex.getMessage()); } if (expand) { try { substituteVariables(variable, varSubstituter, true); } catch (ConfigurationException ex) { throw new VariableSubstitutionException (ex.getMessage()); } } }
Set a variable's value. If the variable does not exist, it is created. If it does exist, its current value is overwritten with the new one. Metacharacters and variable references are not expanded unless the <tt>expand</tt> parameter is <tt>true</tt>. An <tt>expand</tt> value of <tt>false</tt> is useful when creating new configuration data to be written later. @param sectionName name of existing section to contain the variable @param variableName name of variable to set @param value variable's value @param expand <tt>true</tt> to expand metacharacters and variable references in the value, <tt>false</tt> to leave the value untouched. @throws NoSuchSectionException section does not exist @throws VariableSubstitutionException variable substitution error
setVariable
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public boolean getAbortOnUndefinedVariable() { return abortOnUndefinedVariable; }
Get the value of the flag that controls whether the <tt>Configuration</tt> object will abort when it encounters an undefined variable. If this flag is clear, then an undefined variable is expanded to an empty string. If this flag is set, then an undefined value results in a {@link ConfigurationException}. @return <tt>true</tt> if the "abort on undefined variable" capability is enabled, <tt>false</tt> if it is disabled. @see #setAbortOnUndefinedVariable
getAbortOnUndefinedVariable
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void setAbortOnUndefinedVariable(boolean enable) { abortOnUndefinedVariable = enable; varSubstituter.setAbortOnUndefinedVariable(enable); }
Set or clear the flag that controls whether the <tt>Configuration</tt> object will abort when it encounters an undefined variable. If this flag is clear, then an undefined variable is expanded to an empty string. If this flag is set, then an undefined value results in a {@link ConfigurationException}. The flag defaults to <tt>true</tt>. @param enable <tt>true</tt> to enable the "abort on undefined variable" flag, <tt>false</tt> to disable it. @see #getAbortOnUndefinedVariable
setAbortOnUndefinedVariable
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void write (PrintWriter out) throws ConfigurationException { XStringBuffer value = new XStringBuffer(); boolean firstSection = true; out.print (COMMENT_CHARS.charAt (0)); out.print (" Written by "); out.println (this.getClass().getName()); out.print (COMMENT_CHARS.charAt (0)); out.print (" on "); out.println (new Date().toString()); out.println(); for (Section section : sectionsInOrder) { if (! firstSection) out.println(); out.println (SECTION_START + section.getName() + SECTION_END); firstSection = false; for (String varName : section.getVariableNames()) { Variable var = section.getVariable (varName); value.setLength (0); value.append (var.getRawValue()); //value.encodeMetacharacters(); out.println (varName + ": " + value.toString()); } } }
Writes the configuration data to a <tt>PrintWriter</tt>. The sections and variables within the sections are written in the order they were originally read from the file. Non-printable characters (and a few others) are encoded into metacharacter sequences. Comments are not propagated, since they are not retained when the data is parsed. @param out where to write the configuration data @throws ConfigurationException on error @see XStringBuffer#encodeMetacharacters()
write
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public void write (PrintStream out) throws ConfigurationException { PrintWriter w = new PrintWriter (out); write (w); w.flush(); }
Writes the configuration data to a <tt>PrintStream</tt>. The sections and variables within the sections are written in the order they were originally read from the file. Non-printable characters (and a few others) are encoded into metacharacter sequences. Comments and variable references are not propagated, since they are not retained when the data is parsed. @param out where to write the configuration data @throws ConfigurationException on error @see XStringBuffer#encodeMetacharacters()
write
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private synchronized void parse (InputStream in, String encoding, URL url) throws ConfigurationException, UnsupportedEncodingException { loadConfiguration (in, encoding, url, new ParseContext()); }
Parse configuration data from the specified stream. @param in the input stream @param encoding the encoding to use, or null for the default @param url the URL associated with the stream, or null if not known @throws ConfigurationException parse error @throws UnsupportedEncodingException bad encoding
parse
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private void loadConfiguration(InputStream in, String encoding, URL url, ParseContext parseContext) throws ConfigurationException, UnsupportedEncodingException { BufferedReader r; Line line = new Line(); String sURL = url.toExternalForm(); // Now, create the phantom program, env and system sections. These // MUST be created first, or other sections won't be able to // substitute from them. (i.e., They must have the lowest IDs.) programSection = new ProgramSection (PROGRAM_SECTION_NAME, PROGRAM_SECTION_ID); systemSection = new SystemSection (SYSTEM_SECTION_NAME, SYSTEM_SECTION_ID); envSection = new EnvSection (ENV_SECTION_NAME, ENV_SECTION_ID); if (parseContext.openURLs.contains (sURL)) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.recursiveInclude", "{0}, line {1}: Attempt to include \"{2}\" " + "from itself, either directly or indirectly.", new Object[] { url.toExternalForm(), String.valueOf (line.number), sURL }); } parseContext.openURLs.add (sURL); // Parse the entire file into memory before doing variable // substitution and metacharacter expansion. InputStreamReader ir; if (encoding == null) ir = new InputStreamReader(in); else ir = new InputStreamReader(in, encoding); r = new BufferedReader(ir); while (readLogicalLine (r, line)) { try { switch (line.type) { case COMMENT: case BLANK: break; case INCLUDE: handleInclude (line, url, encoding, parseContext); break; case SECTION: parseContext.currentSection = handleNewSection (line, url); break; case VARIABLE: if (parseContext.currentSection == null) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.varBeforeSection", "{0}, line {1}: Variable assignment " + "before first section.", new Object[] { url.toExternalForm(), String.valueOf (line.number) }); } handleVariable (line, url, parseContext); break; default: throw new IllegalStateException ("Bug: line.type=" + line.type); } } catch (IOException ex) { throw new ConfigurationException (getExceptionPrefix (line, url) + ex.toString()); } } parseContext.openURLs.remove (sURL); }
Load the configuration data into memory, without processing metacharacters or variable substitution. Includes are processed, though. @param in input stream @param encoding the encoding to use, or null for the default @param url URL associated with the stream, or null if not known @param parseContext current parsing context @throws ConfigurationException parse error @throws UnsupportedEncodingException bad encoding
loadConfiguration
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private Section handleNewSection (Line line, URL url) throws ConfigurationException { String s = line.buffer.toString().trim(); if (s.charAt (0) != SECTION_START) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.badSectionBegin", "{0}, line {1}: Section does not begin with \"{2}\"", new Object[] { url.toExternalForm(), String.valueOf (line.number), String.valueOf (SECTION_START) }); } else if (s.charAt (s.length() - 1) != SECTION_END) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.badSectionEnd", "{0}, line {1}: Section does not end with \"{2}\"", new Object[] { url.toExternalForm(), String.valueOf (line.number), String.valueOf (SECTION_END) }); } return makeNewSection (s.substring (1, s.length() - 1)); }
Handle a new section. @param line line buffer @param url URL currently being processed, or null if unknown @return a new Section object, which has been stored in the appropriate places @throws ConfigurationException configuration error
handleNewSection
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private void handleVariable (Line line, URL url, ParseContext parseContext) throws ConfigurationException { char[] s = line.buffer.toString().toCharArray(); int iSep; for (iSep = 0; iSep < s.length; iSep++) { if ((s[iSep] == ':') || (s[iSep] == '=')) break; } if (iSep == s.length) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.missingAssignOp", "{0}, line {1}: Missing \"=\" " + "or \":\" for variable " + "definition.", new Object[] { url.toExternalForm(), String.valueOf (line.number) }); } if (iSep == 0) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.noVariablName", "{0}, line {1}: Missing " + "variable name for variable " + "definition.", new Object[] { url.toExternalForm(), String.valueOf (line.number) }); } int i = 0; int j = iSep - 1; while (Character.isWhitespace (s[i])) i++; while (Character.isWhitespace (s[j])) j--; if (i > j) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.noVariablName", "{0}, line {1}: Missing " + "variable name for variable " + "definition.", new Object[] { url.toExternalForm(), String.valueOf (line.number) }); } String varName = new String (s, i, j - i + 1); if (varName.length() == 0) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.noVariablName", "{0}, line {1}: Missing " + "variable name for variable " + "definition.", new Object[] { url.toExternalForm(), String.valueOf (line.number) }); } checkVariableName (varName); i = skipWhitespace (s, iSep + 1); j = s.length - i; Section currentSection = parseContext.currentSection; String value = new String (s, i, j); Variable existing = currentSection.getVariable (varName); if (existing != null) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.duplicateVar", "{0}, line {1}: Section \"{2}\" has a " + "duplicate definition for variable " + "\"{3}\". The first instance was defined " + "on line {4}.", new Object[] { url.toExternalForm(), String.valueOf (line.number), currentSection.getName(), varName, String.valueOf (existing.getLineWhereDefined()) }); } Variable newVar = currentSection.addVariable (varName, value, line.number); // Expand the metacharacters and variable references in the variable. try { newVar.segmentValue(); decodeMetacharacters (newVar); substituteVariables (newVar, varSubstituter, false); newVar.reassembleCookedValueFromSegments(); } catch (VariableSubstitutionException ex) { throw new ConfigurationException (ex.getMessage()); } }
Handle a new variable during parsing. @param line line buffer @param url URL currently being processed, or null if unknown @param parseContext current parsing context @throws ConfigurationException configuration error
handleVariable
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private void checkVariableName (String varName) throws ConfigurationException { for (char c : varName.toCharArray()) { if (! legalVariableCharacter (c)) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.badVariableName", "\"{0}\" is an illegal variable name", new Object[] {varName}); } } }
Determine whether a variable name is legal, throwing an exception if it isn't. @param varName the name to check @throws ConfigurationException bad variable name
checkVariableName
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private void handleInclude (Line line, URL url, String encoding, ParseContext parseContext) throws IOException, ConfigurationException { if (parseContext.includeFileNestingLevel >= MAX_INCLUDE_NESTING_LEVEL) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.maxNestedIncludeExceeded", "{0}, line {1}: Exceeded maximum nested " + "include level of {2}.", new Object[] { url.toExternalForm(), String.valueOf (line.number), String.valueOf (MAX_INCLUDE_NESTING_LEVEL) }); } parseContext.includeFileNestingLevel++; String s = line.buffer.toString(); // Parse the file name. String includeTarget = s.substring (INCLUDE.length() + 1).trim(); int len = includeTarget.length(); // Make sure double quotes surround the file or URL. if ((len < 2) || (! includeTarget.startsWith ("\"")) || (! includeTarget.endsWith ("\""))) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.malformedDirective", "{0}, line {1}: Malformed \"{2}\" directive", new Object[] { url.toExternalForm(), String.valueOf (line.number), INCLUDE }); } // Extract the file. includeTarget = includeTarget.substring (1, len - 1); if (includeTarget.length() == 0) { throw new ConfigurationException (Package.BUNDLE_NAME, "Configuration.includeMissingFile", "{0}, line {1}: Missing file name or URL " + "in \"{2}\" directive", new Object[] { url.toExternalForm(), String.valueOf (line.number), INCLUDE }); } // Process the include try { loadInclude (new URL (includeTarget), encoding, parseContext); } catch (MalformedURLException ex) { // Not obviously a URL. First, determine whether it has // directory information or not. If not, try to use the // parent's directory information. if (FileUtil.isAbsolutePath (includeTarget)) { loadInclude (new URL (url.getProtocol(), url.getHost(), url.getPort(), includeTarget), encoding, parseContext); } else { // It's relative to the parent. If the parent URL is not // specified, then we can't do anything except try to load // the include as is. It'll probably fail... if (url == null) { loadInclude (new File (includeTarget).toURI().toURL(), encoding, parseContext); } else { String parent = new File (url.getFile()).getParent(); if (parent == null) parent = ""; loadInclude (new URL (url.getProtocol(), url.getHost(), url.getPort(), parent + "/" + includeTarget), encoding, parseContext); } } } parseContext.includeFileNestingLevel--; }
Handle an include directive. @param line line buffer @param encoding the encoding to use, or null for the default @param url URL currently being processed, or null if unknown @param parseContext current parsing context @throws IOException I/O error opening or reading include @throws ConfigurationException configuration error
handleInclude
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private void loadInclude (URL url, String encoding, ParseContext parseContext) throws IOException, ConfigurationException { loadConfiguration (url.openStream(), encoding, url, parseContext); }
Actually attempts to load an include reference. This is basically just a simplified front-end to loadConfiguration(). @param url the URL to be included @param encoding the encoding to use, or null for the default @param parseContext current parsing context @throws IOException I/O error @throws ConfigurationException configuration error
loadInclude
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private boolean readLogicalLine (BufferedReader r, Line line) throws ConfigurationException { boolean continued = false; boolean gotSomething = false; line.newLine(); for (;;) { String s; try { s = r.readLine(); } catch (IOException ex) { throw new ConfigurationException (ex.toString()); } if (s == null) break; gotSomething = true; line.number++; // Strip leading white space on all lines. int i; char[] chars = s.toCharArray(); i = skipWhitespace (chars, 0); if (i < chars.length) s = s.substring (i); else s = ""; if (! continued) { // First line. Determine what it is. if (s.length() == 0) line.type = LineType.BLANK; else if (COMMENT_CHARS.indexOf (s.charAt (0)) != -1) line.type = LineType.COMMENT; else if (s.charAt (0) == SECTION_START) line.type = LineType.SECTION; else if (new StringTokenizer (s).nextToken().equals (INCLUDE)) line.type = LineType.INCLUDE; else line.type = LineType.VARIABLE; } if ((line.type == LineType.VARIABLE) && (hasContinuationMark (s))) { continued = true; line.buffer.append (s.substring (0, s.length() - 1)); } else { line.buffer.append (s); break; } } return gotSomething; }
Read the next logical line of input from a config file. @param r the reader @param line where to store the line. The line number in this object is incremented, the "buffer" field is updated, and the "type" field is set appropriately. @return <tt>true</tt> if a line was read, <tt>false</tt> for EOF. @throws ConfigurationException read error
readLogicalLine
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private boolean hasContinuationMark (String s) { boolean has = false; if (s.length() > 0) { char[] chars = s.toCharArray(); if (chars[chars.length-1] == '\\') { // Possibly. See if there are an odd number of them. int total = 0; for (int i = chars.length - 1; i >= 0; i--) { if (chars[i] != '\\') break; total++; } has = ((total % 2) == 1); } } return has; }
Determine whether a line has a continuation mark or not. @param s the line @return true if there's a continuation mark, false if not
hasContinuationMark
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private String getExceptionPrefix (Line line, URL url) { StringBuffer buf = new StringBuffer(); if (url != null) { buf.append (url.toExternalForm()); buf.append (", line "); } else { buf.append ("Line "); } buf.append (line.number); buf.append (": "); return buf.toString(); }
Get an appropriate exception prefix (e.g., line number, etc.) @param line line buffer @param url URL currently being processed, or null if unknown @return a suitable string
getExceptionPrefix
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private void decodeMetacharacters (Variable var) throws VariableSubstitutionException, ConfigurationException { ValueSegment[] segments = var.getCookedSegments(); for (ValueSegment segment : segments) { if (segment.isLiteral) continue; segment.segmentBuf.decodeMetacharacters(); } }
Handle metacharacter substitution for a variable value. @param var The current variable being processed @throws VariableSubstitutionException variable substitution error @throws ConfigurationException some other configuration error
decodeMetacharacters
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private void substituteVariables (Variable var, VariableSubstituter substituter, boolean concatSegments) throws VariableSubstitutionException, ConfigurationException { ValueSegment[] segments = var.getCookedSegments(); SubstitutionContext context = new SubstitutionContext (var); for (ValueSegment segment : segments) { // Keep substituting the current variable's value until there // no more substitutions are performed. This handles the case // where a dereferenced variable value contains its own // variable references. if (segment.isLiteral) continue; String s = segment.segmentBuf.toString(); do { context.totalSubstitutions = 0; s = substituter.substitute (s, this, this, context); } while (context.totalSubstitutions > 0); segment.segmentBuf.setLength (0); segment.segmentBuf.append (s); } if (concatSegments) var.reassembleCookedValueFromSegments(); }
Handle variable substitution for a variable value. @param var The current variable being processed @param substituter VariableSubstituter to use @param concatSegments Re-concatenate the segments @throws VariableSubstitutionException variable substitution error @throws ConfigurationException some other configuration error
substituteVariables
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private int skipWhitespace (char[] chars, int start) { while (start < chars.length) { if (! Character.isWhitespace (chars[start])) break; start++; } return start; }
Get index of first non-whitespace character. @param chars character array to check @param start starting point @return index of first non-whitespace character past "start", or -1
skipWhitespace
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private Section makeNewSection (String sectionName) { int id = nextSectionID(); Section section = new Section (sectionName, id); sectionsInOrder.add (section); sectionsByName.put (sectionName, section); return section; }
Create and save a new Section. @param sectionName the name @return the Section object, which has been saved.
makeNewSection
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
private synchronized int nextSectionID() { return ++nextSectionIDValue; }
Get the next section ID @return the ID
nextSectionID
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/Configuration.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/Configuration.java
MIT
public String getVariableName() { return variableName; }
Gets the variable name associated with this exception. @return the variable name
getVariableName
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/config/NoSuchVariableException.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/config/NoSuchVariableException.java
MIT