Java Reference
In-Depth Information
org.w3c.dom.Element fontElement = (org.w3c.dom.Element)list.item(0);
String styleStr = fontElement.getAttribute("fontstyle");
int style = 0;
if(styleStr.equals("plain"))
style = Font.PLAIN;
else if(styleStr.equals("bold"))
style = Font.BOLD;
else if(styleStr.equals("italic"))
style = Font.ITALIC;
else if(styleStr.equals("bold-italic"))
style = Font.BOLD + Font.ITALIC;
else
assert false;
font = new Font(fontElement.getAttribute("fontname"), style,
Integer.parseInt(fontElement.getAttribute("pointsize")));
// Get string bounds
list = xmlElement.getElementsByTagName("bounds");
org.w3c.dom.Element boundsElement = (org.w3c.dom.Element)list.item(0);
this.bounds = new java.awt.Rectangle(origin.x, origin.y,
Integer.parseInt(boundsElement.getAttribute("width")),
Integer.parseInt(boundsElement.getAttribute("height")));
// Get the string
list = xmlElement.getElementsByTagName("string");
org.w3c.dom.Element string = (org.w3c.dom.Element)list.item(0);
list = string.getChildNodes();
StringBuffer textStr = new StringBuffer();
for(int i = 0 ; i<list.getLength() ; i++)
if(list.item(i).getNodeType()==org.w3c.dom.Node.TEXT _ NODE)
textStr.append(((org.w3c.dom.Text)list.item(i)).getData());
text = textStr.toString().trim();
}
The attributes of the <string> element define the font. Only the fontstyle attribute needs some
analysis since we have to represent the style by an integer constant. This means testing for the possible
values for the string and setting the appropriate integer value in style . Of course, we could have
stored the style as a numeric value, but that would have been meaningless to a human reader. Making
the attribute value a descriptor string makes it completely clear. After obtaining the style code we have
an assertion to make sure it actually happened. Of course, since the XML was created by Sketcher, the
only reasons why this would assert is if there is an error in the code somewhere, or the XML was
written by a different version of Sketcher, or the XML was generated by hand. Of course, errors in the
XML due to inconsistencies with the DTD would be caught by the parser and signaled by one or other
of our ErrorHandler methods.
Text content for an element can appear distributed among several child <Text> nodes. We
accommodate this possibilty by concatenating the data from all the child <Text> nodes that we find,
and then trimming any leading and trailing white space from the string before storing it in text .
Search WWH ::




Custom Search