|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| XmlHashtableMappings.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Replica is published under the terms
|
|
| 3 |
* of the Apache Software License.
|
|
| 4 |
*/
|
|
| 5 |
package replica.command.xml;
|
|
| 6 |
|
|
| 7 |
import java.util.Hashtable;
|
|
| 8 |
import java.util.Vector;
|
|
| 9 |
|
|
| 10 |
import org.w3c.dom.Document;
|
|
| 11 |
import org.w3c.dom.Element;
|
|
| 12 |
import org.w3c.dom.Node;
|
|
| 13 |
import org.w3c.dom.NodeList;
|
|
| 14 |
|
|
| 15 |
import replica.command.CommandException;
|
|
| 16 |
import replica.command.HashtableMappings;
|
|
| 17 |
|
|
| 18 |
/**
|
|
| 19 |
* Mappings class that initializes it self from a xml document.
|
|
| 20 |
*
|
|
| 21 |
* @author Pedro Costa
|
|
| 22 |
* @author Helder Silva
|
|
| 23 |
* @since 17/Jan/2004
|
|
| 24 |
*/
|
|
| 25 |
public class XmlHashtableMappings extends HashtableMappings implements XmlConstants{ |
|
| 26 |
|
|
| 27 |
/**
|
|
| 28 |
* Default constructor. Should not be used directly. Use the XmlHashtableMappings(Document) instead.
|
|
| 29 |
*
|
|
| 30 |
* Mainly used for testing.
|
|
| 31 |
*/
|
|
| 32 | 0 |
XmlHashtableMappings(){
|
| 33 | 0 |
super();
|
| 34 |
} |
|
| 35 |
|
|
| 36 |
/**
|
|
| 37 |
* Used to create a Mappings class from a xml document.
|
|
| 38 |
*/
|
|
| 39 | 0 |
public XmlHashtableMappings(Document xmlDocument) {
|
| 40 |
|
|
| 41 | 0 |
initialize( xmlDocument ); |
| 42 |
} |
|
| 43 |
|
|
| 44 |
/**
|
|
| 45 |
* Initialize configuration from a given Xml Document.
|
|
| 46 |
*
|
|
| 47 |
*/
|
|
| 48 | 0 |
protected void initialize(Document xmlDocument) { |
| 49 |
|
|
| 50 | 0 |
Element root = xmlDocument.getDocumentElement(); |
| 51 |
|
|
| 52 | 0 |
if( !root.getTagName().equals(COMMANDS) )
|
| 53 | 0 |
throw new CommandException("Invalid root element on configuration file."); |
| 54 |
|
|
| 55 | 0 |
NodeList nodes = root.getElementsByTagName(MAPPING); |
| 56 | 0 |
int numOfNodes = nodes.getLength();
|
| 57 |
|
|
| 58 | 0 |
for (int i = 0; i < numOfNodes; i++) |
| 59 | 0 |
processMapping( (Element)nodes.item(i) ); |
| 60 |
} |
|
| 61 |
|
|
| 62 |
/**
|
|
| 63 |
* Process a new <i>mapping</i> element.
|
|
| 64 |
*
|
|
| 65 |
* @param mapping the mapping entry on the xml document to be processed.
|
|
| 66 |
*/
|
|
| 67 | 0 |
protected void processMapping( Element mapping ){ |
| 68 |
|
|
| 69 | 0 |
NodeList classes = mapping.getElementsByTagName(CLASS); |
| 70 |
|
|
| 71 | 0 |
if( classes == null || classes.getLength() == 0 ) |
| 72 | 0 |
return;
|
| 73 |
|
|
| 74 | 0 |
String className = getElementText( (Element)classes.item(0) ); |
| 75 |
|
|
| 76 | 0 |
NodeList params = mapping.getElementsByTagName(PARAM); |
| 77 |
|
|
| 78 | 0 |
Hashtable paramsTable = getParameters(params); |
| 79 |
|
|
| 80 | 0 |
NodeList commandsList = mapping.getElementsByTagName( COMMAND ); |
| 81 |
|
|
| 82 | 0 |
String[] commands = getCommands( commandsList ); |
| 83 |
|
|
| 84 | 0 |
addMappingsForClass(className, paramsTable, commands); |
| 85 |
} |
|
| 86 |
|
|
| 87 |
/**
|
|
| 88 |
* Parse a xml node list of <i>command</i> entries, and returns the values of those elements as
|
|
| 89 |
* an array of Strings.
|
|
| 90 |
*
|
|
| 91 |
* @param commandsList the xml node list to be processed.
|
|
| 92 |
* @return the values of the received elements.
|
|
| 93 |
*/
|
|
| 94 | 0 |
protected String[] getCommands( NodeList commandsList ){
|
| 95 |
|
|
| 96 | 0 |
Vector commands = new Vector();
|
| 97 |
|
|
| 98 | 0 |
for (int i = 0; i < commandsList.getLength(); i++) { |
| 99 |
|
|
| 100 | 0 |
Element command = (Element)commandsList.item(i); |
| 101 |
|
|
| 102 | 0 |
String id = getElementText( command ); |
| 103 |
|
|
| 104 | 0 |
if( id != null ) |
| 105 | 0 |
commands.add( id ); |
| 106 |
} |
|
| 107 |
|
|
| 108 | 0 |
return (String[])commands.toArray(new String[commands.size()]); |
| 109 |
} |
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* Util method that returns an element text value. It returns null if there no Text Node as child of this element.
|
|
| 113 |
* @param element the one to check
|
|
| 114 |
* @return the child text node value
|
|
| 115 |
*/
|
|
| 116 | 0 |
protected String getElementText( Element element ){
|
| 117 |
|
|
| 118 | 0 |
String value = null;
|
| 119 |
|
|
| 120 | 0 |
NodeList list = element.getChildNodes(); |
| 121 |
|
|
| 122 | 0 |
for (int i = 0; value == null || i < list.getLength(); i++) { |
| 123 | 0 |
if( list.item(i).getNodeType() == Node.TEXT_NODE )
|
| 124 | 0 |
value = list.item(i).getNodeValue(); |
| 125 |
} |
|
| 126 |
|
|
| 127 | 0 |
return value;
|
| 128 |
} |
|
| 129 |
|
|
| 130 |
/**
|
|
| 131 |
* Extracts parameters initizing values from a list of elements <i>param</i>.
|
|
| 132 |
* If the received list of nodes is null or empty, returns a null table.
|
|
| 133 |
*
|
|
| 134 |
* @param params list of nodes with parameters values
|
|
| 135 |
* @return table with the extracted values
|
|
| 136 |
*/
|
|
| 137 | 0 |
protected Hashtable getParameters(NodeList params) {
|
| 138 |
|
|
| 139 | 0 |
Hashtable paramsTable = null;
|
| 140 |
|
|
| 141 | 0 |
if( params != null && params.getLength() > 0 ){ |
| 142 | 0 |
int length = params.getLength();
|
| 143 | 0 |
paramsTable = new Hashtable( params.getLength() );
|
| 144 |
|
|
| 145 | 0 |
for (int i = 0; i < length; i++) { |
| 146 |
|
|
| 147 | 0 |
Element param = (Element)params.item( i ); |
| 148 |
|
|
| 149 | 0 |
NodeList names = param.getElementsByTagName( NAME ); |
| 150 | 0 |
NodeList values = param.getElementsByTagName( VALUE ); |
| 151 |
|
|
| 152 | 0 |
if( names != null && names.getLength() > 0 && |
| 153 |
values != null && values.getLength() > 0 ){
|
|
| 154 | 0 |
paramsTable.put( getElementText( (Element)names.item(0) ), |
| 155 |
getElementText( (Element)values.item(0) ) ); |
|
| 156 |
} |
|
| 157 |
} |
|
| 158 |
} |
|
| 159 |
|
|
| 160 | 0 |
return paramsTable;
|
| 161 |
} |
|
| 162 |
} |
|
| 163 |
|
|
||||||||||