com.izforge.izpack.util
Class SpecHelper

java.lang.Object
  extended by com.izforge.izpack.util.SpecHelper

public class SpecHelper
extends java.lang.Object

This class contains some helper methods to simplify handling of xml specification files.

Author:
Klaus Bartz

Field Summary
private  boolean _haveSpec
           
static java.lang.String NO
           
private static java.lang.String PACK_KEY
           
private static java.lang.String PACK_NAME
           
private  XMLElement spec
           
private  java.lang.String specFilename
           
static java.lang.String YES
           
 
Constructor Summary
SpecHelper()
          The default constructor.
 
Method Summary
 java.util.Vector<XMLElement> getAllSubChildren(XMLElement root, java.lang.String[] childdef)
          Returns a Vector with all leafs of the tree which is described with childdef.
 XMLElement getPackForName(java.lang.String packDestName)
          Returns a XML element which represents the pack for the given name.
 java.lang.String getRequiredAttribute(XMLElement element, java.lang.String attrName)
          Returns the attribute for the given attribute name.
 java.io.InputStream getResource(java.lang.String res)
          Gets the stream to a resource.
 XMLElement getSpec()
          Returns the specification.
private  java.util.Vector<XMLElement> getSubChildren(XMLElement root, java.lang.String[] childdef, int depth)
          Returns a Vector with all leafs of the tree which is described with childdef beginning at the given depth.
 boolean haveSpec()
          Returns true if a specification exist, else false.
 boolean isAttributeYes(XMLElement element, java.lang.String attribute, boolean defaultValue)
          Returns whether the value to the given attribute is "yes" or not.
 void parseError(XMLElement parent, java.lang.String message)
          Create parse error with consistent messages.
 void readSpec(java.io.InputStream input)
          Reads the XML specification given by the input stream.
 void readSpec(java.io.InputStream input, VariableSubstitutor substitutor)
          Reads the XML specification given by the input stream.
 void readSpec(java.lang.String specFileName)
          Reads the XML specification given by the file name.
 void readSpec(java.lang.String specFileName, VariableSubstitutor substitutor)
          Reads the XML specification given by the file name.
 void setSpec(XMLElement element)
          Sets the specifaction to the given XML element.
 java.io.InputStream substituteVariables(java.io.InputStream input, VariableSubstitutor substitutor)
          Creates an temp file in to the substitutor the substituted contents of input writes; close it and (re)open it as FileInputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

specFilename

private java.lang.String specFilename

spec

private XMLElement spec

_haveSpec

private boolean _haveSpec

YES

public static final java.lang.String YES
See Also:
Constant Field Values

NO

public static final java.lang.String NO
See Also:
Constant Field Values

PACK_KEY

private static final java.lang.String PACK_KEY
See Also:
Constant Field Values

PACK_NAME

private static final java.lang.String PACK_NAME
See Also:
Constant Field Values
Constructor Detail

SpecHelper

public SpecHelper()
The default constructor.

Method Detail

readSpec

public void readSpec(java.lang.String specFileName)
              throws java.lang.Exception
Reads the XML specification given by the file name. The result is stored in spec.

Throws:
java.lang.Exception - for any problems in reading the specification

readSpec

public void readSpec(java.lang.String specFileName,
                     VariableSubstitutor substitutor)
              throws java.lang.Exception
Reads the XML specification given by the file name. The result is stored in spec.

Throws:
java.lang.Exception - for any problems in reading the specification

readSpec

public void readSpec(java.io.InputStream input)
              throws java.lang.Exception
Reads the XML specification given by the input stream. The result is stored in spec.

Throws:
java.lang.Exception - for any problems in reading the specification

readSpec

public void readSpec(java.io.InputStream input,
                     VariableSubstitutor substitutor)
              throws java.lang.Exception
Reads the XML specification given by the input stream. The result is stored in spec.

Throws:
java.lang.Exception - for any problems in reading the specification

getResource

public java.io.InputStream getResource(java.lang.String res)
Gets the stream to a resource.

Parameters:
res - The resource id.
Returns:
The resource value, null if not found

getPackForName

public XMLElement getPackForName(java.lang.String packDestName)
Returns a XML element which represents the pack for the given name.

Parameters:
packDestName - name of the pack which should be returned
Returns:
a XML element which represents the pack for the given name

parseError

public void parseError(XMLElement parent,
                       java.lang.String message)
                throws InstallerException
Create parse error with consistent messages. Includes file name and line # of parent. It is an error for 'parent' to be null.

Parameters:
parent - The element in which the error occured
message - Brief message explaining error
Throws:
InstallerException

haveSpec

public boolean haveSpec()
Returns true if a specification exist, else false.

Returns:
true if a specification exist, else false

getSpec

public XMLElement getSpec()
Returns the specification.

Returns:
the specification

setSpec

public void setSpec(XMLElement element)
Sets the specifaction to the given XML element.

Parameters:
element -

getAllSubChildren

public java.util.Vector<XMLElement> getAllSubChildren(XMLElement root,
                                                      java.lang.String[] childdef)
Returns a Vector with all leafs of the tree which is described with childdef.

Parameters:
root - the XMLElement which is the current root for the search
childdef - a String array which describes the tree; the last element contains the leaf name
Returns:
a Vector of XMLElements of all leafs founded under root

getSubChildren

private java.util.Vector<XMLElement> getSubChildren(XMLElement root,
                                                    java.lang.String[] childdef,
                                                    int depth)
Returns a Vector with all leafs of the tree which is described with childdef beginning at the given depth.

Parameters:
root - the XMLElement which is the current root for the search
childdef - a String array which describes the tree; the last element contains the leaf name
depth - depth to start in childdef
Returns:
a Vector of XMLElements of all leafs founded under root

substituteVariables

public java.io.InputStream substituteVariables(java.io.InputStream input,
                                               VariableSubstitutor substitutor)
                                        throws java.lang.Exception
Creates an temp file in to the substitutor the substituted contents of input writes; close it and (re)open it as FileInputStream. The file will be deleted on exit.

Parameters:
input - the opened input stream which contents should be substituted
substitutor - substitutor which should substitute the contents of input
Returns:
a file input stream of the created temporary file
Throws:
java.lang.Exception

isAttributeYes

public boolean isAttributeYes(XMLElement element,
                              java.lang.String attribute,
                              boolean defaultValue)
Returns whether the value to the given attribute is "yes" or not. If the attribute does not exist, or the value is not "yes" and not "no", the default value is returned.

Parameters:
element - the XML element which contains the attribute
attribute - the name of the attribute
defaultValue - the default value
Returns:
whether the value to the given attribute is "yes" or not

getRequiredAttribute

public java.lang.String getRequiredAttribute(XMLElement element,
                                             java.lang.String attrName)
                                      throws InstallerException
Returns the attribute for the given attribute name. If no attribute exist, an InstallerException with a detail message is thrown.

Parameters:
element - XML element which should contain the attribute
attrName - key of the attribute
Returns:
the attribute as string
Throws:
InstallerException