redora.generator
Class XMLUtil

java.lang.Object
  extended by redora.generator.XMLUtil

public class XMLUtil
extends Object

XML utilities in specific use for handling the model files.

Author:
Nanjing RedOrange (http://www.red-orange.cn)

Constructor Summary
XMLUtil()
           
 
Method Summary
static void addChildElement(Document doc, String tag, String value)
          Adds an child element to the document's root element.
static Node addFinder(Document doc, Node parent, String jsonValue, boolean forceCreate)
           
static Node addFinder(Document doc, Node parent, String name, String jsonValue)
           
static String asString(Document doc)
          Dumps Document to String.
static void attribute(Node node, String name, String value)
          Insert attribute in given node.
static NodeList attributes(Document doc, String filter)
          Return a nodelist with all the /object/attributes, or filtered as you xpath wished.
static Set<String> definedLanguages(Document doc)
          Retrieves the distinct values of all 'language' attributes in given document.
static Map<String,Document> enumerations(Document allModels, String basePackage)
          Retrieves all enum from model doc.
static Node finder(Node parent, String name)
           
static boolean isSortable(Document doc)
          Looks if this model document has the sorted = true tag.
static Document newDocument(String basePackage, String root)
          Create the model XML document and adds the root element and optional a package element.
static NodeList queries(Document doc)
           
static void xsltTransform(Node xml, InputStream xslt, Writer out, Map<String,String> param)
          Transform xml Node with xslt template file to stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtil

public XMLUtil()
Method Detail

xsltTransform

public static void xsltTransform(@NotNull
                                 Node xml,
                                 @NotNull
                                 InputStream xslt,
                                 @NotNull
                                 Writer out,
                                 @NotNull
                                 Map<String,String> param)
                          throws TransformerException
Transform xml Node with xslt template file to stream

Throws:
TransformerException

definedLanguages

@NotNull
public static Set<String> definedLanguages(@NotNull
                                                   Document doc)
                                    throws ModelGenerationException
Retrieves the distinct values of all 'language' attributes in given document.

Parameters:
doc - (Mandatory) The AllModels document
Returns:
Empty or filled set.
Throws:
ModelGenerationException - Wrapper on XPath exceptions

isSortable

public static boolean isSortable(@NotNull
                                 Document doc)
                          throws XPathExpressionException
Looks if this model document has the sorted = true tag.

Parameters:
doc - (Mandatory) A model document
Returns:
True if yes
Throws:
XPathExpressionException - Just passing on

attributes

@NotNull
public static NodeList attributes(@NotNull
                                          Document doc,
                                          @Nullable
                                          String filter)
                           throws XPathExpressionException
Return a nodelist with all the /object/attributes, or filtered as you xpath wished.

Parameters:
doc - (Mandatory) A model document
filter - (Optional) XPathish filter. Default it is '*' (all).
Returns:
A list of attributes
Throws:
XPathExpressionException - Just passing on

queries

public static NodeList queries(@NotNull
                               Document doc)
                        throws XPathExpressionException
Throws:
XPathExpressionException

attribute

public static void attribute(@NotNull
                             Node node,
                             @NotNull
                             String name,
                             @NotNull
                             String value)
Insert attribute in given node. If this attribute already exists, the new attribute will not be inserted

Parameters:
node - (Mandatory) Node in need of attribute
name - (Mandatory) Attribute name
value - (Mandatory) It's value

finder

@Nullable
public static Node finder(@NotNull
                                   Node parent,
                                   @Nullable
                                   String name)

addFinder

@NotNull
public static Node addFinder(@NotNull
                                     Document doc,
                                     @NotNull
                                     Node parent,
                                     String jsonValue,
                                     boolean forceCreate)

addFinder

@NotNull
public static Node addFinder(@NotNull
                                     Document doc,
                                     @NotNull
                                     Node parent,
                                     String name,
                                     String jsonValue)

addChildElement

public static void addChildElement(@NotNull
                                   Document doc,
                                   @NotNull
                                   String tag,
                                   @Nullable
                                   String value)
Adds an child element to the document's root element. Only adds the tag if it not yet already exists.

Parameters:
doc - (Mandatory) The XML document
tag - (Mandatory) The element's name
value - (Optional) It's value, when null, the tag will be empty

enumerations

@NotNull
public static Map<String,Document> enumerations(@NotNull
                                                        Document allModels,
                                                        @NotNull
                                                        String basePackage)
                                         throws ModelGenerationException
Retrieves all enum from model doc.

Parameters:
allModels - (Mandatory) The allModels document
basePackage - (Mandatory)
Returns:
Empty or filled set.
Throws:
ModelGenerationException - Wrapper on XPath exceptions

newDocument

@NotNull
public static Document newDocument(@Nullable
                                           String basePackage,
                                           @NotNull
                                           String root)
                            throws ModelGenerationException
Create the model XML document and adds the root element and optional a package element. This method is simple and created for your convenience.

Parameters:
basePackage - (Optional) Adds a 'package' element, it will the the root's first child
root - (Mandatory) root element name
Returns:
Document The Model document
Throws:
ModelGenerationException - Wrapper on ParserConfigurationException: should not happen

asString

@NotNull
public static String asString(@NotNull
                                      Document doc)
Dumps Document to String. Any exceptions are ignored

Parameters:
doc - (Mandatory)
Returns:
String of given doc.


Copyright © 2012 Nanjing RedOrange Co. ltd. All Rights Reserved.