Source Code Cross Referenced for DefaultSectionsParser.java in  » IDE-Eclipse » ui-examples » org » eclipse » ui » examples » readmetool » 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 » IDE Eclipse » ui examples » org.eclipse.ui.examples.readmetool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Copyright (c) 2000, 2006 IBM Corporation and others.
003:         * All rights reserved. This program and the accompanying materials
004:         * are made available under the terms of the Eclipse Public License v1.0
005:         * which accompanies this distribution, and is available at
006:         * http://www.eclipse.org/legal/epl-v10.html
007:         *
008:         * Contributors:
009:         *     IBM Corporation - initial API and implementation
010:         *******************************************************************************/package org.eclipse.ui.examples.readmetool;
011:
012:        import java.io.ByteArrayOutputStream;
013:        import java.io.IOException;
014:        import java.io.InputStream;
015:        import java.util.Hashtable;
016:        import java.util.Vector;
017:
018:        import org.eclipse.core.resources.IFile;
019:        import org.eclipse.core.runtime.CoreException;
020:        import org.eclipse.core.runtime.IAdaptable;
021:
022:        /**
023:         * This class is a simple parser implementing the IReadmeFileParser
024:         * interface. It parses a Readme file into sections based on the
025:         * existence of numbered section tags in the input. A line beginning
026:         * with a number followed by a dot will be taken as a section indicator
027:         * (for example, 1., 2., or 12.). 
028:         * As well, a line beginning with a subsection-style series of numbers
029:         * will also be taken as a section indicator, and can be used to 
030:         * indicate subsections (for example, 1.1, or 1.1.12).
031:         */
032:        public class DefaultSectionsParser implements  IReadmeFileParser {
033:            /**
034:             * Returns the mark element that is the logical parent
035:             * of the given mark number.  Each dot in a mark number
036:             * represents a parent-child separation.  For example,
037:             * the parent of 1.2 is 1, the parent of 1.4.1 is 1.4.
038:             * Returns null if there is no appropriate parent.
039:             */
040:            protected IAdaptable getParent(Hashtable toc, String number) {
041:                int lastDot = number.lastIndexOf('.');
042:                if (lastDot < 0)
043:                    return null;
044:                String parentNumber = number.substring(0, lastDot);
045:                return (IAdaptable) toc.get(parentNumber);
046:            }
047:
048:            /**
049:             * Returns a string containing the contents of the given
050:             * file.  Returns an empty string if there were any errors
051:             * reading the file.
052:             */
053:            protected String getText(IFile file) {
054:                try {
055:                    InputStream in = file.getContents();
056:                    ByteArrayOutputStream out = new ByteArrayOutputStream();
057:                    byte[] buf = new byte[1024];
058:                    int read = in.read(buf);
059:                    while (read > 0) {
060:                        out.write(buf, 0, read);
061:                        read = in.read(buf);
062:                    }
063:                    return out.toString();
064:                } catch (CoreException e) {
065:                    // do nothing
066:                } catch (IOException e) {
067:                    // do nothing
068:                }
069:                return ""; //$NON-NLS-1$
070:            }
071:
072:            /**
073:             * Parses the input given by the argument.
074:             *
075:             * @param file  the element containing the input text
076:             * @return an element collection representing the parsed input
077:             */
078:            public MarkElement[] parse(IFile file) {
079:                Hashtable markTable = new Hashtable(40);
080:                Vector topLevel = new Vector();
081:                String s = getText(file);
082:                int start = 0;
083:                int end = -1;
084:                int lineno = 0;
085:                int lastlineno = 0;
086:                MarkElement lastme = null;
087:                int ix;
088:
089:                // parse content for headings
090:                ix = s.indexOf('\n', start);
091:                while (ix != -1) {
092:                    start = end + 1;
093:                    end = ix = s.indexOf('\n', start);
094:                    lineno++;
095:                    if (ix != -1) {
096:                        // skip blanks
097:                        while (s.charAt(start) == ' '
098:                                || s.charAt(start) == '\t') {
099:                            start++;
100:                        }
101:                        if (Character.isDigit(s.charAt(start))) {
102:                            if (lastme != null) {
103:                                lastme
104:                                        .setNumberOfLines(lineno - lastlineno
105:                                                - 1);
106:                            }
107:                            lastlineno = lineno;
108:                            String markName = parseHeading(s, start, end);
109:
110:                            //get the parent mark, if any.
111:                            String markNumber = parseNumber(markName);
112:                            IAdaptable parent = getParent(markTable, markNumber);
113:                            if (parent == null)
114:                                parent = file;
115:
116:                            MarkElement me = new MarkElement(parent, markName,
117:                                    start, end - start);
118:                            lastme = me;
119:
120:                            markTable.put(markNumber, me);
121:                            if (parent == file) {
122:                                topLevel.add(me);
123:                            }
124:                        }
125:                    }
126:                }
127:                if (lastme != null) {
128:                    // set the number of lines for the last section
129:                    lastme.setNumberOfLines(lineno - lastlineno - 1);
130:                }
131:                MarkElement[] results = new MarkElement[topLevel.size()];
132:                topLevel.copyInto(results);
133:                return results;
134:            }
135:
136:            /**
137:             * Creates a section name from the buffer and trims trailing
138:             * space characters.
139:             *
140:             * @param buffer  the string from which to create the section name
141:             * @param start  the start index
142:             * @param end  the end index
143:             * @return a section name
144:             */
145:            private String parseHeading(String buffer, int start, int end) {
146:                while (Character.isWhitespace(buffer.charAt(end - 1))
147:                        && end > start) {
148:                    end--;
149:                }
150:                return buffer.substring(start, end);
151:            }
152:
153:            /**
154:             * Returns the number for this heading.  A heading consists
155:             * of a number (an arbitrary string of numbers and dots), followed by
156:             * arbitrary text.
157:             */
158:            protected String parseNumber(String heading) {
159:                int start = 0;
160:                int end = heading.length();
161:                char c;
162:                do {
163:                    c = heading.charAt(start++);
164:                } while ((c == '.' || Character.isDigit(c)) && start < end);
165:
166:                //disregard trailing dots
167:                while (heading.charAt(start - 1) == '.' && start > 0) {
168:                    start--;
169:                }
170:                return heading.substring(0, start);
171:            }
172:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.