Source Code Cross Referenced for MakeTableOfContents.java in  » Wiki-Engine » VeryQuickWiki » vqwiki » lex » 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 » Wiki Engine » VeryQuickWiki » vqwiki.lex 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package vqwiki.lex;
002:
003:        import java.util.Enumeration;
004:        import java.util.Vector;
005:        import java.util.regex.Matcher;
006:        import java.util.regex.Pattern;
007:        import org.apache.log4j.Logger;
008:        import vqwiki.Environment;
009:
010:        /**
011:         * Experimental class. This class may be used in two ways:
012:         *
013:         * The static addTableOfContents(String) method may be called to automatically
014:         * adds a table of contents on the right side of an article.  This method
015:         * works with all lexers, because it parses the HTML for headers. However it
016:         * doesn't care where it is. So if you have a header on the TopArea / LeftMenu /
017:         * BottomArea, it will also add a TOC there...
018:         *
019:         * The static addTableOfContents(MakeTableOfContents, StringBuffer) method
020:         * may be called to insert a pre-built MakeTableOfContents object into an
021:         * article.  This method requires that the parser has added all table of
022:         * contents headings to the object and included a TOC_INSERT_TAG at the point
023:         * where the table of contents should be inserted.  It is a bit more flexible
024:         * but requires more preperatory work.
025:         *
026:         * @author studer
027:         */
028:        public class MakeTableOfContents {
029:
030:            private static final Logger logger = Logger
031:                    .getLogger(MakeTableOfContents.class);
032:            public static final int STATUS_TOC_UNINITIALIZED = 0;
033:            public static final int STATUS_TOC_INITIALIZED = 1;
034:            public static final int STATUS_NO_TOC = 2;
035:            public static final String TOC_INSERT_TAG = "__INSERT_TOC__";
036:            private int currentLevel = 0;
037:            private int minLevel = 4;
038:            private Vector entries = new Vector();
039:            private int status = STATUS_TOC_UNINITIALIZED;
040:
041:            /**
042:             * Adds TOC at the beginning as a table on the right side of the page if the
043:             * page has any HTML-headers.
044:             *
045:             * @param text
046:             * @return
047:             */
048:            public static String addTableOfContents(String text) {
049:                logger.debug("Start TOC generating...");
050:                Pattern p = Pattern
051:                        .compile("<[Hh][123][^>]*>(.*)</[Hh][123][^>]*>");
052:                Matcher m = p.matcher(text);
053:                StringBuffer result = new StringBuffer();
054:                StringBuffer toc = new StringBuffer();
055:                toc.append("<table align=\"right\" class=\"toc\"><tr><td>");
056:                int position = 0;
057:                while (m.find()) {
058:                    result.append(text.substring(position, m.start(1)));
059:                    position = m.start(1);
060:                    result.append("<a class=\"tocheader\" name=\"" + position
061:                            + "\" id=\"" + position + "\"></a>");
062:                    if (m.group().startsWith("<h1")
063:                            || m.group().startsWith("<H1")) {
064:                        toc.append("<span class=\"tocheader1\">");
065:                    } else if (m.group().startsWith("<h2")
066:                            || m.group().startsWith("<H2")) {
067:                        toc.append("<span class=\"tocheader2\">");
068:                    } else {
069:                        toc.append("<span class=\"tocheader3\">");
070:                    }
071:                    toc.append("<li><a href=\"#" + position + "\">"
072:                            + m.group(1) + "</a></li></span>");
073:                    result.append(text.substring(position, m.end(1)));
074:                    position = m.end(1);
075:                    logger.debug("Adding content: " + m.group(1));
076:                }
077:                toc.append("</td></tr></table>");
078:                result.append(text.substring(position));
079:                if (position > 0) {
080:                    logger.debug("adding TOC at the beginning!");
081:                    toc.append(result);
082:                } else {
083:                    toc = result;
084:                }
085:                return toc.toString();
086:            }
087:
088:            /**
089:             * Insert an existing MakeTableOfContents object into formatted HTML
090:             * output.
091:             *
092:             * @param toc A pre-built MakeTableOfContents object.
093:             * @param contents The Wiki syntax, which should contain TOC_INSERT_TAG at
094:             *  the point where the table of contents object is to be inserted.
095:             * @return The formatted content containing the table of contents.
096:             */
097:            public static StringBuffer addTableOfContents(
098:                    MakeTableOfContents toc, StringBuffer contents) {
099:                int pos = contents.indexOf(MakeTableOfContents.TOC_INSERT_TAG);
100:                if (pos >= 0) {
101:                    // FIXME - don't hardcode minimum TOC size
102:                    if (toc == null
103:                            || toc.size() <= 3
104:                            || toc.getStatus() == MakeTableOfContents.STATUS_NO_TOC
105:                            || !Environment.getInstance().isMakeToc()) {
106:                        // remove the insert tag
107:                        contents.delete(pos, pos
108:                                + MakeTableOfContents.TOC_INSERT_TAG.length());
109:                    } else {
110:                        // insert the toc
111:                        contents.replace(pos, pos
112:                                + MakeTableOfContents.TOC_INSERT_TAG.length(),
113:                                toc.toHTML());
114:                    }
115:                }
116:                return contents;
117:            }
118:
119:            /**
120:             * Add a new table of contents entry.
121:             *
122:             * @param name The name of the entry, to be used in the anchor tag name.
123:             * @param text The text to display for the table of contents entry.
124:             * @param level The level of the entry.  If an entry is a sub-heading of
125:             *  another entry the value should be 2.  If there is a sub-heading of that
126:             *  entry then its value would be 3, and so forth.
127:             */
128:            public void addEntry(String name, String text, int level) {
129:                if (this .status != STATUS_NO_TOC)
130:                    this .status = STATUS_TOC_INITIALIZED;
131:                TableOfContentsEntry entry = new TableOfContentsEntry(name,
132:                        text, level);
133:                entries.add(entry);
134:                if (level < minLevel)
135:                    minLevel = level;
136:            }
137:
138:            /**
139:             * Internal method to close any list tags prior to adding the next entry.
140:             */
141:            private void closeList(int level, StringBuffer text) {
142:                while (level < currentLevel) {
143:                    // close lists to current level
144:                    text.append("</li></ol>");
145:                    currentLevel--;
146:                }
147:            }
148:
149:            /**
150:             * Return the current table of contents status, such as "no table of contents
151:             * allowed" or "uninitialized".
152:             */
153:            public int getStatus() {
154:                return this .status;
155:            }
156:
157:            /**
158:             * Internal method to open any list tags prior to adding the next entry.
159:             */
160:            private void openList(int level, StringBuffer text) {
161:                if (level == currentLevel) {
162:                    // same level as previous item, close previous and open new
163:                    text.append("</li><li>");
164:                    return;
165:                }
166:                while (level > currentLevel) {
167:                    // open lists to current level
168:                    text.append("<ol><li>");
169:                    currentLevel++;
170:                }
171:            }
172:
173:            /**
174:             * Set the current table of contents status, such as "no table of contents
175:             * allowed" or "uninitialized".
176:             */
177:            public void setStatus(int status) {
178:                this .status = status;
179:            }
180:
181:            /**
182:             * Return the number of entries in this TOC object.
183:             */
184:            public int size() {
185:                return this .entries.size();
186:            }
187:
188:            /**
189:             * Return an HTML representation of this table of contents object.
190:             */
191:            public String toHTML() {
192:                Enumeration e = entries.elements();
193:                StringBuffer text = new StringBuffer();
194:                text.append("<table class=\"toc\"><tr><td>");
195:                TableOfContentsEntry entry = null;
196:                int adjustedLevel = 0;
197:                while (e.hasMoreElements()) {
198:                    entry = (TableOfContentsEntry) e.nextElement();
199:                    // adjusted level determines how far to indent the list
200:                    adjustedLevel = ((entry.level - minLevel) + 1);
201:                    closeList(adjustedLevel, text);
202:                    openList(adjustedLevel, text);
203:                    text.append("<a href=\"#").append(entry.name).append("\">")
204:                            .append(entry.text).append("</a>");
205:                }
206:                closeList(0, text);
207:                text.append("</td></tr></table>");
208:                return text.toString();
209:            }
210:
211:            /**
212:             * Inner class holds TOC entries until they can be processed for display.
213:             */
214:            class TableOfContentsEntry {
215:
216:                int level;
217:                String name;
218:                String text;
219:
220:                /**
221:                 *
222:                 */
223:                TableOfContentsEntry(String name, String text, int level) {
224:                    this.name = name;
225:                    this.text = text;
226:                    this.level = level;
227:                }
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.