/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.clearsilver.HDF; import org.clearsilver.CS; import java.io.Reader; import java.io.IOException; import java.io.FileReader; import java.io.LineNumberReader; import java.util.regex.Pattern; import java.util.regex.Matcher; /* * SampleTagInfo copies text from a given file into the javadoc comment. * * The @include tag copies the text verbatim from the given file. * * The @sample tag copies the text from the given file, stripping leading and * trailing whitespace, and reducing the indent level of the text to the indent * level of the first non-whitespace line. * * Both tags accept either a filename and an id or just a filename. If no id * is provided, the entire file is copied. If an id is provided, the lines * in the given file between the first two lines containing BEGIN_INCLUDE(id) * and END_INCLUDE(id), for the given id, are copied. The id may be only * letters, numbers and underscore (_). * * Four examples: * {@include samples/ApiDemos/src/com/google/app/Notification1.java} * {@sample samples/ApiDemos/src/com/google/app/Notification1.java} * {@include samples/ApiDemos/src/com/google/app/Notification1.java Bleh} * {@sample samples/ApiDemos/src/com/google/app/Notification1.java Bleh} * */ public class SampleTagInfo extends TagInfo { static final int STATE_BEGIN = 0; static final int STATE_MATCHING = 1; static final Pattern TEXT = Pattern.compile( "[\r\n \t]*([^\r\n \t]*)[\r\n \t]*([0-9A-Za-z_]*)[\r\n \t]*", Pattern.DOTALL); private static final String BEGIN_INCLUDE = "BEGIN_INCLUDE"; private static final String END_INCLUDE = "END_INCLUDE"; private ContainerInfo mBase; private String mIncluded; public static String escapeHtml(String str) { return str.replace("&", "&").replace("<", "<").replace(">", ">"); } private static boolean isIncludeLine(String str) { return str.indexOf(BEGIN_INCLUDE)>=0 || str.indexOf(END_INCLUDE)>=0; } SampleTagInfo(String name, String kind, String text, ContainerInfo base, SourcePositionInfo position) { super(name, kind, text, position); mBase = base; Matcher m = TEXT.matcher(text); if (!m.matches()) { Errors.error(Errors.BAD_INCLUDE_TAG, position, "Bad @include tag: " + text); return; } String filename = m.group(1); String id = m.group(2); boolean trim = "@sample".equals(name); if (id == null || "".equals(id)) { mIncluded = readFile(position, filename, id, trim, true, false); } else { mIncluded = loadInclude(position, filename, id, trim); } if (mIncluded == null) { Errors.error(Errors.BAD_INCLUDE_TAG, position, "include tag '" + id + "' not found in file: " + filename); } } static String getTrimString(String line) { int i = 0; int len = line.length(); for (; i= 0) { state = STATE_MATCHING; } break; case STATE_MATCHING: if (line.indexOf(end) >= 0) { return result.substring(0); } else { boolean empty = "".equals(line.trim()); if (trim) { if (isIncludeLine(line)) { continue; } if (trimLength < 0 && !empty) { trimString = getTrimString(line); if (trimString != null) { trimLength = trimString.length(); } } if (trimLength >= 0 && line.length() > trimLength) { boolean trimThisLine = true; for (int i=0; i= 0) { if (!empty) { for (int i=0; i