Java Reference
In-Depth Information
This uses essentially the same mechanism as the previous method. Here the attributes strings represent
double values so we use the static parseDouble() method from the Double class to convert them to
the numeric equivalent.
Every sketcher element has a color, a position, and an angle that are stored in base class fields so we can
create a base class constructor to initialize these from the document node for the element:
protected Element(org.w3c.dom.Element xmlElement) {
// Get the <color> element
org.w3c.dom.NodeList list = xmlElement.getElementsByTagName("color");
setElementColor((org.w3c.dom.Element)list.item(0)); // Set the color
list = xmlElement.getElementsByTagName("position"); // Get <position>
setElementPosition((org.w3c.dom.Element)list.item(0)); // Set the position
angle = Double.parseDouble(xmlElement.getAttribute("angle")); // Set the angle
}
We have declared this constructor as protected to prevent the possibility of it being called externally.
Every one of our new constructors in the inner classes to Element will call this constructor first. An
important point to remember is that if a constructor for a derived class object does not call a base class
constructor as the first statement in the body of the constructor, the compiler will insert a call to the no-
arg constructor for the base class. This means that a base class always has to have a no-arg constructor if
the derived class constructors do not call a base class constructor with arguments.
We first extract the child element for the current element with the name "color" by calling the
getElementsByTagName() method for xmlElement . This method, declared in the
org.w3c.dom.Element interface, returns a NodeList object containing all the child nodes with the
given name. If you pass the string "*" as the argument to this method, it will return all child
org.w3c.dom.Element objects in the node list. There's another method,
getElementsByTagNameNS() , that is declared in the Element interface that does the same for
documents using namespaces. The first argument in this case is the namespace URI and the second
argument is the element name. The strings to either or both arguments can be "*" , in which case all
namespaces and/or names will be matched.
We pass the reference to the element with the name "color" to the setElementColor() method
that is inherited from the base class. This sets the value of the color field in the base class.
Next we initialize the position field in the Element class by calling the setElementPosition()
method. The process is much the same as for the color field. Lastly we set the angle field by
converting the string that is the value for the angle attribute for the current node to type double .
Now we are ready to add the new constructors to the subclasses to create sketch elements from XML
elements.
Creating a Line Element
We can construct an Element.Line object by first calling the base class constructor we have just
defined to set the color , position , and angle fields, and then setting the line field in the derived
class. Here's the code for the constructor:
Search WWH ::




Custom Search