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 XmlHashtableMappings(){
33 super();
34 }
35
36 /***
37 * Used to create a Mappings class from a xml document.
38 */
39 public XmlHashtableMappings(Document xmlDocument) {
40
41 initialize( xmlDocument );
42 }
43
44 /***
45 * Initialize configuration from a given Xml Document.
46 *
47 */
48 protected void initialize(Document xmlDocument) {
49
50 Element root = xmlDocument.getDocumentElement();
51
52 if( !root.getTagName().equals(COMMANDS) )
53 throw new CommandException("Invalid root element on configuration file.");
54
55 NodeList nodes = root.getElementsByTagName(MAPPING);
56 int numOfNodes = nodes.getLength();
57
58 for (int i = 0; i < numOfNodes; i++)
59 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 protected void processMapping( Element mapping ){
68
69 NodeList classes = mapping.getElementsByTagName(CLASS);
70
71 if( classes == null || classes.getLength() == 0 )
72 return;
73
74 String className = getElementText( (Element)classes.item(0) );
75
76 NodeList params = mapping.getElementsByTagName(PARAM);
77
78 Hashtable paramsTable = getParameters(params);
79
80 NodeList commandsList = mapping.getElementsByTagName( COMMAND );
81
82 String[] commands = getCommands( commandsList );
83
84 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 protected String[] getCommands( NodeList commandsList ){
95
96 Vector commands = new Vector();
97
98 for (int i = 0; i < commandsList.getLength(); i++) {
99
100 Element command = (Element)commandsList.item(i);
101
102 String id = getElementText( command );
103
104 if( id != null )
105 commands.add( id );
106 }
107
108 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 protected String getElementText( Element element ){
117
118 String value = null;
119
120 NodeList list = element.getChildNodes();
121
122 for (int i = 0; value == null || i < list.getLength(); i++) {
123 if( list.item(i).getNodeType() == Node.TEXT_NODE )
124 value = list.item(i).getNodeValue();
125 }
126
127 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 protected Hashtable getParameters(NodeList params) {
138
139 Hashtable paramsTable = null;
140
141 if( params != null && params.getLength() > 0 ){
142 int length = params.getLength();
143 paramsTable = new Hashtable( params.getLength() );
144
145 for (int i = 0; i < length; i++) {
146
147 Element param = (Element)params.item( i );
148
149 NodeList names = param.getElementsByTagName( NAME );
150 NodeList values = param.getElementsByTagName( VALUE );
151
152 if( names != null && names.getLength() > 0 &&
153 values != null && values.getLength() > 0 ){
154 paramsTable.put( getElementText( (Element)names.item(0) ),
155 getElementText( (Element)values.item(0) ) );
156 }
157 }
158 }
159
160 return paramsTable;
161 }
162 }
This page was automatically generated by Maven