001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.php.docgen;
042:
043: import java.io.BufferedReader;
044: import java.io.File;
045: import java.io.FileReader;
046: import java.io.IOException;
047:
048: /**
049: * @author ads
050: *
051: */
052: class CategoriesParser {
053:
054: private static final String DIV = "div"; // NOI18N
055:
056: private static final String CLASS = "class"; // NOI18N
057:
058: private static final String REFERENCE = "reference"; // NOI18N
059:
060: public static final String OPEN_BRACKET = "<"; // NOI18N
061:
062: public static final String CLOSE_BRACKET = ">"; // NOI18N
063:
064: private static final String EQUAL = "="; // NOI18N
065:
066: private static final String PART = "part"; // NOI18N
067:
068: CategoriesParser(File file, File folder) throws IOException {
069: myFolder = folder;
070: StringBuilder builder = new StringBuilder();
071: BufferedReader reader = new BufferedReader(new FileReader(file));
072: String line = null;
073: try {
074: while ((line = reader.readLine()) != null) {
075: builder.append(line);
076: builder.append(" ");
077: }
078: } finally {
079: reader.close();
080: }
081: myContent = builder.toString();
082: builder.delete(0, builder.length());
083: builder = null;
084: }
085:
086: void parse() throws WriteException {
087: int index = getContent().indexOf(OPEN_BRACKET);
088: int start = -1;
089: int count = 0;
090: while (index != -1) {
091: int end = getContent().indexOf(CLOSE_BRACKET, index);
092: if (end != -1) {
093: if (findCategory(getContent().substring(index, end),
094: REFERENCE)) {
095: parseCategory(start, index, count);
096: count++;
097: start = index;
098: }
099: }
100: index = getContent().indexOf(OPEN_BRACKET, end);
101: }
102:
103: /*
104: * Flag for last reference section. Its end differs from end of
105: * refrences in the middle, so we need special hanlder.
106: */
107: boolean found = false;
108: index = getContent().indexOf(CLOSE_BRACKET, start);
109: while (index != -1) {
110: int end = getContent().indexOf(CLOSE_BRACKET, index);
111: if (end != -1) {
112: if (findCategory(getContent().substring(index, end),
113: PART)) {
114: found = true;
115: parseCategory(start, index, count);
116: break;
117: }
118: }
119: index = getContent().indexOf(OPEN_BRACKET, end);
120: }
121:
122: myContent = null;
123: if (!found) {
124: throw new IllegalStateException(
125: "Couldn't recognize last category"); // NOI18N
126: }
127: }
128:
129: private void parseCategory(int start, int end, int indx)
130: throws WriteException {
131: if (start > 0) {
132: CategoryParser parser = new CategoryParser(getContent()
133: .substring(start, end), indx, getFolder());
134: parser.parse();
135: }
136: }
137:
138: private boolean findCategory(String tag, String classType) {
139: if (tag.length() <= 2) {
140: return false;
141: }
142: String tagContent = tag.substring(1, tag.length() - 1).trim()
143: .toLowerCase();
144: if (tagContent.startsWith(DIV)) {
145: tagContent = tagContent.substring(DIV.length()).trim();
146: return findReference(tagContent, classType);
147: } else {
148: return false;
149: }
150: }
151:
152: private boolean findReference(String tagContent, String classType) {
153: if (tagContent.startsWith(CLASS)) {
154: String str = tagContent.substring(CLASS.length()).trim();
155: if (!str.startsWith(EQUAL)) {
156: return false;
157: }
158: str = str.substring(1);
159: return str.equals(classType)
160: || str.equals("'" + classType + "'")
161: || str.equals('"' + classType + '"');
162: }
163: return false;
164: }
165:
166: File getFolder() {
167: return myFolder;
168: }
169:
170: private String getContent() {
171: return myContent;
172: }
173:
174: private File myFolder;
175:
176: private String myContent;
177: }
|