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.BufferedWriter;
044: import java.io.File;
045: import java.io.FileWriter;
046: import java.io.IOException;
047: import java.util.regex.Matcher;
048: import java.util.regex.Pattern;
049:
050: /**
051: * @author ads
052: *
053: */
054: class MethodParser {
055:
056: private static final String HTML_POSTFIX = ".html"; // NOI18N
057:
058: private static final String METHOD_POSTFIX = ".method"; // NOI18N
059:
060: /*
061: * Head html tag.
062: */
063: private static final String H = "h"; // NOI18N
064:
065: private static final Pattern FUNCTION_NAME_PATTERN = Pattern
066: .compile("</[ \t]*a[ \t]*>([^<]*)<\\/[ \t]*h([^>]*)>",
067: Pattern.CASE_INSENSITIVE); // NOI18N
068:
069: private static final String TYPE_PATTERN = "[\\s>]*([^\\s>]*)\\s*<b\\s+class\\s*=\\s*"
070: + "\"methodname\"" + "\\s*>\\s*"; // NOI18N
071:
072: private static final String ARGS_PATTERN = "\\s*</b\\s*>\\s*(\\([^\\)]*\\))"; // NOI18N
073:
074: MethodParser(String methodNameId, String content, File folder) {
075: myNameId = methodNameId;
076: myContent = content.trim();
077: myFolder = folder;
078:
079: initContent();
080: }
081:
082: void parse() throws WriteException {
083: File folder = getFolder();
084: File file = new File(folder, getFileName());
085:
086: addToIndex();
087:
088: writeMethod();
089:
090: BufferedWriter writer = null;
091: try {
092: writer = new BufferedWriter(new FileWriter(file));
093: writer.write(getContent());
094: } catch (IOException e) {
095: throw new WriteException("Couldn't write file " + file, e);
096: } finally {
097: if (writer != null) {
098: try {
099: writer.close();
100: } catch (IOException e) {
101: throw new WriteException("Couldn't close file "
102: + file, e);
103: }
104: }
105: }
106: }
107:
108: private void writeMethod() throws WriteException {
109: writeMethodField(myDocName);
110: writeMethodField(myType);
111: writeMethodField(myArguments);
112: }
113:
114: private void writeMethodField(String field) throws WriteException {
115: File file = new File(getFolder(), getMethodName());
116: BufferedWriter writer = null;
117: try {
118: writer = new BufferedWriter(new FileWriter(file, true));
119: if (field != null) {
120: writer.write(field);
121: }
122: writer.newLine();
123: } catch (IOException e) {
124: throw new WriteException(
125: "Couldn't write method structure file for " + // NOI18N
126: myNameId + " method ", e);
127: } finally {
128: try {
129: writer.close();
130: } catch (IOException e) {
131: throw new WriteException(
132: "Couldn't close method name file for " + // NOI18N
133: myNameId, e);
134: }
135: }
136: }
137:
138: private void addToIndex() throws WriteException {
139: File file = new File(getFolder(), CategoryParser.INDEX);
140: BufferedWriter writer = null;
141: try {
142: writer = new BufferedWriter(new FileWriter(file, true));
143: writer.write(getFileName());
144: writer.write('\n');
145: } catch (IOException e) {
146: throw new WriteException("Couldn't write index file for " + // NOI18N
147: getFolder().getName(), e);
148: } finally {
149: try {
150: writer.close();
151: } catch (IOException e) {
152: throw new WriteException(
153: "Couldn't close index file for " + // NOI18N
154: getFolder().getName(), e);
155: }
156: }
157: }
158:
159: private void initContent() {
160: Matcher matcher = FUNCTION_NAME_PATTERN.matcher(getContent());
161: if (!matcher.find() || matcher.start() != 0) {
162: throw new IllegalStateException(
163: "Unrecognized method format found, " + "method "
164: + getNameId());
165: }
166: int end = matcher.end();
167: StringBuilder builder = new StringBuilder();
168: String name = matcher.group(1).trim();
169: String headNumber = matcher.group(2).trim();
170: builder.append(CategoriesParser.OPEN_BRACKET);
171: builder.append(H);
172: builder.append(headNumber);
173: builder.append(CategoriesParser.CLOSE_BRACKET);
174: builder.append(name);
175: builder.append(CategoriesParser.OPEN_BRACKET);
176: builder.append('/');
177: builder.append(H);
178: builder.append(headNumber);
179: builder.append(CategoriesParser.CLOSE_BRACKET);
180: builder.append(getContent().substring(end));
181: myContent = builder.toString();
182:
183: Pattern pattern = Pattern.compile(TYPE_PATTERN + name
184: + ARGS_PATTERN, Pattern.CASE_INSENSITIVE);
185: matcher = pattern.matcher(getContent());
186: if (matcher.find()) {
187: myType = matcher.group(1).trim();
188: myArguments = matcher.group(2).trim();
189: }
190:
191: myDocName = name;
192: }
193:
194: private String getFileName() {
195: return getNameId() + HTML_POSTFIX;
196: }
197:
198: private String getMethodName() {
199: return getNameId() + METHOD_POSTFIX;
200: }
201:
202: private String getNameId() {
203: return myNameId;
204: }
205:
206: private String getContent() {
207: return myContent;
208: }
209:
210: private File getFolder() {
211: return myFolder;
212: }
213:
214: private String myNameId;
215:
216: private String myContent;
217:
218: private File myFolder;
219:
220: private String myDocName;
221:
222: private String myType;
223:
224: private String myArguments;
225: }
|