The class CSSWriter
As mentioned previously, Java does not provide a class to save information in a StyleSheet to a file. To be able to save CSS information, SimplyHTML defines and uses class CSSWriter. CSSWriter is passed a Writer object and a StyleSheet object upon construction. The StyleSheet object contains the CSS information to be saved and the Writer object identifies the destination to store the CSS information at.
In the constructor of CSSWriter, the two parameters Writer and StyleSheet are stored in respective fields w and s of class CSSWriter for later use. To actually write out the styles found in the StyleSheet s, method write is used.
Method write
In method write an Enumeration of all rules in the StyleSheet is created. An Enumeration is a good way to iterate over a collection of elements. It provides two convenience methods hasMoreElements and nextElement for this purpose. While there are more Elements in the Enumeration of styles, method write gets the next element in the Enumeration being the name of the next Style object.
It then gets the Style object identified by that name. Style objects are AttributeSet objects that can contain attribute objects or other AttributeSet objects. The length of the style name is taken to find out how far from the left side the style attributes have to be written to file. This indentation length is stored in field indentLen of class CSSWriter.
First the style name is written to file followed by the character opening a collection of attributes for a CSS style ( '{' ). Every style except the one identified by StyleContext.DEFAULT_STYLE then is written to file by calling method writeStyle on respective Style object.
Method writeStyle
In method writeStyle again an Enumeration is used to go through all attributes found in the style AttributeSet. An AttributeSet is a pair of objects for the key and the value of a style attribute. For every element in the Enumeration the key and value objects are read.
Attribute StyleConstants.NameAttribute can be left out from writing because it contains the name of the style, this Style object is describing. As well the attribute StyleConstants.ResolveAttribute is not written to file itself, because it contains another AttributeSet in its value object. For attributes of type StyleConstants.ResolveAttribute method writeStyle is called recursively to write out all attributes found in this attribute.
All other attributes are written to file by first writing the key object having the name of the attribute (such as font-size, left-margin, etc.) followed by a colon, the attribute value (such as '12pt' or '20%', etc.) and a semicolon. Every attribute except the first is preceded by a new line and the indentation needed to make the collection of attributes more legible.
Finally the closing character for a CSS style ( '}' ) and a new line is written.
Using the appropriate line separator
At design time it can not be predicted on which operating system application SimplyHTML might be used. As different operating systems use different line separators with their file system, this has to be taken into account for our save routine. CSSWriter gets the correct line separator from a global constant defined in Java with command System.getProperty("line.separator"). The value returned by this call is appended to every line written to the target style sheet file.