001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * 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, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.doctool;
017:
018: import com.sun.javadoc.Tag;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.io.InputStream;
024:
025: /**
026: * Utility methods related to including external resources in doc.
027: */
028: public class ResourceIncluder {
029:
030: /**
031: * Copied from {@link com.google.gwt.util.tools.Utility#close(InputStream)}.
032: */
033: public static void close(InputStream is) {
034: try {
035: if (is != null) {
036: is.close();
037: }
038: } catch (IOException e) {
039: }
040: }
041:
042: public static String getResourceFromClasspathScrubbedForHTML(Tag tag) {
043: String partialPath = tag.text();
044: try {
045: String contents;
046: contents = getFileFromClassPath(partialPath);
047: contents = scrubForHtml(contents);
048: return contents;
049: } catch (IOException e) {
050: System.err.println(tag.position().toString()
051: + ": unable to include resource " + partialPath
052: + " for tag " + tag);
053: System.exit(1);
054: return null;
055: }
056: }
057:
058: /**
059: * Copied from
060: * {@link com.google.gwt.util.tools.Utility#getFileFromClassPath(String)}.
061: */
062: private static String getFileFromClassPath(String partialPath)
063: throws IOException {
064: InputStream in = ResourceIncluder.class.getClassLoader()
065: .getResourceAsStream(partialPath);
066: try {
067: if (in == null) {
068: throw new FileNotFoundException(partialPath);
069: }
070: ByteArrayOutputStream os = new ByteArrayOutputStream();
071: int ch;
072: while ((ch = in.read()) != -1) {
073: os.write(ch);
074: }
075: return new String(os.toByteArray(), "UTF-8");
076: } finally {
077: close(in);
078: }
079: }
080:
081: private static String scrubForHtml(String contents) {
082: char[] chars = contents.toCharArray();
083: int len = chars.length;
084: StringBuffer sb = new StringBuffer(len);
085: for (int i = 0; i < len; ++i) {
086: char c = chars[i];
087: switch (c) {
088: case '\r':
089: // collapse \r\n into \n
090: if (i == len - 1 || chars[i + 1] != '\n') {
091: sb.append('\n');
092: }
093: break;
094: case '&':
095: sb.append("&");
096: break;
097: case '<':
098: sb.append("<");
099: break;
100: case '>':
101: sb.append(">");
102: break;
103: default:
104: sb.append(c);
105: break;
106: }
107: }
108: return sb.toString();
109: }
110: }
|