org.mmbase.applications.editwizard
Class Utils

java.lang.Object
  extended byorg.mmbase.applications.editwizard.Utils

public class Utils
extends java.lang.Object

This class contains static utility methods used by the editwizard. Most methods handle xml functions for you and are just to support ease and lazyness.

Since:
MMBase-1.6
Version:
$Id: Utils.java,v 1.37.2.1 2004/04/15 10:55:39 michiel Exp $
Author:
Kars Veling, Pierre van Rooden, Michiel Meeuwissen
Warning: No Java Documentation Available.

Constructor Summary
Utils()
           
 
Method Summary
static java.util.Collection appendNodeList(org.w3c.dom.NodeList list, org.w3c.dom.Node dest)
          This method clones, imports and places all nodes in the list.
static void copyAllAttributes(org.w3c.dom.Node source, org.w3c.dom.Node dest)
          Copies all attributes from one node to the other.
static void copyAllAttributes(org.w3c.dom.Node source, org.w3c.dom.Node dest, java.util.List except)
          Same as above, but now you can supply a Vector with names which should NOT be copied.
static org.w3c.dom.Node createAndAppendNode(org.w3c.dom.Node parentnode, java.lang.String nodename, java.lang.String nodevalue)
          This method creates a new node, places text and attaches it to the parent.
static org.w3c.dom.Document emptyDocument()
          This method returns an empty XMLDocument.
static java.lang.String fillInParams(java.lang.String text, java.util.Map params)
          This method fills in params in a string.
static java.lang.String getAttribute(org.w3c.dom.Node node, java.lang.String name)
          Gets an attribute of an node.
static java.lang.String getAttribute(org.w3c.dom.Node node, java.lang.String name, java.lang.String defaultvalue)
          Gets an attribute of an node.
static javax.xml.parsers.DocumentBuilder getDocumentBuilder(boolean validate)
          This method returns a new instance of a DocumentBuilder.
static java.lang.String getSerializedXML(org.w3c.dom.Node node)
          Serialize a node to a string, and return the result.
static java.lang.String getText(org.w3c.dom.Node node)
          Returns the text value of the given node
static java.lang.String getText(org.w3c.dom.Node node, java.lang.String defaultvalue)
          Returns the text value of the given node.
static java.lang.String getText(org.w3c.dom.Node node, java.lang.String defaultvalue, java.util.Map params)
          Returns the text value of the given node.
static java.lang.String getXML(org.w3c.dom.Node node)
          Serialize a node and returns the resulting String.
static java.lang.String getXML(org.w3c.dom.NodeList nodeList)
          Serialize a nodelist and returns the resulting String (for debugging).
static org.w3c.dom.Document loadXMLFile(java.io.File file)
          This method can load a xml file and returns the resulting document.
static java.lang.String multipleReplace(java.lang.String text, java.lang.String searchfor, java.lang.String replacewith)
          replaces single or multiple occurences of a string in a given source string.
static org.w3c.dom.Document parseXML(java.lang.String xml)
          With this method you can parse a xml string and get the resulting Document.
static void printXML(org.w3c.dom.Node node, java.io.Writer writer)
          Serialize a node to the given writer.
static org.w3c.dom.NodeList selectNodeList(org.w3c.dom.Node contextnode, java.lang.String xpath)
          This method selects a multiple nodes using the given contextnode and xpath.
static org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node contextnode, java.lang.String xpath)
          This method selects a single node using the given contextnode and xpath.
static java.lang.String selectSingleNodeText(org.w3c.dom.Node node, java.lang.String xpath, java.lang.String defaultvalue)
          Selects a single node using the given xpath and uses the given node a a starting context and returns the textnode found.
static java.lang.String selectSingleNodeText(org.w3c.dom.Node node, java.lang.String xpath, java.lang.String defaultvalue, Cloud cloud)
          Selects a single node using the given xpath and uses the given node a a starting context and returns the textnode found.
static void setAttribute(org.w3c.dom.Node node, java.lang.String name, java.lang.String value)
          Sets an attribute of a specific node.
protected static void setStylesheetParams(javax.xml.transform.Transformer transformer, java.util.Map params)
          This method can set the stylesheetparams for a transformer.
static void storeText(org.w3c.dom.Node node, java.lang.String text)
          Same as above, but without the params.
static void storeText(org.w3c.dom.Node node, java.lang.String text, java.util.Map params)
          This method stores text in a node.
static java.lang.String stringFormatted(org.w3c.dom.Node node)
          For debugging purposes.
static int tagNodeList(org.w3c.dom.NodeList list, java.lang.String name, java.lang.String pre)
          This method tags all nodes in the nodelist.
static int tagNodeList(org.w3c.dom.NodeList list, java.lang.String name, java.lang.String pre, int start)
          Same as above, but now you can supply a startnumber.
static java.lang.String transformAttribute(org.w3c.dom.Node context, java.lang.String attributeTemplate)
          transforms an attribute.
static java.lang.String transformAttribute(org.w3c.dom.Node context, java.lang.String attributeTemplate, boolean plainTextIsPath)
          same as above, but now you can supply if the given attributeTemplate is already a xpath or not.
static java.lang.String transformAttribute(org.w3c.dom.Node context, java.lang.String attributeTemplate, boolean plainTextIsXpath, java.util.Map params)
          Executes an attribute template.
static org.w3c.dom.Node transformNode(org.w3c.dom.Node node, java.io.File xslFile, URIResolver uri)
          same as above, but now the result is returned in a new Node and some less params.
static org.w3c.dom.Node transformNode(org.w3c.dom.Node node, java.io.File xslFile, URIResolver uri, java.util.Map params)
          same as above, but now you can supply a params hashtable.
static void transformNode(org.w3c.dom.Node node, java.io.File xslFile, URIResolver uri, javax.xml.transform.Result result, java.util.Map params)
          This method does a standard XSL(T) transform on a node as a base context node and sends it to the given Result result.
static void transformNode(org.w3c.dom.Node node, java.io.File xslFile, URIResolver uri, java.io.Writer out)
          same as above, but now the result is written to the writer.
static void transformNode(org.w3c.dom.Node node, java.io.File xslFile, URIResolver uri, java.io.Writer out, java.util.Map params)
          same as above, but now the result is written to the writer and you can use params.
static org.w3c.dom.Node transformNode(org.w3c.dom.Node node, java.lang.String xslFile, URIResolver uri, java.io.Writer out, java.util.Map params)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

getDocumentBuilder

public static javax.xml.parsers.DocumentBuilder getDocumentBuilder(boolean validate)
This method returns a new instance of a DocumentBuilder.

Returns:
a DocumentBuilder.

emptyDocument

public static org.w3c.dom.Document emptyDocument()
This method returns an empty XMLDocument.

Returns:
a new empty Document. Returns null if something went wrong.

loadXMLFile

public static org.w3c.dom.Document loadXMLFile(java.io.File file)
                                        throws WizardException
This method can load a xml file and returns the resulting document. If something went wrong, null is returned.

Parameters:
file - the file to be loaded.
Returns:
The loaded xml Document
Throws:
WizardException - if the document is invalid

parseXML

public static org.w3c.dom.Document parseXML(java.lang.String xml)
                                     throws WizardException
With this method you can parse a xml string and get the resulting Document.

Parameters:
xml - The xml string to be parsed. Note that you should supply xml for a valid document (one root node, etc)
Returns:
The newly created xml Document
Throws:
WizardException - if something went wrong

printXML

public static void printXML(org.w3c.dom.Node node,
                            java.io.Writer writer)
Serialize a node to the given writer.

Parameters:
node - The node to serialize
writer - The writer where the stream should be written to.

getSerializedXML

public static java.lang.String getSerializedXML(org.w3c.dom.Node node)
Serialize a node to a string, and return the result.

Parameters:
node - The node to serialize

getXML

public static java.lang.String getXML(org.w3c.dom.Node node)
Serialize a node and returns the resulting String.

Parameters:
node - The node to serialize
Returns:
The resulting string.

getXML

public static java.lang.String getXML(org.w3c.dom.NodeList nodeList)
Serialize a nodelist and returns the resulting String (for debugging).

Since:
MMBase-1.7.1

setAttribute

public static void setAttribute(org.w3c.dom.Node node,
                                java.lang.String name,
                                java.lang.String value)
Sets an attribute of a specific node. If the attribute does not exist, a new attribute is created. If the attribute already exists, the value is overwritten with the new one.

Parameters:
node - The node of which the

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Node node,
                                            java.lang.String name)
Gets an attribute of an node.

Parameters:
node - the node to get the attribute from
name - the attributename requested
Returns:
The value of the attribute. Returns "" if none exists.

getAttribute

public static java.lang.String getAttribute(org.w3c.dom.Node node,
                                            java.lang.String name,
                                            java.lang.String defaultvalue)
Gets an attribute of an node.

Parameters:
node - the node to get the attribute from
name - the attributename requested
defaultvalue - the defaultvalue what should be returned if attribute was not found.
Returns:
The value of the attribute. Returns the defaultvalue if none exists.

getText

public static java.lang.String getText(org.w3c.dom.Node node)
Returns the text value of the given node

Parameters:
node - the node where you want the text from.
Returns:
The value of the containing textnode. If no textnode present, "" is returned.

getText

public static java.lang.String getText(org.w3c.dom.Node node,
                                       java.lang.String defaultvalue,
                                       java.util.Map params)
Returns the text value of the given node. But can do more.

Parameters:
node - the node where you want the text from.
defaultvalue - of no text is found, this defaultvalue will be returned
params - params to be used. eg.: $username will be replaced by the values in the hashtable, if a 'username' key is in the hashtable.
Returns:
The value of the containing textnode. If no textnode present, defaultvalue is returned.

getText

public static java.lang.String getText(org.w3c.dom.Node node,
                                       java.lang.String defaultvalue)
Returns the text value of the given node.

Parameters:
node - the node where you want the text from.
defaultvalue - if no text is found, this defaultvalue will be returned
Returns:
The value of the containing textnode. If no textnode present, defaultvalue is returned.

selectSingleNodeText

public static java.lang.String selectSingleNodeText(org.w3c.dom.Node node,
                                                    java.lang.String xpath,
                                                    java.lang.String defaultvalue)
Selects a single node using the given xpath and uses the given node a a starting context and returns the textnode found. If no text is found, the default value is given.

Parameters:
node - the contextnode to start the xpath from.
xpath - the xpath which should be fired.
defaultvalue - this value will be returned when no node is found using the xpath.
Returns:
The text string.

selectSingleNodeText

public static java.lang.String selectSingleNodeText(org.w3c.dom.Node node,
                                                    java.lang.String xpath,
                                                    java.lang.String defaultvalue,
                                                    Cloud cloud)
Selects a single node using the given xpath and uses the given node a a starting context and returns the textnode found. If no text is found, the default value is given. If a cloud argument is passed, it is used to select the most approprite text (using xml:lang attributes) depending on the cloud's properties.

Parameters:
node - the contextnode to start the xpath from.
xpath - the xpath which should be fired.
defaultvalue - this value will be returned when no node is found using the xpath.
cloud - the cloud whose locale is to be used for selecting language-specific texts
Returns:
The text string.

storeText

public static void storeText(org.w3c.dom.Node node,
                             java.lang.String text,
                             java.util.Map params)
This method stores text in a node. If needed, a new text node is created.

Parameters:
node - the parentnode on which a textnode should be created or overwritten.
text - The text what should be placed in the textnode.
params - optional params which should be used in a replace action.

storeText

public static void storeText(org.w3c.dom.Node node,
                             java.lang.String text)
Same as above, but without the params. This method first removes all text/CDATA nodes. It then adds the parsed text as either a text ndoe or a CDATA node.


appendNodeList

public static java.util.Collection appendNodeList(org.w3c.dom.NodeList list,
                                                  org.w3c.dom.Node dest)
This method clones, imports and places all nodes in the list.

Returns:
Collection with the new nodes.

createAndAppendNode

public static org.w3c.dom.Node createAndAppendNode(org.w3c.dom.Node parentnode,
                                                   java.lang.String nodename,
                                                   java.lang.String nodevalue)
This method creates a new node, places text and attaches it to the parent.

Parameters:
parentnode - Place where new node should be appended
nodename - the name of the new node
nodevalue - the new nodevalue
Returns:
the newly created node

tagNodeList

public static int tagNodeList(org.w3c.dom.NodeList list,
                              java.lang.String name,
                              java.lang.String pre)
This method tags all nodes in the nodelist. Id-counter starts counting with 1.

Parameters:
list - the nodelist
name - the name of the tags
pre - the prefix what should be used in the tag-values

tagNodeList

public static int tagNodeList(org.w3c.dom.NodeList list,
                              java.lang.String name,
                              java.lang.String pre,
                              int start)
Same as above, but now you can supply a startnumber.


copyAllAttributes

public static void copyAllAttributes(org.w3c.dom.Node source,
                                     org.w3c.dom.Node dest)
Copies all attributes from one node to the other.

Parameters:
source - One node
dest - The other node

copyAllAttributes

public static void copyAllAttributes(org.w3c.dom.Node source,
                                     org.w3c.dom.Node dest,
                                     java.util.List except)
Same as above, but now you can supply a Vector with names which should NOT be copied.


setStylesheetParams

protected static void setStylesheetParams(javax.xml.transform.Transformer transformer,
                                          java.util.Map params)
This method can set the stylesheetparams for a transformer.

Parameters:
transformer - The transformer.
params - The params to be placed. Standard name/value pairs.

transformNode

public static void transformNode(org.w3c.dom.Node node,
                                 java.io.File xslFile,
                                 URIResolver uri,
                                 javax.xml.transform.Result result,
                                 java.util.Map params)
                          throws javax.xml.transform.TransformerException
This method does a standard XSL(T) transform on a node as a base context node and sends it to the given Result result.

Parameters:
node - the base context node to run the xsl(t) against.
xslFile - the xsl file
result - The place where to put the result of the transformation
params - Optional params.
Throws:
javax.xml.transform.TransformerException

stringFormatted

public static java.lang.String stringFormatted(org.w3c.dom.Node node)
For debugging purposes. Return the constructed node as a String.


transformNode

public static org.w3c.dom.Node transformNode(org.w3c.dom.Node node,
                                             java.io.File xslFile,
                                             URIResolver uri)
                                      throws javax.xml.transform.TransformerException
same as above, but now the result is returned in a new Node and some less params.

Parameters:
node - the base context node.
xslFile - the xslFile.
Returns:
the documentelement of the resulting xml (of the transformation)
Throws:
javax.xml.transform.TransformerException

transformNode

public static org.w3c.dom.Node transformNode(org.w3c.dom.Node node,
                                             java.io.File xslFile,
                                             URIResolver uri,
                                             java.util.Map params)
                                      throws javax.xml.transform.TransformerException
same as above, but now you can supply a params hashtable.

Throws:
javax.xml.transform.TransformerException

transformNode

public static void transformNode(org.w3c.dom.Node node,
                                 java.io.File xslFile,
                                 URIResolver uri,
                                 java.io.Writer out)
                          throws javax.xml.transform.TransformerException
same as above, but now the result is written to the writer.

Throws:
javax.xml.transform.TransformerException

transformNode

public static void transformNode(org.w3c.dom.Node node,
                                 java.io.File xslFile,
                                 URIResolver uri,
                                 java.io.Writer out,
                                 java.util.Map params)
                          throws javax.xml.transform.TransformerException
same as above, but now the result is written to the writer and you can use params.

Throws:
javax.xml.transform.TransformerException

transformNode

public static org.w3c.dom.Node transformNode(org.w3c.dom.Node node,
                                             java.lang.String xslFile,
                                             URIResolver uri,
                                             java.io.Writer out,
                                             java.util.Map params)
                                      throws javax.xml.transform.TransformerException
Throws:
javax.xml.transform.TransformerException

transformAttribute

public static java.lang.String transformAttribute(org.w3c.dom.Node context,
                                                  java.lang.String attributeTemplate)
transforms an attribute. A attributeTemplate is used to place values. with { and } people can place simple xpaths to calculate values.

Parameters:
context - the contextnode
attributeTemplate - the template to evaluate.
Returns:
a string with the result.

transformAttribute

public static java.lang.String transformAttribute(org.w3c.dom.Node context,
                                                  java.lang.String attributeTemplate,
                                                  boolean plainTextIsPath)
same as above, but now you can supply if the given attributeTemplate is already a xpath or not. (Default should be false).


transformAttribute

public static java.lang.String transformAttribute(org.w3c.dom.Node context,
                                                  java.lang.String attributeTemplate,
                                                  boolean plainTextIsXpath,
                                                  java.util.Map params)
Executes an attribute template. example node: Johnny example template: "hi there {person/name}, you're looking {person/@state}"

Parameters:
context - the Node on which any xpaths are fired.
attributeTemplate - the String containting an attribute template.
plainTextIsXpath - true means that if the template doesn't contain any curly braces, the template is assumed to be a valid xpath (instead of plain data). Else the template is assumed to be a valid attribute template.

selectSingleNode

public static org.w3c.dom.Node selectSingleNode(org.w3c.dom.Node contextnode,
                                                java.lang.String xpath)
This method selects a single node using the given contextnode and xpath.

Parameters:
contextnode -
xpath -
Returns:
The found node.

selectNodeList

public static org.w3c.dom.NodeList selectNodeList(org.w3c.dom.Node contextnode,
                                                  java.lang.String xpath)
This method selects a multiple nodes using the given contextnode and xpath.

Parameters:
contextnode -
xpath -
Returns:
The found nodes in a NodeList.

fillInParams

public static java.lang.String fillInParams(java.lang.String text,
                                            java.util.Map params)
This method fills in params in a string. It uses the params hashtable to do a string replace. if the name/value: username --> kars is in the hashtable, all $username occurences will be replaced with kars. Note: #multipleReplace method is used for replacements.

Parameters:
text - the source text to be used
params - the table with params (name/value pairs)
Returns:
The resulting string

multipleReplace

public static java.lang.String multipleReplace(java.lang.String text,
                                               java.lang.String searchfor,
                                               java.lang.String replacewith)
replaces single or multiple occurences of a string in a given source string. Note: if the searchfor-string contains the replacewith-string, no replacement is made. It would result in an inifinite loop!

Parameters:
text - the source text (the haystack)
searchfor - the needle. the this we're looking for
replacewith - the string which should be placed.


MMBase build 1.7.1.20041002