KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > view > FormatType


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: FormatType.java,v 1.9 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.view;
21
22 import java.util.*;
23 import org.w3c.dom.*;
24 import org.w3c.dom.html.*;
25
26 /**
27  * <p>This class defines all valid FormatTypes.
28  *
29  * <p>We start by defining a series of basic interfaces to define all known format types
30  * and specify how they relate to one another. Please note that these are hierarchical in
31  * nature. Strongly typed FormatType interfaces include:
32  *
33  * <ul>
34  * <li>GenericData<ul>
35  * <li>AsciiData<ul>
36  * <li>Html<ul>
37  * <li>HtmlStandard<ul>
38  * <li>Html3x<ul>
39  * <li>Html4x</li>
40  * </ul>
41  * </li>
42  * </ul>
43  * </li>
44  * <li>Chtml<ul>
45  * <li>Chtml1x<ul>
46  * <li>Chtml2x</li>
47  * </ul>
48  * </li>
49  * </ul>
50  * </li>
51  * </ul>
52  * </li>
53  * <li>Xml<ul>
54  * <li>Xml1x</li>
55  * <li>Vxml<ul>
56  * <li>Vxml1x</li>
57  * </ul>
58  * </li>
59  * <li>Wml<ul>
60  * <li>Wml1x</li>
61  * </ul>
62  * </li>
63  * <li>Xhtml<ul>
64  * <li>XhtmlBasic<ul>
65  * <li>XhtmlBasic1x<ul>
66  * <li>XhtmlBasic2x</li>
67  * </ul>
68  * </li>
69  * <li>XhtmlStandard<ul>
70  * <li>XhtmlStandard1x<ul>
71  * <li>XhtmlStandard2x</li>
72  * </ul>
73  * </li>
74  * </ul>
75  * </li>
76  * </ul>
77  * </li>
78  * </ul>
79  * </li>
80  * </ul>
81  * </li>
82  * </ul>
83  * </li>
84  * <li>BinaryData<ul>
85  * <li>Rtf</li>
86  * <li>Doc</li>
87  * <li>Pdf</li>
88  * <li>Exe</li>
89  * </ul>
90  * </li>
91  * <li>Unknown<br>
92  * </li>
93  * </ul>
94  * </li>
95  * </ul>
96  *
97  * We can reference these interfaces to determine what kind of FormatType we're
98  * actually dealing with. For instance, say we have a reference to a format type 'ft':
99  *
100  * <p>&nbsp;&nbsp;&nbsp;&nbsp;ft = FormatType.HTML_4_0
101  *
102  * <p>Note here that we assigned the format type value by referring to a
103  * concrete instance of the format type (the idea here is that if you wish
104  * to assign a FormatType to some object you must select a concrete type).
105  *
106  * <p>Concrete format types include:
107  * <ul>
108  * <li>FormatType.HTML_3_0</li>
109  * <li>FormatType.HTML_3_1</li>
110  * <li>FormatType.HTML_3_2</li>
111  * <li>FormatType.HTML_4_0</li>
112  * <li>FormatType.HTML_4_1</li>
113  * <li>FormatType.CHTML_1_0</li>
114  * <li>FormatType.CHTML_2_0</li>
115  * <li>FormatType.XML_1_0</li>
116  * <li>FormatType.VXML_1_0</li>
117  * <li>FormatType.XHTML_BASIC_1_0</li>
118  * <li>FormatType.XHTML_STANDARD_1_0</li>
119  * <li>FormatType.UNKNOWN_FORMAT</li>
120  * </ul>
121  *
122  * <p> Now lets say we want to check if 'ft' is at least compatible with HTML 3.2,
123  * we can use the instanceOf() operator like this:
124  *
125  * <p>&nbsp;&nbsp;&nbsp;&nbsp;if (ft instanceof FormatType.Html3x) {...}
126  *
127  * <p>Since FormatType.HTML_4_0 implements FormatType.Html3x the logic check
128  * succeeds and our 'if' statement is executed.
129  *
130  * <p>In case you are wondering 'Why in the world did we go to the effort to
131  * define all these interfaces and such' the reason is that we wanted to make it
132  * easy to check for "classes" of formats (ie. anything that conforms to HTML 3.2
133  * for instance...an HTML 4.0 browser does). The net effect of this is that you
134  * can write code that targets a baseline and as new versions come out your code
135  * will continue to work.
136  *
137  * <p>The reason we used interfaces is that as code evolves it sometimes supports
138  * more than one format. XHTML is an excellent example of this. There are 2 different
139  * flavors of this: XHTML Basic is a subset of XHTML. What this means is that XHTML_1_x
140  * should be an instance of XHTML_Basic. However, it should also be an instance of
141  * XHTML_Basic_1_x. Since Java does not allow multiple inheritance in concrete classes,
142  * we had to instead use interfaces so that a given format can specify support multiple
143  * formats.
144  */

145 public abstract class FormatType {
146
147     //concrete instances of format types
148
public static final FormatType HTML_3_0 = new Html3_0Impl();
149     public static final FormatType HTML_3_1 = new Html3_1Impl();
150     public static final FormatType HTML_3_2 = new Html3_2Impl();
151     public static final FormatType HTML_4_0 = new Html4_0Impl();
152     public static final FormatType HTML_4_1 = new Html4_1Impl();
153     public static final FormatType CHTML_1_0 = new Chtml1_0Impl();
154     public static final FormatType CHTML_2_0 = new Chtml2_0Impl();
155     public static final FormatType XML_1_0 = new Xml1_0Impl();
156     public static final FormatType VXML_1_0 = new Vxml1_0Impl();
157     public static final FormatType WML_1_0 = new Wml1_0Impl();
158     public static final FormatType XHTML_BASIC_1_0 = new XhtmlBasic1_0Impl();
159     public static final FormatType XHTML_STANDARD_1_0 = new XhtmlStandard1_0Impl();
160     public static final FormatType UNKNOWN_FORMAT = new UnknownFormatImpl();
161
162     //base format types (hierarchical defs)
163
public interface GenericData {};
164     public interface AsciiData extends GenericData {};
165     public interface HtmlBasic extends AsciiData {};
166     public interface Html extends HtmlBasic {};
167     public interface Html3x extends Html {};
168     public interface Html4x extends Html3x {};
169     public interface Chtml extends HtmlBasic {};
170     public interface Chtml1x extends Chtml {};
171     public interface Chtml2x extends Chtml1x {};
172     public interface Xml extends AsciiData {};
173     public interface Xml1x extends Xml {};
174     public interface Vxml extends Xml {};
175     public interface Vxml1x extends Vxml {};
176     public interface Wml extends Xml {};
177     public interface Wml1x extends Wml {};
178     public interface Xhtml extends Xml {};
179     public interface XhtmlBasic extends Xhtml {};
180     public interface XhtmlBasic1x extends XhtmlBasic {};
181     public interface XhtmlBasic2x extends XhtmlBasic1x {};
182     public interface XhtmlStandard extends XhtmlBasic {};
183     public interface XhtmlStandard1x extends XhtmlStandard, XhtmlBasic1x {}; //TODO: doesn't this also need to implement Html4x?
184
public interface XhtmlStandard2x extends XhtmlStandard1x, XhtmlBasic2x {};
185     public interface BinaryData extends GenericData {};
186     public interface Rtf extends BinaryData {};
187     public interface Doc extends BinaryData {};
188     public interface Pdf extends BinaryData {};
189     public interface Exe extends BinaryData {};
190     public interface UnknownFormat extends GenericData {};
191
192
193     //dom class
194
protected Class JavaDoc domCl = Node.class;
195
196     //private implementations (while the concrete implementations above are final
197
//so they can't be changed, the specific implementations are left merely protected,
198
//allowing developers to extend for custom types if needed).
199

200     //format types
201
protected static class GenericDataImpl extends FormatType implements GenericData {
202         public String JavaDoc toString() {
203             String JavaDoc s = this.getClass().getName();
204             int spos = s.indexOf("$");
205             int epos = s.indexOf("Impl");
206             return s.substring(spos+1, epos);
207         }
208     };
209     protected static class AsciiDataImpl extends GenericDataImpl implements AsciiData {};
210     protected static class HtmlBasicImpl extends AsciiDataImpl implements HtmlBasic {
211         //this specifies that all Html data types will be using the HTMLElement DOM
212
//interface rather than the Node interface
213
public HtmlBasicImpl() {domCl = HTMLElement.class;}
214     };
215     protected static class HtmlImpl extends HtmlBasicImpl implements Html {};
216     protected static class Html3xImpl extends HtmlImpl implements Html3x {};
217     protected static class Html3_0Impl extends Html3xImpl {};
218     protected static class Html3_1Impl extends Html3_0Impl {};
219     protected static class Html3_2Impl extends Html3_1Impl {};
220     protected static class Html4xImpl extends Html3xImpl implements Html4x {};
221     protected static class Html4_0Impl extends Html4xImpl {};
222     protected static class Html4_1Impl extends Html4xImpl {};
223     protected static class ChtmlImpl extends HtmlBasicImpl implements Chtml {};
224     protected static class Chtml1xImpl extends ChtmlImpl implements Chtml1x {};
225     protected static class Chtml1_0Impl extends Chtml1xImpl {};
226     protected static class Chtml2xImpl extends Chtml1xImpl implements Chtml2x {};
227     protected static class Chtml2_0Impl extends Chtml2xImpl {};
228     protected static class XmlImpl extends AsciiDataImpl implements Xml {};
229     protected static class Xml1xImpl extends XmlImpl implements Xml1x {};
230     protected static class Xml1_0Impl extends Xml1xImpl {};
231     protected static class VxmlImpl extends XmlImpl implements Vxml {};
232     protected static class Vxml1xImpl extends VxmlImpl implements Vxml1x {};
233     protected static class Vxml1_0Impl extends Vxml1xImpl {};
234     protected static class WmlImpl extends XmlImpl implements Wml {
235 //TODO: we need to specify the proper WML DOM class for domCl
236
// public WmlImpl() {domCl = WMLElement.class;}
237
};
238     protected static class Wml1xImpl extends WmlImpl implements Wml1x {};
239     protected static class Wml1_0Impl extends Wml1xImpl {};
240     protected static class XhtmlImpl extends XmlImpl implements Xhtml {};
241     protected static class XhtmlBasicImpl extends XhtmlImpl implements XhtmlBasic {};
242     protected static class XhtmlBasic1xImpl extends XhtmlBasicImpl implements XhtmlBasic1x {};
243     protected static class XhtmlBasic1_0Impl extends XhtmlBasic1xImpl {};
244     protected static class XhtmlStandardImpl extends XhtmlBasicImpl implements XhtmlStandard {};
245     protected static class XhtmlStandard1xImpl extends XhtmlStandardImpl implements XhtmlStandard1x {};
246     protected static class XhtmlStandard1_0Impl extends XhtmlStandard1xImpl {};
247     protected static class BinaryDataImpl extends GenericDataImpl implements BinaryData {
248 //TODO: we don't have a DOM for parsing binary data
249
public BinaryDataImpl() {domCl = null;}
250     };
251     protected static class RtfImpl extends BinaryDataImpl implements Rtf {};
252     protected static class DocImpl extends BinaryDataImpl implements Doc {};
253     protected static class PdfImpl extends BinaryDataImpl implements Pdf {};
254     protected static class ExeImpl extends BinaryDataImpl implements Exe {};
255     protected static class UnknownFormatImpl extends GenericDataImpl implements UnknownFormat {};
256
257
258
259     /**
260      * Private constructor to prevent external instantiation
261      */

262     protected FormatType() {}
263
264     /**
265      * Get the DOM class associated with this particular format type.
266      * This is useful because it makes it possible for components to
267      * look up a Renderer by getting the DOM class from the format
268      * type. By default, returns a reference to Node.class. HTML formats
269      * return a reference to HTMLElement.class.
270      *
271      * @return the DOM class associated with this particular format type.
272      */

273     public Class JavaDoc getDOMClass() {
274         return domCl;
275     }
276
277     public static void main(String JavaDoc args[]) {
278         System.out.println ("Testing...");
279         System.out.println ("HTML_3_0 instanceof FormatType.GenericData:"+(FormatType.HTML_3_0 instanceof FormatType.GenericData));
280     }
281 }
282
Popular Tags