org.mmbase.applications.editwizard
Class Utils

java.lang.Object
  extended by org.mmbase.applications.editwizard.Utils

public class Utils
extends 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 43027 2010-08-02 13:50:39Z pierre $
Author:
Kars Veling, Pierre van Rooden, Michiel Meeuwissen
Javadoc:

Constructor Summary
Utils()
           
 
Method Summary
static Collection<Node> appendNodeList(NodeList list, Node dest)
          This method clones, imports and places all nodes in the list.
static void copyAllAttributes(Node source, Node dest)
          Copies all attributes from one node to the other.
static void copyAllAttributes(Node source, Node dest, List<String> except)
          Same as above, but now you can supply a Vector with names which should NOT be copied.
static Node createAndAppendNode(Node parentnode, String nodename, String nodevalue)
          This method creates a new node, places text and attaches it to the parent.
static Document emptyDocument()
          This method returns an empty XMLDocument.
static String fillInParams(String text, Map<String,?> params)
          This method fills in params in a string.
static String getAttribute(Node node, String name)
          Gets an attribute of an node.
static String getAttribute(Node node, String name, String defaultvalue)
          Gets an attribute of an node.
static DocumentBuilder getDocumentBuilder(boolean validate)
          This method returns a new instance of a DocumentBuilder.
static String getSerializedXML(Node node)
          Serialize a node to a string, and return the result.
static String getText(Node node)
          Returns the text value of the given node
static String getText(Node node, String defaultvalue)
          Returns the text value of the given node.
static String getText(Node node, String defaultvalue, Map<String,?> params)
          Returns the text value of the given node.
static String getXML(Node node)
          Serialize a node and returns the resulting String.
static String getXML(NodeList nodeList)
          Serialize a nodelist and returns the resulting String (for debugging).
static boolean isEmptyNodeList(NodeList list)
          Is nodelist empty
static boolean isNotEmptyNodeList(NodeList list)
          Is nodelist not empty
static Document loadXMLFile(URL file)
          This method can load a xml file and returns the resulting document.
static String multipleReplace(String text, String searchfor, String replacewith)
          replaces single or multiple occurences of a string in a given source string.
static Document parseXML(String xml)
          With this method you can parse a xml string and get the resulting Document.
static void printXML(Node node, Writer writer)
          Serialize a node to the given writer.
static NodeList selectNodeList(Node contextNode, String xpath)
          This method selects a multiple nodes using the given contextNode and xpath.
static Node selectSingleNode(Node contextNode, String xpath)
          This method selects a single node using the given contextNode and xpath.
static String selectSingleNodeText(Node node, String xpath, 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 String selectSingleNodeText(Node node, String xpath, 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(Node node, String name, String value)
          Sets an attribute of a specific node.
protected static void setStylesheetParams(Transformer transformer, Map<String,?> params, Cloud cloud)
          This method can set the stylesheetparams for a transformer.
static void storeText(Node node, String text)
          Same as above, but without the params.
static void storeText(Node node, String text, Map<String,?> params)
          This method stores text in a node.
static String stringFormatted(Node node)
          For debugging purposes.
static int tagNodeList(NodeList list, String name, String pre)
          This method tags all nodes in the nodelist.
static int tagNodeList(NodeList list, String name, String pre, int start)
          Same as above, but now you can supply a startnumber.
static String transformAttribute(Node context, String attributeTemplate)
          transforms an attribute.
static String transformAttribute(Node context, String attributeTemplate, boolean plainTextIsPath)
          same as above, but now you can supply if the given attributeTemplate is already a xpath or not.
static String transformAttribute(Node context, String attributeTemplate, boolean plainTextIsXpath, Map<String,?> params)
          Executes an attribute template.
static Node transformNode(Node node, String xslFile, URIResolver uri, Writer out, Map<String,?> params, Cloud cloud)
           
static Node transformNode(Node node, URL xslFile, URIResolver uri)
          same as above, but now the result is returned in a new Node and some less params.
static Node transformNode(Node node, URL xslFile, URIResolver uri, Map<String,?> params, Cloud cloud)
          same as above, but now you can supply a params hashtable.
static void transformNode(Node node, URL xslFile, URIResolver uri, Result result, Map<String,?> params, Cloud cloud)
          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(Node node, URL xslFile, URIResolver uri, Writer out)
          same as above, but now the result is written to the writer.
static void transformNode(Node node, URL xslFile, URIResolver uri, Writer out, Map<String,?> params, Cloud cloud)
          same as above, but now the result is written to the writer and you can use 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 DocumentBuilder getDocumentBuilder(boolean validate)
This method returns a new instance of a DocumentBuilder.

Returns:
a DocumentBuilder.

emptyDocument

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

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

loadXMLFile

public static Document loadXMLFile(URL 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 Document parseXML(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(Node node,
                            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 String getSerializedXML(Node node)
Serialize a node to a string, and return the result.

Parameters:
node - The node to serialize

getXML

public static String getXML(Node node)
Serialize a node and returns the resulting String.

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

getXML

public static String getXML(NodeList nodeList)
Serialize a nodelist and returns the resulting String (for debugging).

Since:
MMBase-1.7.1

setAttribute

public static void setAttribute(Node node,
                                String name,
                                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 String getAttribute(Node node,
                                  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 String getAttribute(Node node,
                                  String name,
                                  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 String getText(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 String getText(Node node,
                             String defaultvalue,
                             Map<String,?> 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 String getText(Node node,
                             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 String selectSingleNodeText(Node node,
                                          String xpath,
                                          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 String selectSingleNodeText(Node node,
                                          String xpath,
                                          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(Node node,
                             String text,
                             Map<String,?> 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(Node node,
                             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 Collection<Node> appendNodeList(NodeList list,
                                              Node dest)
This method clones, imports and places all nodes in the list.

Returns:
Collection with the new nodes.

createAndAppendNode

public static Node createAndAppendNode(Node parentnode,
                                       String nodename,
                                       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(NodeList list,
                              String name,
                              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(NodeList list,
                              String name,
                              String pre,
                              int start)
Same as above, but now you can supply a startnumber.


copyAllAttributes

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

Parameters:
source - One node
dest - The other node

copyAllAttributes

public static void copyAllAttributes(Node source,
                                     Node dest,
                                     List<String> except)
Same as above, but now you can supply a Vector with names which should NOT be copied.


setStylesheetParams

protected static void setStylesheetParams(Transformer transformer,
                                          Map<String,?> params,
                                          Cloud cloud)
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(Node node,
                                 URL xslFile,
                                 URIResolver uri,
                                 Result result,
                                 Map<String,?> params,
                                 Cloud cloud)
                          throws 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:
TransformerException

stringFormatted

public static String stringFormatted(Node node)
For debugging purposes. Return the constructed node as a String.


transformNode

public static Node transformNode(Node node,
                                 URL xslFile,
                                 URIResolver uri)
                          throws 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:
TransformerException

transformNode

public static Node transformNode(Node node,
                                 URL xslFile,
                                 URIResolver uri,
                                 Map<String,?> params,
                                 Cloud cloud)
                          throws TransformerException
same as above, but now you can supply a params hashtable.

Throws:
TransformerException

transformNode

public static void transformNode(Node node,
                                 URL xslFile,
                                 URIResolver uri,
                                 Writer out)
                          throws TransformerException
same as above, but now the result is written to the writer.

Throws:
TransformerException

transformNode

public static void transformNode(Node node,
                                 URL xslFile,
                                 URIResolver uri,
                                 Writer out,
                                 Map<String,?> params,
                                 Cloud cloud)
                          throws TransformerException
same as above, but now the result is written to the writer and you can use params.

Throws:
TransformerException

transformNode

public static Node transformNode(Node node,
                                 String xslFile,
                                 URIResolver uri,
                                 Writer out,
                                 Map<String,?> params,
                                 Cloud cloud)
                          throws TransformerException
Throws:
TransformerException

transformAttribute

public static String transformAttribute(Node context,
                                        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 String transformAttribute(Node context,
                                        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 String transformAttribute(Node context,
                                        String attributeTemplate,
                                        boolean plainTextIsXpath,
                                        Map<String,?> 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 Node selectSingleNode(Node contextNode,
                                    String xpath)
This method selects a single node using the given contextNode and xpath.

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

selectNodeList

public static NodeList selectNodeList(Node contextNode,
                                      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 String fillInParams(String text,
                                  Map<String,?> 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 String multipleReplace(String text,
                                     String searchfor,
                                     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.

isEmptyNodeList

public static boolean isEmptyNodeList(NodeList list)
Is nodelist empty

Parameters:
list - NodeList
Returns:
true when empty

isNotEmptyNodeList

public static boolean isNotEmptyNodeList(NodeList list)
Is nodelist not empty

Parameters:
list - NodeList
Returns:
true when not empty


MMBase 2.0-SNAPSHOT - null