001: /*
002: * Helma License Notice
003: *
004: * The contents of this file are subject to the Helma License
005: * Version 2.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://adele.helma.org/download/helma/license.txt
008: *
009: * Copyright 1998-2003 Helma Software. All Rights Reserved.
010: *
011: * $RCSfile$
012: * $Author: root $
013: * $Revision: 8604 $
014: * $Date: 2007-09-28 15:16:38 +0200 (Fre, 28 Sep 2007) $
015: */
016:
017: package helma.doc;
018:
019: import java.awt.Point;
020: import java.io.*;
021:
022: /**
023: *
024: */
025: public final class Util {
026: /**
027: * chops a string from comment-delimiters
028: *
029: * @param line a line of raw text
030: *
031: * @return chopped string
032: */
033: public static String chopDelimiters(String line) {
034: line = line.trim();
035: if (line == null) {
036: return null;
037: }
038: if (line.endsWith("*/")) {
039: line = line.substring(0, line.length() - 2);
040: }
041: if (line.startsWith("/**")) {
042: line = line.substring(3).trim();
043: } else if (line.startsWith("/*")) {
044: line = line.substring(2).trim();
045: } else if (line.startsWith("*")) {
046: line = line.substring(1).trim();
047: } else if (line.startsWith("//")) {
048: line = line.substring(2).trim();
049: }
050: return line;
051: }
052:
053: /**
054: * chops anything that comes after a closing comment tag
055: *
056: * @param comment the raw comment
057: *
058: * @return chopped comment
059: */
060: public static String chopComment(String comment) {
061: int idx = comment.indexOf("*/");
062: if (idx > 0) {
063: return comment.substring(0, idx + 2);
064: } else {
065: return comment;
066: }
067: }
068:
069: /**
070: * Extract a part of a file defined by two points from a String array
071: * @param lines an array of lines
072: * @param start of string to extract defined by column x and row y
073: * @param end of string to extract
074: * @return string
075: */
076: public static String extractString(String[] lines, Point start,
077: Point end) {
078: StringBuffer buf = new StringBuffer();
079: int to = Math.min(end.y + 1, lines.length);
080: for (int i = start.y; i < to; i++) {
081: int from = (i == start.y) ? start.x : 0;
082: if (from < 0 || from > lines[i].length()) {
083: System.err.println("Start index " + from
084: + " out of range [0.." + lines[i].length()
085: + "]");
086: from = 0;
087: }
088: if (i == end.y && end.x < lines[i].length())
089: buf.append(lines[i].substring(from, end.x));
090: else
091: buf.append(lines[i].substring(from));
092: buf.append("\n");
093: }
094: return buf.toString().trim();
095: }
096:
097: /**
098: * Extract a part of a file defined by two points from a String array
099: * @param lines an array of lines
100: * @param start of string to extract defined by column x and row y
101: * @param end of string to extract
102: * @return string
103: */
104: public static String extractString(String[] lines, int start,
105: int end) {
106: StringBuffer buf = new StringBuffer();
107: int to = Math.min(end + 1, lines.length);
108: for (int i = start; i < to; i++) {
109: buf.append(lines[i]);
110: buf.append("\n");
111: }
112: return buf.toString().trim();
113: }
114:
115: /**
116: * method to debug file/stream-handling with Point objects. extracts the line p
117: * points to and prints it with a pointer to the given column
118: *
119: * @param sourceFile
120: * @param p x-value is used for column, y for row
121: * @param debugStr string prefixed to output
122: */
123: public static void debugLineFromFile(File sourceFile, Point p,
124: String debugStr) {
125: try {
126: BufferedReader in = new BufferedReader(new FileReader(
127: sourceFile));
128: String line = "";
129: int ct = 0;
130: while (line != null) {
131: line = in.readLine();
132: if (line == null) {
133: System.out.println("eof reached");
134: break;
135: }
136: if (ct == p.y) {
137: System.out.println(debugStr + ": " + line);
138: for (int i = 0; i < (debugStr.length() + 1 + p.x); i++) {
139: System.out.print(" ");
140: }
141: System.out.println("^");
142: break;
143: }
144: ct++;
145: }
146: } catch (Exception e) {
147: e.printStackTrace();
148: System.out.println(e.getMessage());
149: }
150: }
151:
152: }
|