001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Vadim L. Bogdanov
020: * @version $Revision$
021: */package javax.swing.text.html;
022:
023: import java.awt.Color;
024: import java.awt.Component;
025: import java.awt.Graphics;
026: import java.io.IOException;
027: import java.io.StringWriter;
028: import java.io.Writer;
029:
030: import javax.swing.Icon;
031: import javax.swing.JLabel;
032: import javax.swing.SwingTestCase;
033: import javax.swing.text.AbstractDocument;
034: import javax.swing.text.AttributeSet;
035: import javax.swing.text.BadLocationException;
036: import javax.swing.text.DefaultStyledDocument;
037: import javax.swing.text.Element;
038: import javax.swing.text.MutableAttributeSet;
039: import javax.swing.text.SimpleAttributeSet;
040: import javax.swing.text.Style;
041: import javax.swing.text.StyleConstants;
042: import javax.swing.text.StyledDocument;
043:
044: public class MinimalHTMLWriterTest extends SwingTestCase {
045: private static class TestMinimalHTMLWriter extends
046: MinimalHTMLWriter {
047: public TestMinimalHTMLWriter(final Writer w,
048: final StyledDocument doc) {
049: super (w, doc);
050: }
051:
052: public TestMinimalHTMLWriter(final Writer w,
053: final StyledDocument doc, final int pos, final int len) {
054: super (w, doc, pos, len);
055: }
056:
057: protected void incrIndent() {
058: super .incrIndent();
059: }
060:
061: protected int getIndentLevel() {
062: return super .getIndentLevel();
063: }
064:
065: protected int getLineLength() {
066: return super .getLineLength();
067: }
068: }
069:
070: private StyledDocument doc;
071: private TestMinimalHTMLWriter writer;
072: private Writer out;
073: private Element iconElement;
074: private Element componentElement;
075:
076: public MinimalHTMLWriterTest(final String name) {
077: super (name);
078: }
079:
080: protected void setUp() throws Exception {
081: super .setUp();
082:
083: createDocument();
084: out = new StringWriter();
085: createWriter();
086: }
087:
088: protected void tearDown() throws Exception {
089: super .tearDown();
090: }
091:
092: public void testText() throws Exception {
093: Element par = doc.getParagraphElement(0);
094: Element text = par.getElement(par.getElementCount() - 1);
095: writer.text(text);
096: assertEquals("green text", out.toString());
097: }
098:
099: public void testWriteAttributes() throws Exception {
100: MutableAttributeSet attrs = new SimpleAttributeSet();
101: StyleConstants.setItalic(attrs, true);
102: StyleConstants.setFontFamily(attrs, "serif");
103:
104: writer.writeAttributes(attrs);
105: if (isHarmony()) {
106: assertTrue(" font-style: italic;~ font-family: serif;~"
107: .equals(out.toString())
108: ^ " font-family: serif;~ font-style: italic;~"
109: .equals(out.toString()));
110: } else {
111: assertTrue(" family:serif;~ italic:italic;~".equals(out
112: .toString())
113: ^ " italic:italic;~ family:serif;~".equals(out
114: .toString()));
115: }
116: }
117:
118: public void testWrite() throws Exception {
119: writer = new TestMinimalHTMLWriter(out, doc) {
120: protected void writeHeader() throws IOException {
121: write("_header_");
122: }
123:
124: protected void writeBody() throws IOException,
125: BadLocationException {
126: write("_body");
127: }
128: };
129: setupWriter();
130:
131: writer.write();
132: assertEquals(" <html>~_header__body </html>~", out.toString());
133: }
134:
135: public void testMinimalHTMLWriterWriterStyledDocumentIntInt() {
136: assertEquals(100, writer.getLineLength());
137: assertFalse(writer.inFontTag());
138: }
139:
140: public void testMinimalHTMLWriterWriterStyledDocument() {
141: assertEquals(100, writer.getLineLength());
142: assertFalse(writer.inFontTag());
143: }
144:
145: public void testInFontTag() throws Exception {
146: assertFalse(writer.inFontTag());
147:
148: MutableAttributeSet attrs = new SimpleAttributeSet();
149: StyleConstants.setForeground(attrs, Color.GREEN);
150: writer.writeNonHTMLAttributes(attrs);
151: assertTrue(writer.inFontTag());
152: }
153:
154: public void testIsText() {
155: assertFalse(writer.isText(doc.getParagraphElement(0)));
156: assertTrue(writer.isText(doc.getCharacterElement(0)));
157: assertFalse(writer.isText(iconElement));
158: }
159:
160: public void testStartFontTag() throws Exception {
161: assertFalse(writer.inFontTag());
162: int indentLevel = writer.getIndentLevel();
163: writer.startFontTag("italic");
164: assertFalse(writer.inFontTag());
165: assertEquals(indentLevel + 1, writer.getIndentLevel());
166: writer.startFontTag("bold");
167: assertEquals(
168: " <font style=\"italic\">~ <font style=\"bold\">~",
169: out.toString());
170: }
171:
172: public void testEndFontTag() throws Exception {
173: int indentLevel = writer.getIndentLevel();
174: writer.startFontTag("italic");
175: writer.endFontTag();
176: assertEquals(indentLevel, writer.getIndentLevel());
177: assertEquals(" <font style=\"italic\">~~ </font>~", out
178: .toString());
179: }
180:
181: public void testWriteBody() throws Exception {
182: writer = new TestMinimalHTMLWriter(out, doc) {
183: protected void writeContent(final Element elem,
184: final boolean needsIndenting) throws IOException,
185: BadLocationException {
186:
187: if (needsIndenting) {
188: indent();
189: }
190: write("_content_");
191: }
192:
193: protected void writeLeaf(final Element elem)
194: throws IOException {
195: write("_leaf_");
196: }
197: };
198: setupWriter();
199:
200: writer.writeBody();
201: assertEquals(" <body>~ <p class=default>~"
202: + " _content__content__content_~ </p>~"
203: + " <p class=myStylea>~ _content__leaf__leaf_~"
204: + " </p>~ </body>~", out.toString());
205:
206: out = new StringWriter();
207: Element e = doc.getParagraphElement(0).getElement(1);
208: writer = new TestMinimalHTMLWriter(out, doc,
209: e.getStartOffset(), e.getEndOffset()
210: - e.getStartOffset());
211: setupWriter();
212: writer.writeBody();
213: assertEquals(
214: " <body>~ <p class=default>~ <i>italic text</i>~"
215: + " </p>~ </body>~", out.toString());
216: }
217:
218: public void testWriteContent() throws Exception {
219: writer.writeContent(doc.getCharacterElement(0), true);
220: writer.writeContent(doc.getCharacterElement(0), true);
221: assertEquals(" <b>bold text bold text ", out.toString());
222:
223: out = new StringWriter();
224: createWriter();
225: writer.writeContent(doc.getCharacterElement(0), false);
226: writer.writeContent(
227: doc.getParagraphElement(doc.getLength() - 1)
228: .getElement(0), false);
229: assertEquals(
230: "<b>bold text </b> <span style=\"color: #00ff00\">~"
231: + " more green text", out.toString());
232: }
233:
234: public void testWriteHTMLTags() throws Exception {
235: MutableAttributeSet attrs = new SimpleAttributeSet();
236: StyleConstants.setItalic(attrs, true);
237: writer.writeHTMLTags(attrs);
238: assertEquals("<i>", out.toString());
239:
240: StyleConstants.setBold(attrs, true);
241: writer.writeHTMLTags(attrs);
242: assertEquals("<i><b>", out.toString());
243:
244: attrs = new SimpleAttributeSet();
245: StyleConstants.setUnderline(attrs, true);
246: StyleConstants.setStrikeThrough(attrs, true);
247: writer.writeHTMLTags(attrs);
248: assertEquals("<i><b><u>", out.toString());
249: }
250:
251: public void testWriteHeader() throws Exception {
252: writer = new TestMinimalHTMLWriter(out, doc) {
253: protected void writeStyles() throws IOException {
254: indent();
255: write("...styles...");
256: writeLineSeparator();
257: }
258: };
259: setupWriter();
260:
261: writer.writeHeader();
262: String result = out.toString();
263: if (!isHarmony()) {
264: result = result.replaceFirst("<style>",
265: "<style type=\"text/css\">");
266: }
267: assertEquals(" <head>~ <style type=\"text/css\">~"
268: + " <!--~ ...styles...~"
269: + " -->~ </style>~ </head>~", result);
270: }
271:
272: public void testWriteImage() throws Exception {
273: writer.incrIndent();
274: writer.writeLeaf(iconElement);
275: assertEquals(" ", out.toString());
276:
277: writer.writeLeaf(doc.getCharacterElement(0));
278: assertEquals(" ", out.toString());
279: }
280:
281: public void testWriteComponent() throws Exception {
282: writer.incrIndent();
283: writer.writeLeaf(componentElement);
284: assertEquals(" ", out.toString());
285:
286: writer.writeLeaf(doc.getCharacterElement(0));
287: assertEquals(" ", out.toString());
288: }
289:
290: public void testWriteLeaf() throws Exception {
291: writer = new TestMinimalHTMLWriter(out, doc) {
292: protected void writeImage(final Element elem)
293: throws IOException {
294: super .writeImage(elem);
295: write("_image_");
296: }
297:
298: protected void writeComponent(final Element elem)
299: throws IOException {
300: super .writeComponent(elem);
301: write("_component_");
302: }
303: };
304: setupWriter();
305:
306: writer.writeLeaf(doc.getCharacterElement(0));
307: assertEquals(" ", out.toString());
308:
309: writer.writeLeaf(iconElement);
310: assertEquals(" _image_", out.toString());
311:
312: writer.writeLeaf(componentElement);
313: assertEquals(" _image_ _component_", out.toString());
314: }
315:
316: public void testWriteNonHTMLAttributes() throws Exception {
317: MutableAttributeSet attrs = new SimpleAttributeSet();
318: StyleConstants.setItalic(attrs, true);
319: StyleConstants.setForeground(attrs, Color.RED);
320: StyleConstants.setLineSpacing(attrs, 1.0f);
321: int indentLevel = writer.getIndentLevel();
322: writer.writeNonHTMLAttributes(attrs);
323:
324: assertEquals(" <span style=\"color: #ff0000\">~", out
325: .toString());
326: assertTrue(writer.inFontTag());
327: assertEquals(indentLevel + 1, writer.getIndentLevel());
328: writer.writeEndParagraph();
329:
330: out = new StringWriter();
331: createWriter();
332: attrs = new SimpleAttributeSet();
333: writer.writeNonHTMLAttributes(attrs);
334: assertEquals("", out.toString());
335: assertFalse(writer.inFontTag());
336: }
337:
338: public void testWriteStartParagraph() throws Exception {
339: Element par = doc.getParagraphElement(0);
340: writer.writeStartParagraph(par);
341: assertEquals(" <p class=default>~", out.toString());
342:
343: out = new StringWriter();
344: createWriter();
345: par = doc.getParagraphElement(doc.getLength() - 1);
346: writer.writeStartParagraph(par);
347: assertEquals(" <p class=myStylea>~", out.toString());
348: }
349:
350: public void testWriteEndParagraph() throws Exception {
351: writer.writeEndParagraph();
352: assertEquals("~</p>~", out.toString());
353:
354: MutableAttributeSet attrs = new SimpleAttributeSet();
355: StyleConstants.setItalic(attrs, true);
356: StyleConstants.setFontFamily(attrs, "serif");
357: writer.incrIndent();
358:
359: writer.writeNonHTMLAttributes(attrs);
360: writer.writeHTMLTags(attrs);
361: writer.writeEndParagraph();
362: assertEquals(
363: "~</p>~ <span style=\"font-family: serif\">~<i></i>"
364: + "~ </span>~</p>~", out.toString());
365: }
366:
367: public void testWriteStartTag() throws Exception {
368: int indentLevel = writer.getIndentLevel();
369: writer.writeStartTag("the_tag");
370: assertEquals(" the_tag~", out.toString());
371: assertEquals(indentLevel + 1, writer.getIndentLevel());
372: }
373:
374: public void testWriteEndTag() throws Exception {
375: int indentLevel = writer.getIndentLevel();
376: writer.writeEndTag("the_tag");
377: assertEquals("the_tag~", out.toString());
378: assertEquals(indentLevel - 1, writer.getIndentLevel());
379: }
380:
381: public void testWriteStyles() throws Exception {
382: writer = new TestMinimalHTMLWriter(out, doc) {
383: protected void writeAttributes(final AttributeSet attr)
384: throws IOException {
385:
386: indent();
387: write("_attributes_");
388: writeLineSeparator();
389: }
390: };
391: setupWriter();
392:
393: writer.writeStyles();
394:
395: String output = out.toString();
396: // order in which styles are written is not important
397: output = output.replaceFirst("p.myStylea", "p.myStyle");
398:
399: assertEquals(" p.myStyle {~ _attributes_~ }~"
400: + " p.myStyle {~ _attributes_~ }~", output);
401: }
402:
403: private void createWriter() {
404: writer = new TestMinimalHTMLWriter(out, doc);
405: setupWriter();
406: }
407:
408: private void setupWriter() {
409: writer.setLineSeparator("~");
410: writer.incrIndent();
411: }
412:
413: private void createDocument() throws Exception {
414: super .setUp();
415: doc = new DefaultStyledDocument();
416: MutableAttributeSet boldStyle = new SimpleAttributeSet();
417: StyleConstants.setBold(boldStyle, true);
418: MutableAttributeSet italicStyle = new SimpleAttributeSet();
419: StyleConstants.setItalic(italicStyle, true);
420: MutableAttributeSet colorStyle = new SimpleAttributeSet();
421: StyleConstants.setForeground(colorStyle, Color.GREEN);
422:
423: Style style = doc.addStyle("myStyle", null);
424: StyleConstants.setBackground(style, Color.RED);
425: style = doc.addStyle("myStylea", null);
426: StyleConstants.setForeground(style, Color.GREEN);
427:
428: doc.insertString(0, "bold text ", boldStyle);
429: doc.insertString(doc.getLength(), "italic text", italicStyle);
430: doc.insertString(doc.getLength(),
431: "green text\n more green text", colorStyle);
432:
433: doc.setLogicalStyle(doc.getLength() - 1, style);
434:
435: MutableAttributeSet attrs = new SimpleAttributeSet();
436: Icon icon = new Icon() {
437: public int getIconHeight() {
438: return 0;
439: }
440:
441: public int getIconWidth() {
442: return 0;
443: }
444:
445: public void paintIcon(final Component c, final Graphics g,
446: final int x, final int y) {
447: }
448: };
449: StyleConstants.setIcon(attrs, icon);
450: attrs.addAttribute(AbstractDocument.ElementNameAttribute,
451: StyleConstants.IconElementName);
452: doc.insertString(doc.getLength(), "ppp", attrs);
453:
454: iconElement = doc.getCharacterElement(doc.getLength() - 1);
455:
456: attrs = new SimpleAttributeSet();
457: StyleConstants.setComponent(attrs, new JLabel("lab1"));
458: attrs.addAttribute(AbstractDocument.ElementNameAttribute,
459: StyleConstants.ComponentElementName);
460: doc.insertString(doc.getLength(), "ccc", attrs);
461: componentElement = doc.getCharacterElement(doc.getLength() - 1);
462: }
463: }
|