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: * @author Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.awt.Rectangle;
023: import java.io.StringReader;
024:
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.event.DocumentEvent;
027: import javax.swing.text.AttributeSet;
028: import javax.swing.text.DefaultStyledDocument;
029: import javax.swing.text.Document;
030: import javax.swing.text.Element;
031: import javax.swing.text.MutableAttributeSet;
032: import javax.swing.text.SimpleAttributeSet;
033: import javax.swing.text.StyledDocument;
034: import javax.swing.text.View;
035: import javax.swing.text.AbstractDocument.AbstractElement;
036: import javax.swing.text.AbstractDocument.BranchElement;
037: import javax.swing.text.StyleContext.NamedStyle;
038: import javax.swing.text.html.HTMLDocument.BlockElement;
039: import javax.swing.text.html.HTMLDocument.RunElement;
040:
041: public class StyleSheet_TranslateHTMLToCSS extends BasicSwingTestCase {
042: private static class TestDocument extends HTMLDocument {
043: public TestDocument() {
044: super ();
045: }
046:
047: public TestDocument(final StyleSheet ss) {
048: super (ss);
049: }
050:
051: public void lockWrite() {
052: writeLock();
053: }
054:
055: public void unlockWrite() {
056: writeUnlock();
057: }
058: }
059:
060: private TestDocument doc;
061: private StyleSheet ss;
062: private AttributeSet attr;
063:
064: protected void setUp() throws Exception {
065: super .setUp();
066: setIgnoreNotImplemented(true);
067: doc = new TestDocument();
068: doc.insertString(0, "normal test text", null);
069: ss = new StyleSheet();
070:
071: doc.lockWrite();
072: }
073:
074: protected void tearDown() throws Exception {
075: super .tearDown();
076:
077: doc.unlockWrite();
078: }
079:
080: public void testTranslateHTMLToCSSBody() throws Exception {
081: AbstractElement body = (AbstractElement) doc
082: .getDefaultRootElement().getElement(0);
083: assertEquals("body", body.getName());
084: assertTrue(body instanceof BlockElement);
085:
086: body.addAttribute(HTML.Attribute.BGCOLOR, "#ffffff");
087: body.addAttribute(HTML.Attribute.BACKGROUND, "bg.jpg");
088: body.addAttribute(HTML.Attribute.TEXT, "black");
089: body.addAttribute(HTML.Attribute.LINK, "blue");
090: body.addAttribute(HTML.Attribute.ALINK, "red");
091: body.addAttribute(HTML.Attribute.VLINK, "purple");
092: attr = ss.translateHTMLToCSS(body);
093: assertSame(NamedStyle.class, attr.getClass());
094: assertNull(((NamedStyle) attr).getName());
095: assertEquals(3, attr.getAttributeCount());
096: assertEquals(isHarmony() ? "url(bg.jpg)" : "bg.jpg", attr
097: .getAttribute(CSS.Attribute.BACKGROUND_IMAGE)
098: .toString());
099: assertEquals("#ffffff", attr.getAttribute(
100: CSS.Attribute.BACKGROUND_COLOR).toString());
101: assertEquals("black", attr.getAttribute(CSS.Attribute.COLOR)
102: .toString());
103: }
104:
105: public void testTranslateHTMLToCSSP() throws Exception {
106: AbstractElement p = (AbstractElement) doc
107: .getDefaultRootElement().getElement(0).getElement(0);
108: assertEquals("p", p.getName());
109: assertTrue(p instanceof BlockElement);
110:
111: p.addAttribute(HTML.Attribute.BGCOLOR, "#ffffff");
112: p.addAttribute(HTML.Attribute.BACKGROUND, "bg.jpg");
113: p.addAttribute(HTML.Attribute.TEXT, "black");
114: p.addAttribute(HTML.Attribute.LINK, "blue");
115: p.addAttribute(HTML.Attribute.ALINK, "red");
116: p.addAttribute(HTML.Attribute.VLINK, "purple");
117:
118: attr = ss.translateHTMLToCSS(p);
119: assertSame(NamedStyle.class, attr.getClass());
120: assertNull(((NamedStyle) attr).getName());
121:
122: assertEquals(4, attr.getAttributeCount());
123: assertEquals(isHarmony() ? "url(bg.jpg)" : "bg.jpg", attr
124: .getAttribute(CSS.Attribute.BACKGROUND_IMAGE)
125: .toString());
126: assertEquals("#ffffff", attr.getAttribute(
127: CSS.Attribute.BACKGROUND_COLOR).toString());
128: assertEquals("black", attr.getAttribute(CSS.Attribute.COLOR)
129: .toString());
130:
131: assertEquals("0", attr.getAttribute(CSS.Attribute.MARGIN_TOP)
132: .toString());
133: }
134:
135: public void testTranslateHTMLToCSSPContent() throws Exception {
136: AbstractElement content = (AbstractElement) doc
137: .getDefaultRootElement().getElement(0).getElement(0)
138: .getElement(0);
139: assertEquals("content", content.getName());
140: assertTrue(content instanceof RunElement);
141:
142: content.addAttribute(HTML.Attribute.BGCOLOR, "#ffffff");
143: content.addAttribute(HTML.Attribute.BACKGROUND, "bg.jpg");
144: content.addAttribute(HTML.Attribute.TEXT, "black");
145: content.addAttribute(HTML.Attribute.LINK, "blue");
146: content.addAttribute(HTML.Attribute.ALINK, "red");
147: content.addAttribute(HTML.Attribute.VLINK, "purple");
148:
149: attr = ss.translateHTMLToCSS(content);
150: assertSame(NamedStyle.class, attr.getClass());
151: assertNull(((NamedStyle) attr).getName());
152: if (isHarmony()) {
153: assertEquals("url(bg.jpg)", attr.getAttribute(
154: CSS.Attribute.BACKGROUND_IMAGE).toString());
155: assertEquals("#ffffff", attr.getAttribute(
156: CSS.Attribute.BACKGROUND_COLOR).toString());
157: assertEquals("black", attr
158: .getAttribute(CSS.Attribute.COLOR).toString());
159: } else {
160: assertEquals(0, attr.getAttributeCount());
161: }
162: }
163:
164: public void testTranslateHTMLToCSSStyledDocument() throws Exception {
165: StyledDocument doc = new DefaultStyledDocument();
166: doc.insertString(0, "line1\nline2", null);
167:
168: MutableAttributeSet mas = new SimpleAttributeSet();
169: mas.addAttribute(HTML.Attribute.BGCOLOR, "#ffffff");
170: mas.addAttribute(HTML.Attribute.TEXT, "black");
171: doc.setParagraphAttributes(0, 1, mas, false);
172:
173: AbstractElement branch = (AbstractElement) doc
174: .getDefaultRootElement().getElement(0);
175: assertEquals("paragraph", branch.getName());
176: assertTrue(branch instanceof BranchElement);
177: assertSame(BranchElement.class, branch.getClass());
178:
179: attr = ss.translateHTMLToCSS(branch);
180: assertSame(NamedStyle.class, attr.getClass());
181: assertNull(((NamedStyle) attr).getName());
182:
183: assertEquals(2, attr.getAttributeCount());
184: assertEquals("#ffffff", attr.getAttribute(
185: CSS.Attribute.BACKGROUND_COLOR).toString());
186: assertEquals("black", attr.getAttribute(CSS.Attribute.COLOR)
187: .toString());
188: }
189:
190: public void testTranslateHTMLToCSSA() throws Exception {
191: doc.remove(0, doc.getLength());
192: HTMLEditorKit kit = new HTMLEditorKit();
193: kit.read(new StringReader("<a href=\"http://go\">link</a>"),
194: doc, 0);
195:
196: AbstractElement body = (AbstractElement) doc
197: .getDefaultRootElement().getElement(1);
198: assertEquals("body", body.getName());
199:
200: body.addAttribute(HTML.Attribute.BGCOLOR, "#ffffff");
201: body.addAttribute(HTML.Attribute.BACKGROUND, "bg.jpg");
202: body.addAttribute(HTML.Attribute.TEXT, "black");
203: body.addAttribute(HTML.Attribute.LINK, "blue");
204: body.addAttribute(HTML.Attribute.ALINK, "red");
205: body.addAttribute(HTML.Attribute.VLINK, "purple");
206:
207: AbstractElement a = (AbstractElement) doc
208: .getCharacterElement(2);
209: assertNotNull(a.getAttribute(HTML.Tag.A));
210: attr = ss.translateHTMLToCSS(a);
211: assertNull(((NamedStyle) attr).getName());
212: assertEquals(0, attr.getAttributeCount());
213: }
214:
215: public void testTranslateHTMLToCSSAlignLeft() throws Exception {
216: AbstractElement branch = (AbstractElement) doc
217: .getDefaultRootElement().getElement(0);
218: assertEquals("body", branch.getName());
219:
220: branch.addAttribute(HTML.Attribute.ALIGN, "left");
221: attr = ss.translateHTMLToCSS(branch);
222: assertEquals(1, attr.getAttributeCount());
223: assertEquals("left", attr
224: .getAttribute(CSS.Attribute.TEXT_ALIGN).toString());
225: }
226:
227: public void testTranslateHTMLToCSSAlignCenter() throws Exception {
228: AbstractElement branch = (AbstractElement) doc
229: .getDefaultRootElement().getElement(0);
230: assertEquals("body", branch.getName());
231:
232: branch.addAttribute(HTML.Attribute.ALIGN, "center");
233: attr = ss.translateHTMLToCSS(branch);
234: assertEquals(1, attr.getAttributeCount());
235: assertEquals("center", attr.getAttribute(
236: CSS.Attribute.TEXT_ALIGN).toString());
237: }
238:
239: public void testTranslateHTMLToCSSAlignRight() throws Exception {
240: AbstractElement branch = (AbstractElement) doc
241: .getDefaultRootElement().getElement(0);
242: assertEquals("body", branch.getName());
243:
244: branch.addAttribute(HTML.Attribute.ALIGN, "right");
245: attr = ss.translateHTMLToCSS(branch);
246: assertEquals(1, attr.getAttributeCount());
247: assertEquals("right", attr.getAttribute(
248: CSS.Attribute.TEXT_ALIGN).toString());
249: }
250:
251: public void testTranslateHTMLToCSSAlignJustify() throws Exception {
252: AbstractElement branch = (AbstractElement) doc
253: .getDefaultRootElement().getElement(0);
254: assertEquals("body", branch.getName());
255:
256: branch.addAttribute(HTML.Attribute.ALIGN, "justify");
257: attr = ss.translateHTMLToCSS(branch);
258: assertEquals(1, attr.getAttributeCount());
259: assertEquals("justify", attr.getAttribute(
260: CSS.Attribute.TEXT_ALIGN).toString());
261: }
262:
263: public void testTranslateHTMLToCSSAlignTop() throws Exception {
264: AbstractElement branch = (AbstractElement) doc
265: .getDefaultRootElement().getElement(0);
266: assertEquals("body", branch.getName());
267:
268: branch.addAttribute(HTML.Attribute.ALIGN, "top");
269: attr = ss.translateHTMLToCSS(branch);
270: if (isHarmony()) {
271: assertEquals(0, attr.getAttributeCount());
272: assertNull(attr.getAttribute(CSS.Attribute.TEXT_ALIGN));
273: } else {
274: assertEquals(1, attr.getAttributeCount());
275: assertEquals("top", attr.getAttribute(
276: CSS.Attribute.TEXT_ALIGN).toString());
277: }
278: }
279:
280: public void testTranslateHTMLToCSSAlignBottom() throws Exception {
281: AbstractElement branch = (AbstractElement) doc
282: .getDefaultRootElement().getElement(0);
283: assertEquals("body", branch.getName());
284:
285: branch.addAttribute(HTML.Attribute.ALIGN, "bottom");
286: attr = ss.translateHTMLToCSS(branch);
287: if (isHarmony()) {
288: assertEquals(0, attr.getAttributeCount());
289: assertNull(attr.getAttribute(CSS.Attribute.TEXT_ALIGN));
290: } else {
291: assertEquals(1, attr.getAttributeCount());
292: assertEquals("bottom", attr.getAttribute(
293: CSS.Attribute.TEXT_ALIGN).toString());
294: }
295: }
296:
297: public void testTranslateHTMLToCSSAlignChar() throws Exception {
298: AbstractElement branch = (AbstractElement) doc
299: .getDefaultRootElement().getElement(0);
300: assertEquals("body", branch.getName());
301:
302: branch.addAttribute(HTML.Attribute.ALIGN, "char");
303: attr = ss.translateHTMLToCSS(branch);
304: if (isHarmony()) {
305: assertEquals(0, attr.getAttributeCount());
306: assertNull(attr.getAttribute(CSS.Attribute.TEXT_ALIGN));
307: } else {
308: assertEquals(1, attr.getAttributeCount());
309: assertEquals("char", attr.getAttribute(
310: CSS.Attribute.TEXT_ALIGN).toString());
311: }
312: }
313:
314: public void testTranslateHTMLToCSSVAlignTop() throws Exception {
315: AbstractElement branch = (AbstractElement) doc
316: .getDefaultRootElement().getElement(0);
317: assertEquals("body", branch.getName());
318:
319: branch.addAttribute(HTML.Attribute.VALIGN, "top");
320: attr = ss.translateHTMLToCSS(branch);
321: assertEquals(1, attr.getAttributeCount());
322: assertEquals("top", attr.getAttribute(
323: CSS.Attribute.VERTICAL_ALIGN).toString());
324: }
325:
326: public void testTranslateHTMLToCSSVAlignMiddle() throws Exception {
327: AbstractElement branch = (AbstractElement) doc
328: .getDefaultRootElement().getElement(0);
329: assertEquals("body", branch.getName());
330:
331: branch.addAttribute(HTML.Attribute.VALIGN, "middle");
332: attr = ss.translateHTMLToCSS(branch);
333: assertEquals(1, attr.getAttributeCount());
334: assertEquals("middle", attr.getAttribute(
335: CSS.Attribute.VERTICAL_ALIGN).toString());
336: }
337:
338: public void testTranslateHTMLToCSSVAlignBottom() throws Exception {
339: AbstractElement branch = (AbstractElement) doc
340: .getDefaultRootElement().getElement(0);
341: assertEquals("body", branch.getName());
342:
343: branch.addAttribute(HTML.Attribute.VALIGN, "bottom");
344: attr = ss.translateHTMLToCSS(branch);
345: assertEquals(1, attr.getAttributeCount());
346: assertEquals("bottom", attr.getAttribute(
347: CSS.Attribute.VERTICAL_ALIGN).toString());
348: }
349:
350: public void testTranslateHTMLToCSSVAlignBaseline() throws Exception {
351: AbstractElement branch = (AbstractElement) doc
352: .getDefaultRootElement().getElement(0);
353: assertEquals("body", branch.getName());
354:
355: branch.addAttribute(HTML.Attribute.VALIGN, "baseline");
356: attr = ss.translateHTMLToCSS(branch);
357: assertEquals(1, attr.getAttributeCount());
358: assertEquals("baseline", attr.getAttribute(
359: CSS.Attribute.VERTICAL_ALIGN).toString());
360: }
361:
362: public void testTranslateHTMLToCSSWidth() throws Exception {
363: AbstractElement branch = (AbstractElement) doc
364: .getParagraphElement(0);
365: assertEquals("p", branch.getName());
366:
367: branch.addAttribute(HTML.Attribute.WIDTH, "50%");
368: attr = ss.translateHTMLToCSS(branch);
369: assertEquals(2, attr.getAttributeCount());
370: assertEquals("50%", attr.getAttribute(CSS.Attribute.WIDTH)
371: .toString());
372: assertEquals("0", attr.getAttribute(CSS.Attribute.MARGIN_TOP)
373: .toString());
374: }
375:
376: public void testTranslateHTMLToCSSHeight() throws Exception {
377: AbstractElement branch = (AbstractElement) doc
378: .getParagraphElement(0);
379: assertEquals("p", branch.getName());
380:
381: branch.addAttribute(HTML.Attribute.HEIGHT, "331");
382: attr = ss.translateHTMLToCSS(branch);
383: assertEquals(2, attr.getAttributeCount());
384: assertEquals("331" + (isHarmony() ? "pt" : ""), attr
385: .getAttribute(CSS.Attribute.HEIGHT).toString());
386: assertEquals("0", attr.getAttribute(CSS.Attribute.MARGIN_TOP)
387: .toString());
388: }
389:
390: public void testTranslateHTMLToCSSStyle() throws Exception {
391: AbstractElement branch = (AbstractElement) doc
392: .getParagraphElement(0);
393: assertEquals("p", branch.getName());
394:
395: branch.addAttribute(CSS.Attribute.BORDER_BOTTOM_WIDTH, "1pt");
396: attr = ss.translateHTMLToCSS(branch);
397: assertEquals(2, attr.getAttributeCount());
398: assertEquals("1pt", attr.getAttribute(
399: CSS.Attribute.BORDER_BOTTOM_WIDTH).toString());
400: assertEquals("0", attr.getAttribute(CSS.Attribute.MARGIN_TOP)
401: .toString());
402: }
403:
404: public void testTranslateHTMLToCSSViewAttributes() throws Exception {
405: final Marker gva = new Marker(true);
406: final Marker th2c = new Marker(true);
407: ss = new StyleSheet() {
408: public AttributeSet getViewAttributes(View v) {
409: gva.setOccurred();
410: return super .getViewAttributes(v);
411: }
412:
413: public AttributeSet translateHTMLToCSS(
414: AttributeSet htmlAttrSet) {
415: th2c.setOccurred();
416: return super .translateHTMLToCSS(htmlAttrSet);
417: }
418: };
419: doc = new TestDocument(ss);
420: doc.lockWrite();
421: final AbstractElement branch = (AbstractElement) doc
422: .getParagraphElement(0);
423: assertEquals("p", branch.getName());
424:
425: assertFalse(gva.isOccurred());
426: assertFalse(th2c.isOccurred());
427: final View view = new InlineView(branch);
428: assertTrue(gva.isOccurred());
429: assertTrue(th2c.isOccurred());
430:
431: final AttributeSet va = view.getAttributes();
432: assertFalse(gva.isOccurred());
433: assertFalse(th2c.isOccurred());
434:
435: assertSame(va, view.getAttributes());
436: assertEquals(1, va.getAttributeCount());
437: assertEquals("0", va.getAttribute(CSS.Attribute.MARGIN_TOP)
438: .toString());
439:
440: branch.addAttribute(HTML.Attribute.WIDTH, "200");
441:
442: assertFalse(gva.isOccurred());
443: assertFalse(th2c.isOccurred());
444:
445: view.changedUpdate(new DocumentEvent() {
446: public int getOffset() {
447: return branch.getStartOffset();
448: }
449:
450: public int getLength() {
451: return branch.getEndOffset() - branch.getStartOffset();
452: }
453:
454: public Document getDocument() {
455: return doc;
456: }
457:
458: public EventType getType() {
459: return EventType.CHANGE;
460: }
461:
462: public ElementChange getChange(final Element elem) {
463: return null;
464: }
465: }, new Rectangle(), null);
466: assertTrue(gva.isOccurred());
467: assertTrue(th2c.isOccurred());
468:
469: final AttributeSet mva = view.getAttributes();
470: assertFalse(gva.isOccurred());
471: assertFalse(th2c.isOccurred());
472:
473: assertNotSame(va, mva);
474: assertSame(mva, view.getAttributes());
475: assertEquals(2, mva.getAttributeCount());
476: }
477: }
|