Saving CSS style information

A style sheet is saved by application SimplyHTML with method saveStyleSheet in class DocumentPane. This method is called by method saveDocument whenever a document is saved. To read more about HTML and style sheets in general and about how SimplyHTML uses style sheets, see chapters 'Style sheets and HTML documents' and ' Style handling design in SimplyHTML'.

Method saveStyleSheet

Method saveStyleSheet first determines the file name of the style sheet associated with the document to be saved by calling mehtod getStyleSheetName (see below).

getStyleSheetName returns the file name as a URL string. A URL object is created with that string and method getFile is called on that URL object to transform the URL string to a file string. This file string is taken to create a new File object for the style sheet to be saved.

With the File object it is first ensured that the file does not already exist with the help of method File.exists. If it exists, the style sheet is not saved (see ' Style handling design in SimlpyHTML').

If the file does not exist, it is created by calling method createNewFile on the File object. An OutputStream object is opened on the newly created file and an OutputStreamWriter is created for that OutputStream.

If the document to be saved has been newly created, the StyleSheet object of SHTMLEditorKit is taken to be written to file. If the document was loaded from file, the style sheet of the document is taken instead (the second case will not happen in stage 1 of SimplyHTML).

A new CSSWriter object is created with that style sheet and the previously created Writer. The style sheet is written to file by calling CSSWriter's write method. Once done, OutputStreamWriter and OutputStream are closed.

Method getStyleSheetName

A style sheet is saved at the location pointed to by the style sheet link reference of its associated document. The style sheet link reference usually is a relative expression containing the file name of the style sheet and an optional path which usually is a relative path.

Method getStyleName returns the path and name of the style sheet by combining the document base (the path the document actually is stored at) and the (possibly relative) path and name of the style sheet reference.

First path and name of the style sheet as contained in the document's style sheet reference link is taken by calling method getStyleRef (see below). Then the document base is read with method getBase of class HTMLDocument.

If a style sheet reference link is not found in the document, the default style sheet name is taken from class SHTMLEditorKit. Finally a relative path possibly contained in the style sheet reference is resolved by method resolveRelativePath and the resulting name is returned.

Method getStyleRef

In method getStyleRef , the first occurrence of a <link> tag is searched in the element tree of the document with the help of method findElement. If a <link> tag is found, the value object of its href attribute is copied to the local string variable linkName.

If no <link> tag is found or it does not contain a href attribute, null is returned.

There is no implementation for the case that there is more than one link reference to style sheets.