001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.gmaven.runtime.support.stubgen.model; 018 019 import com.thoughtworks.qdox.JavaDocBuilder; 020 import com.thoughtworks.qdox.model.DocletTag; 021 import com.thoughtworks.qdox.model.JavaClass; 022 023 import java.io.PrintWriter; 024 import java.io.StringReader; 025 import java.io.StringWriter; 026 import java.util.regex.Matcher; 027 import java.util.regex.Pattern; 028 029 /** 030 * Parses doclet tags from javadoc. 031 * 032 * @version $Id: JavaDocParser.java 18 2009-07-16 09:39:40Z user57 $ 033 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 034 */ 035 public class JavaDocParser 036 { 037 private static final Pattern JAVADOCS_PATTERN = Pattern.compile("(?s).*/\\*\\*(.*?)\\*/[^\\*/}]*$"); 038 039 public JavaDocDef parse(String text) { 040 // text may be null 041 042 JavaDocDef def = null; 043 044 if (text != null) { 045 Matcher m = JAVADOCS_PATTERN.matcher(text); 046 047 if (m.matches()) { 048 int lastGroupIndex = m.groupCount(); 049 050 if (lastGroupIndex > 0) { 051 text = m.group(lastGroupIndex).trim(); 052 } 053 054 if (text != null) { 055 def = parseWithQDox(text); 056 } 057 } 058 } 059 060 return def; 061 } 062 063 private JavaDocDef parseWithQDox(final String text) { 064 assert text != null; 065 066 // Render a synthetic class to parse out the comment and tags 067 StringWriter writer = new StringWriter(); 068 PrintWriter out = new PrintWriter(writer); 069 070 out.println("/**"); 071 out.println(text); 072 out.println(" */"); 073 out.println("class Dummy {}"); 074 out.flush(); 075 076 StringReader reader = new StringReader(writer.getBuffer().toString()); 077 078 JavaDocBuilder builder = new JavaDocBuilder(); 079 builder.addSource(reader); 080 081 JavaClass[] classes = builder.getClasses(); 082 083 JavaDocDef def = new JavaDocDef(); 084 085 def.setComment(classes[0].getComment()); 086 087 DocletTag[] tags = classes[0].getTags(); 088 089 if (tags != null) { 090 for (int i=0; i<tags.length; i++) { 091 TagDef tag = new TagDef(); 092 093 tag.setName(tags[i].getName()); 094 tag.setValue(tags[i].getValue()); 095 tag.setParameters(tags[i].getParameters()); 096 tag.setNamedParameters(tags[i].getNamedParameterMap()); 097 098 def.addTag(tag); 099 } 100 } 101 102 return def; 103 } 104 }