|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CSSWriter.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* SimplyHTML, a word processor based on Java, HTML and CSS
|
|
3 |
* Copyright (C) 2002 Ulrich Hilger
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU General Public License
|
|
7 |
* as published by the Free Software Foundation; either version 2
|
|
8 |
* of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
|
|
20 |
import java.io.Writer;
|
|
21 |
import java.io.IOException;
|
|
22 |
import javax.swing.text.html.StyleSheet;
|
|
23 |
import javax.swing.text.Style;
|
|
24 |
import javax.swing.text.StyleConstants;
|
|
25 |
import javax.swing.text.StyleContext;
|
|
26 |
import javax.swing.text.AttributeSet;
|
|
27 |
import java.util.Enumeration;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* A writer for creating a cascading style sheet (CSS) file
|
|
31 |
* from a <code>StyleSheet</code>.
|
|
32 |
*
|
|
33 |
* @author Ulrich Hilger
|
|
34 |
* @author Light Development
|
|
35 |
* @author <a href="http://www.lightdev.com">http://www.lightdev.com</a>
|
|
36 |
* @author <a href="mailto:info@lightdev.com">info@lightdev.com</a>
|
|
37 |
* @author published under the terms and conditions of the
|
|
38 |
* GNU General Public License,
|
|
39 |
* for details see file gpl.txt in the distribution
|
|
40 |
* package of this software
|
|
41 |
*
|
|
42 |
* @version stage 11, April 27, 2003
|
|
43 |
*/
|
|
44 |
|
|
45 |
public class CSSWriter { |
|
46 |
|
|
47 |
/** spaces for indent */
|
|
48 |
private char[] indentChars; |
|
49 |
|
|
50 |
/** new line character sequence */
|
|
51 |
private String newLine = System.getProperty("line.separator"); |
|
52 |
|
|
53 |
/** the writer to write to */
|
|
54 |
private Writer w;
|
|
55 |
|
|
56 |
/** the style sheet to write */
|
|
57 |
private StyleSheet s;
|
|
58 |
|
|
59 |
/** indent length */
|
|
60 |
private int indentLen; |
|
61 |
|
|
62 |
/**
|
|
63 |
* construct a new CSSWriter
|
|
64 |
*
|
|
65 |
* @param writer the writer to write to
|
|
66 |
* @param styles the StyleSheet to write
|
|
67 |
*/
|
|
68 | 0 |
public CSSWriter(Writer writer, StyleSheet styles) {
|
69 | 0 |
this.w = writer;
|
70 | 0 |
this.s = styles;
|
71 |
} |
|
72 |
|
|
73 |
/** write the style sheet to the given writer */
|
|
74 | 0 |
public void write() throws IOException { |
75 | 0 |
Enumeration rules = s.getStyleNames(); |
76 | 0 |
while(rules.hasMoreElements()) {
|
77 | 0 |
writeRule((String) rules.nextElement()); |
78 | 0 |
try {
|
79 | 0 |
Thread.currentThread().sleep(0, 1); |
80 |
} catch(Exception e) { }
|
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
/**
|
|
85 |
* write out a rule with a given name
|
|
86 |
*
|
|
87 |
* <p>Takes the style with the given name from the style sheet passed in the
|
|
88 |
* constructor and writes it to the writer passed in the constructor.</p>.
|
|
89 |
*
|
|
90 |
* @param ruleName the name of the rule to write out
|
|
91 |
*
|
|
92 |
* @exception IOException if i/o fails
|
|
93 |
*/
|
|
94 | 0 |
public void writeRule(String ruleName) throws IOException { |
95 | 0 |
writeRule(ruleName, s.getStyle(ruleName)); |
96 |
} |
|
97 |
|
|
98 |
/**
|
|
99 |
* write out a rule with a given name and style
|
|
100 |
*
|
|
101 |
* <p>Takes the style passed in paramter 'rule' and writes it under
|
|
102 |
* the given name to the writer passed in the constructor.</p>.
|
|
103 |
|
|
104 |
* @param ruleName the name of the rule to write out
|
|
105 |
* @apram rule the style to write out
|
|
106 |
*
|
|
107 |
* @exception IOException if i/o fails
|
|
108 |
*/
|
|
109 | 0 |
public void writeRule(String ruleName, AttributeSet rule) throws IOException { |
110 |
//System.out.println("CSSWriter writeRule ruleName=" + ruleName);
|
|
111 | 0 |
indentLen = ruleName.length() + 3; |
112 | 0 |
if(!ruleName.equalsIgnoreCase(StyleContext.DEFAULT_STYLE)) {
|
113 | 0 |
w.write(ruleName); |
114 | 0 |
w.write(" { ");
|
115 | 0 |
writeStyle(rule); |
116 | 0 |
w.write(newLine); |
117 |
} |
|
118 |
} |
|
119 |
|
|
120 |
/**
|
|
121 |
* write a given style
|
|
122 |
*
|
|
123 |
* <p>A style is an AttributeSet which can have other
|
|
124 |
* AttributeSets in the value field of one of its Attributes.
|
|
125 |
* Therefore this is recursively called whenever an Attribute
|
|
126 |
* contains another AttributeSet.</p>
|
|
127 |
*
|
|
128 |
* @param style the <code>Style</code> to write
|
|
129 |
*
|
|
130 |
* @return true, if the style was closed in this run of recursion,
|
|
131 |
* false if not
|
|
132 |
*/
|
|
133 | 0 |
public boolean writeStyle(AttributeSet style) throws IOException { |
134 | 0 |
boolean closed = false; |
135 | 0 |
Enumeration names = style.getAttributeNames(); |
136 | 0 |
Object value; |
137 | 0 |
Object key; |
138 | 0 |
int count = 0;
|
139 | 0 |
while(names.hasMoreElements()) {
|
140 | 0 |
key = names.nextElement(); |
141 | 0 |
value = style.getAttribute(key); |
142 | 0 |
if( (!key.equals(StyleConstants.NameAttribute)) &&
|
143 |
(!key.equals(StyleConstants.ResolveAttribute)) ) |
|
144 |
{ |
|
145 | 0 |
if(count > 0) {
|
146 | 0 |
w.write(newLine); |
147 | 0 |
indent(indentLen); |
148 |
} |
|
149 |
else {
|
|
150 | 0 |
count++; |
151 |
} |
|
152 | 0 |
w.write(key.toString()); |
153 | 0 |
w.write(":");
|
154 | 0 |
w.write(value.toString()); |
155 | 0 |
w.write(";");
|
156 |
} |
|
157 |
else {
|
|
158 | 0 |
if(key.equals(StyleConstants.ResolveAttribute)) {
|
159 | 0 |
closed = writeStyle((Style) value); |
160 |
} |
|
161 |
} |
|
162 |
} |
|
163 | 0 |
if(!closed) {
|
164 | 0 |
w.write(" }");
|
165 | 0 |
w.write(newLine); |
166 | 0 |
closed = true;
|
167 |
} |
|
168 | 0 |
return closed;
|
169 |
} |
|
170 |
|
|
171 |
/**
|
|
172 |
* indent by a given number of characters
|
|
173 |
*
|
|
174 |
* @param len the number of characters to indent
|
|
175 |
*/
|
|
176 | 0 |
private void indent(int len) throws IOException { |
177 | 0 |
if(indentChars == null || len > indentChars.length) { |
178 | 0 |
indentChars = new char[len]; |
179 | 0 |
for (int i = 0; i < len; i++) { |
180 | 0 |
indentChars[i] = ' '; |
181 |
} |
|
182 |
} |
|
183 | 0 |
w.write(indentChars, 0, len); |
184 |
} |
|
185 |
} |
|