Source Code Cross Referenced for HtmlGenerator.java in  » Web-Server » Jigsaw » org » w3c » jigsaw » html » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Jigsaw » org.w3c.jigsaw.html 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // HtmlGenerator.java
002:        // $Id: HtmlGenerator.java,v 1.10 2004/07/20 15:10:05 ylafon Exp $
003:        // (c) COPYRIGHT MIT and INRIA, 1996.
004:        // Please first read the full copyright statement in file COPYRIGHT.html
005:
006:        package org.w3c.jigsaw.html;
007:
008:        import java.io.ByteArrayInputStream;
009:        import java.io.InputStream;
010:        import java.io.UnsupportedEncodingException;
011:
012:        import java.util.Vector;
013:
014:        import org.w3c.www.mime.MimeType;
015:
016:        class HtmlHead {
017:
018:            String title = null;
019:            String base = null;
020:            Vector links = null;
021:            HtmlStyle style = null;
022:            HtmlScript script = null;
023:            StringBuffer meta = null; // FIXME
024:
025:            /**
026:             * @return the current HtmlStyle (if any)
027:             */
028:
029:            public HtmlStyle getStyle() {
030:                return style;
031:            }
032:
033:            /**
034:             * Create and add/replace a new style in the head
035:             * @param type, the MimeType of the style
036:             * @param style, the full text of the style sheet
037:             */
038:
039:            public void addStyle(MimeType type, String style) {
040:                this .style = new HtmlStyle(type, style);
041:            }
042:
043:            /**
044:             * Add or append style to the current Style
045:             * @param style, the full text of the style sheet to append
046:             */
047:
048:            public void addStyle(String style) {
049:                if (this .style == null)
050:                    this .style = new HtmlStyle(style);
051:                else
052:                    this .style.append(style);
053:            }
054:
055:            /**
056:             * Create and add/replace a new script in the head
057:             * @param lang, the scripting language
058:             * @param style, the full text (or a part) of the script
059:             */
060:            public void addScript(String lang, String script) {
061:                this .script = new HtmlScript(lang, script);
062:            }
063:
064:            /**
065:             * Add or append script to the current Style
066:             * @param script, the full text (or a part) of the script.
067:             */
068:
069:            public void addScript(String script) {
070:                if (this .script == null)
071:                    this .script = new HtmlScript(script);
072:                else
073:                    this .script.append(script);
074:            }
075:
076:            /**
077:             * set the HREF part of the BASE element
078:             */
079:
080:            public void addBase(String base) {
081:                this .base = base;
082:            }
083:
084:            /**
085:             * Add a link to the head
086:             * @param link an HtmlLink
087:             */
088:
089:            public void addLink(HtmlLink link) {
090:                if (links == null)
091:                    links = new Vector(4);
092:                links.addElement(link);
093:            }
094:
095:            /** Add some meta-http-equiv information 
096:             * @param name, the name of the meta-http 
097:             * @param value, the name of the meta-http
098:             */
099:
100:            public void addMeta(String name, String value) {
101:                if (meta == null) {
102:                    meta = new StringBuffer("<meta http-equiv=\"" + name
103:                            + "\" content=\"" + value + "\">");
104:                } else {
105:                    meta.append("<meta http-equiv=\"" + name + "\" content=\""
106:                            + value + "\">");
107:                }
108:            }
109:
110:            /**
111:             * generate a String format of the HEAD element that can
112:             * be inserted in a HTML document
113:             */
114:
115:            public String toString() {
116:                StringBuffer strlink = new StringBuffer("");
117:                if (links != null) {
118:                    for (int i = 0; i < links.size(); i++)
119:                        strlink.append(links.elementAt(i).toString());
120:                }
121:                return "<head>\n <title>"
122:                        + title
123:                        + "</title>\n"
124:                        + ((meta != null) ? meta.toString() : "")
125:                        + ((base != null) ? " <base href=\"" + base + "\">\n"
126:                                : "") + strlink.toString()
127:                        + ((script != null) ? script.toString() : "")
128:                        + ((style != null) ? style.toString() : "") + "</head>";
129:            }
130:
131:            public HtmlHead(String title) {
132:                this .title = title;
133:            }
134:        }
135:
136:        /**
137:         * A simple HTML generator.
138:         * This class implements an HTML generator that allows to output dynamic
139:         * HTML content out.
140:         */
141:
142:        public class HtmlGenerator {
143:            private static MimeType defaultType = MimeType.TEXT_HTML;
144:
145:            HtmlHead head = null;
146:            StringBuffer body = null;
147:            String content = null; // content once closed.
148:            boolean bodytag = true; // frameset hack
149:            String encoding = null; // the encoding, default is ISO8859_1
150:            private MimeType type = null;
151:
152:            /**
153:             * Get this stream MIME type.
154:             * This defaults to <strong>text/html</strong>.
155:             */
156:
157:            public MimeType getMimeType() {
158:                return (type == null) ? defaultType : type;
159:            }
160:
161:            /**
162:             * Don't emit body tag. This is usefull in conjunction with the FRAMESET
163:             * tag, that requires that no BODY tag be emited.
164:             * @param value If <strong>true</strong>, a BODY tag will be emited.
165:             */
166:
167:            public void emitBODYTag(boolean value) {
168:                this .bodytag = value;
169:            }
170:
171:            /**
172:             * Append the given string, escaping all special characters. This can be 
173:             * used only if you know that the string you are inserting doesn't contain
174:             * HTML tags
175:             */
176:
177:            public void appendAndEscape(String content) {
178:                for (int i = 0; i < content.length(); i++) {
179:                    char ch = content.charAt(i);
180:                    switch (ch) {
181:                    case '<':
182:                        body.append("&lt;");
183:                        break;
184:                    case '>':
185:                        body.append("&gt;");
186:                        break;
187:                    case '&':
188:                        body.append("&amp;");
189:                        break;
190:                    default:
191:                        body.append(ch);
192:                        break;
193:                    }
194:                }
195:            }
196:
197:            /**
198:             * Add a Base element to the head
199:             * @param the href part of the BASE element
200:             */
201:
202:            public void addBase(String base) {
203:                head.addBase(base);
204:            }
205:
206:            /**
207:             * Add style to this html page
208:             * @see org.w3c.jigsaw.html.HtmlStyle
209:             */
210:
211:            public void addStyle(String style) {
212:                head.addStyle(style);
213:            }
214:
215:            /**
216:             * Add style to this html page
217:             * @see org.w3c.jigsaw.html.HtmlStyle
218:             */
219:
220:            public void addStyle(MimeType type, String style) {
221:                head.addStyle(type, style);
222:            }
223:
224:            /**
225:             * Add script to this html page
226:             * @see org.w3c.jigsaw.html.HtmlScript
227:             */
228:
229:            public void addScript(String script) {
230:                head.addScript(script);
231:            }
232:
233:            /**
234:             * Add script to this html page
235:             * @see org.w3c.jigsaw.html.HtmlScript
236:             */
237:
238:            public void addScript(String lang, String script) {
239:                head.addScript(lang, script);
240:            }
241:
242:            /**
243:             * Add a link to the head of this html document
244:             * @param link the link
245:             */
246:
247:            public void addLink(HtmlLink link) {
248:                head.addLink(link);
249:            }
250:
251:            /**
252:             * Append the given string to the document body.
253:             * @param The HTML string to append.
254:             */
255:
256:            public void append(String content) {
257:                body.append(content);
258:            }
259:
260:            /**
261:             * Append the two strings to the document body.
262:             * <code>append("x"+"y");</code> is equivalent but slower than
263:             * <code>append("x", "y");</code>.
264:             * @param str1 The first string.
265:             * @param str2 The second string.
266:             */
267:
268:            public void append(String str1, String str2) {
269:                body.append(str1);
270:                body.append(str2);
271:            }
272:
273:            public void append(String s1, String s2, String s3) {
274:                body.append(s1);
275:                body.append(s2);
276:                body.append(s3);
277:            }
278:
279:            /**
280:             * Get the length of this html document
281:             * @return the length in bytes of the document
282:             */
283:
284:            public int length() {
285:                if (content == null)
286:                    close();
287:                return content.length();
288:            }
289:
290:            /**
291:             * Close the given document: its composition is now finished.
292:             * @return	The content length for this document.
293:             */
294:
295:            public void close() {
296:                if (content != null)
297:                    return;
298:                content = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 "
299:                        + "Transitional//EN\"\n"
300:                        + "               \"http://www.w3.org/TR/html4/loose.dtd\">\n"
301:                        + "<html>\n"
302:                        + head.toString()
303:                        + "\n"
304:                        + (bodytag ? ("<body>\n" + body.toString() + "</body>\n")
305:                                : body.toString()) + "</html>";
306:                return;
307:            }
308:
309:            /**
310:             * adds a htt-equiv meta tag to the head of the document
311:             * @param name the name of the pseudo http tag
312:             * @param value, the string value of the pseudo header
313:             */
314:
315:            public void addMeta(String name, String value) {
316:                head.addMeta(name, value);
317:            }
318:
319:            /**
320:             * @deprecated
321:             * @see addMeta
322:             */
323:
324:            public void meta(String name, String value) {
325:                addMeta(name, value);
326:            }
327:
328:            /**
329:             * Get the input string for reading the document.
330:             * @return An input stream to get the generated document from.
331:             */
332:
333:            public InputStream getInputStream() {
334:                close();
335:                try {
336:                    return new ByteArrayInputStream(content.getBytes(encoding));
337:                } catch (UnsupportedEncodingException ex) {
338:                    throw new RuntimeException(this .getClass().getName()
339:                            + "[getInputStream] Unable to convert"
340:                            + "properly char to bytes");
341:                }
342:            }
343:
344:            /**
345:             * create the HTML generator with a specific encoding
346:             * @param the title, a String
347:             * @param the encoding used, also a String
348:             */
349:            public HtmlGenerator(String title, String encoding) {
350:                this .head = new HtmlHead(title);
351:                this .body = new StringBuffer();
352:                this .encoding = encoding;
353:                String param[] = new String[1];
354:                String value[] = new String[1];
355:                param[0] = "charset";
356:                // a translation table may be needed there
357:                value[0] = encoding;
358:                this .type = new MimeType("text", "html", param, value);
359:            }
360:
361:            /**
362:             * create the HTML generator with the default HTML encoding "ISO8859_1"
363:             * @param title, the document title
364:             */
365:            public HtmlGenerator(String title) {
366:                this (title, "ISO-8859-1");
367:            }
368:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.