Source Code Cross Referenced for AnalysisParser.java in  » Web-Framework » argun » biz » hammurapi » web » analysis » 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 Framework » argun » biz.hammurapi.web.analysis 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * argun 1.0
003:         * Web 2.0 delivery framework 
004:         * Copyright (C) 2007  Hammurapi Group
005:         *
006:         * This program is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2 of the License, or (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         *
020:         * URL: http://www.hammurapi.biz
021:         * e-Mail: support@hammurapi.biz 
022:         */
023:        package biz.hammurapi.web.analysis;
024:
025:        import java.io.StringReader;
026:        import java.util.ArrayList;
027:        import java.util.Iterator;
028:        import java.util.List;
029:
030:        import org.apache.log4j.Logger;
031:
032:        import antlr.Token;
033:        import antlr.TokenStreamException;
034:        import biz.hammurapi.web.analysis.sql.AnalysisTerm;
035:        import biz.hammurapi.web.util.HTMLLexer;
036:        import biz.hammurapi.web.util.HTMLTokenTypes;
037:
038:        /**
039:         * Helper class to form help topic tree.
040:         * @author Pavel Vlasov
041:         *
042:         */
043:        public class AnalysisParser {
044:            private static final Logger logger = Logger
045:                    .getLogger(AnalysisParser.class);
046:
047:            private static final String DOUBLE_RIGHT_BRACKET = "drb";
048:
049:            private static final String DOUBLE_LEFT_BRACKET = "dlb";
050:
051:            private static final String TOOLTIP_END = "tooltip-end";
052:
053:            private static final int MIN_TOOLTIP_LENGTH = 100;
054:
055:            private static final String TERM_OPEN = "[[";
056:
057:            private static final String TERM_CLOSE = "]]";
058:
059:            public static final int DEFAULT_MAX_TOOLTIP_LENGTH = 1000;
060:
061:            static final String ELLIPSIS = " ...";
062:
063:            static final int MAX_TERM_LENGTH = 7000;
064:
065:            private TermSource termSource;
066:
067:            public AnalysisParser(TermSource termSource) {
068:                this .termSource = termSource;
069:            }
070:
071:            /**
072:             * Expands [[...]] terms to values obtained from TermSource.
073:             * @param input
074:             * @param isTooltip
075:             * @return
076:             */
077:            public String parse(String input, boolean isTooltip) {
078:                StringBuffer ret = new StringBuffer();
079:
080:                // Go through text and break at [[...]]
081:                String txt = input;
082:                int oidx = txt.indexOf(TERM_OPEN);
083:                while (oidx != -1) {
084:                    int cidx = txt.indexOf(TERM_CLOSE, oidx);
085:                    if (cidx == -1) {
086:                        break;
087:                    }
088:                    //			 Term is too long. Must be a mistake, ignore open tag.
089:                    if (cidx - oidx > MAX_TERM_LENGTH) {
090:                        logger.warn("Term is too long at position " + oidx);
091:                        ret.append(txt.substring(0, oidx + 2));
092:                        txt = txt.substring(oidx + 2);
093:                        oidx = txt.indexOf(TERM_OPEN);
094:                        continue;
095:                    }
096:                    if (oidx > 0) {
097:                        ret.append(txt.substring(0, oidx));
098:                    }
099:                    String term = txt.substring(oidx + 2, cidx);
100:                    int tpidx = term.indexOf("|");
101:                    int nidx = term.indexOf(":");
102:                    String namespace = nidx != -1
103:                            && (tpidx == -1 || nidx < tpidx) ? term.substring(
104:                            0, nidx) : null;
105:                    String alternativeText = tpidx == -1 ? null : term
106:                            .substring(tpidx + 1);
107:                    String termName = tpidx == -1 ? term.substring(nidx + 1)
108:                            : term.substring(nidx + 1, tpidx);
109:                    if (namespace == null && alternativeText == null) {
110:                        if (TOOLTIP_END.equals(termName)) {
111:                            if (isTooltip) {
112:                                ret.append(ELLIPSIS);
113:                                return ret.toString();
114:                            }
115:                        } else if (DOUBLE_LEFT_BRACKET.equals(termName)) {
116:                            ret.append("[[");
117:                        } else if (DOUBLE_RIGHT_BRACKET.equals(termName)) {
118:                            ret.append("]]");
119:                        } else {
120:                            Object trm = termSource.getTerm(namespace, termName
121:                                    .trim(), alternativeText);
122:                            ret.append(term2text(isTooltip, alternativeText,
123:                                    termName, trm));
124:                        }
125:                    } else {
126:                        Object trm = termSource.getTerm(
127:                                namespace == null ? namespace : namespace
128:                                        .trim(), termName.trim(),
129:                                alternativeText);
130:                        ret.append(term2text(isTooltip, alternativeText,
131:                                termName, trm));
132:                    }
133:                    txt = txt.substring(cidx + 2);
134:                    oidx = txt.indexOf(TERM_OPEN);
135:                }
136:                ret.append(txt);
137:
138:                int maxTooltipLength = DEFAULT_MAX_TOOLTIP_LENGTH;
139:
140:                if (!isTooltip || ret.length() < maxTooltipLength) {
141:                    return ret.toString();
142:                }
143:
144:                // Paragraph detection.
145:                Token lastPARA = null;
146:                List tokenChain = new ArrayList();
147:                HTMLLexer lexer = new HTMLLexer(
148:                        new StringReader(ret.toString()));
149:                int textcounter = 0; // To protect against infinite loops.
150:                try {
151:                    for (Token t = lexer.nextToken(); t != null
152:                            && t.getType() != HTMLTokenTypes.CHTML
153:                            && t.getType() != HTMLTokenTypes.EOF; t = lexer
154:                            .nextToken()) {
155:                        tokenChain.add(t);
156:                        if (t.getText() != null
157:                                && ((t.getType() == HTMLTokenTypes.UNDEFINED_TOKEN && t
158:                                        .getText().length() == 1) || t
159:                                        .getType() == HTMLTokenTypes.PCDATA)) {
160:                            textcounter += t.getText().length();
161:                            if (textcounter >= maxTooltipLength) {
162:                                tokenChain.add(new Token(
163:                                        HTMLTokenTypes.UNDEFINED_TOKEN,
164:                                        ELLIPSIS));
165:                                break;
166:                            }
167:                        }
168:
169:                        switch (t.getType()) {
170:                        case HTMLTokenTypes.OPARA:
171:                        case HTMLTokenTypes.CPARA:
172:                        case HTMLTokenTypes.SAPARA:
173:                            lastPARA = t;
174:                            break;
175:                        }
176:                    }
177:                } catch (TokenStreamException e) {
178:                    if (ret.length() > maxTooltipLength) {
179:                        return ret.substring(0, maxTooltipLength) + ELLIPSIS;
180:                    }
181:                }
182:
183:                if (lastPARA != null) {
184:                    StringBuffer rsb = new StringBuffer();
185:                    Iterator it = tokenChain.iterator();
186:                    while (it.hasNext()) {
187:                        Token t = (Token) it.next();
188:                        if (t == lastPARA) {
189:                            if (rsb.toString().trim().length() < MIN_TOOLTIP_LENGTH) {
190:                                break;
191:                            }
192:                            rsb.append(ELLIPSIS);
193:                            return rsb.toString();
194:                        }
195:                        rsb.append(t.getText());
196:                    }
197:                }
198:
199:                StringBuffer rsb = new StringBuffer();
200:                Iterator it = tokenChain.iterator();
201:                while (it.hasNext()) {
202:                    Token t = (Token) it.next();
203:                    rsb.append(t.getText());
204:                }
205:                return rsb.toString();
206:            }
207:
208:            private String term2text(boolean isTooltip, String alternativeText,
209:                    String termName, Object trm) {
210:                String termText;
211:                if (trm == null) {
212:                    if (alternativeText == null) {
213:                        termText = termName;
214:                    } else {
215:                        termText = alternativeText;
216:                    }
217:                } else {
218:                    if (isTooltip && trm instanceof  AnalysisTerm) {
219:                        String text = AnalysisActions.isBlank(alternativeText) ? ((AnalysisTerm) trm)
220:                                .getName()
221:                                : alternativeText;
222:                        termText = "<B style=\"border-bottom:gray dotted 1px\">"
223:                                + text + "</B>";
224:                    } else {
225:                        termText = trm.toString();
226:                    }
227:                }
228:                return termText;
229:            }
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.