001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/devtools/GetTextFromXML.java $
003: * $Id: GetTextFromXML.java 17070 2006-10-12 00:07:52Z ktsao@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.devtools;
021:
022: import java.io.BufferedReader;
023: import java.io.File;
024: import java.io.FileNotFoundException;
025: import java.io.FileReader;
026: import java.io.IOException;
027: import java.util.StringTokenizer;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.sakaiproject.tool.assessment.util.StringParseUtils;
033:
034: /**
035: *
036: * <p>Description: Get text delimited by "<" and ">" as in XML and HTML</p>
037: * <p>Copyright: Copyright (c) 2004</p>
038: * <p>Company: </p>
039: * @author Ed Smiley
040: * @version $Id: GetTextFromXML.java 17070 2006-10-12 00:07:52Z ktsao@stanford.edu $
041: */
042:
043: public class GetTextFromXML {
044:
045: private static Log log = LogFactory.getLog(GetTextFromXML.class);
046:
047: private static String dirName; // JSF source directory
048: private static File file;
049: private static String SEP = "<>";
050: private static String ends = "jsp";
051:
052: public static void main(String[] args) {
053: if (args.length == 0) {
054: //log.info("no argument!");
055: System.exit(0);
056: }
057: dirName = args[0];
058:
059: try {
060: file = new File(dirName);
061: if (!file.isDirectory()) {
062: throw new RuntimeException(args[0] + " not a directory");
063: }
064:
065: File[] list = file.listFiles();
066: boolean displayToken = true;
067: for (int i = 0; i < list.length; i++) {
068: File src = list[i];
069: if (src.isDirectory())
070: continue;
071:
072: StringTokenizer st = new StringTokenizer(
073: getContents(src), SEP, true);
074: while (st.hasMoreElements()) {
075: String tok = st.nextToken();
076: if (tok.equals("<")) {
077: displayToken = false;
078: } else if (tok.equals(">")) {
079: displayToken = true;
080: continue;
081: }
082:
083: tok = tok.trim();
084:
085: if (displayToken && tok.length() > 0) {
086: //log.info(getVarName(tok) + "=" + tok);
087: }
088: }
089:
090: }
091:
092: } catch (RuntimeException ex) {
093: //log.info(ex);
094: System.exit(0);
095: }
096:
097: }
098:
099: /**
100: * compute a variable name
101: * @param value String
102: * @return String
103: */
104: public static String getVarName(String value) {
105: return StringParseUtils.simplifyString(value, 20, 20);
106: }
107:
108: /**
109: * return a String containing the file
110: * @return String
111: */
112: private static String getContents(File file) {
113: String contents = "";
114: String line = "";
115: BufferedReader br = null;
116: FileReader fr = null;
117: try {
118: fr = new FileReader(file);
119: br = new BufferedReader(fr);
120: while (line != null) {
121: line = br.readLine();
122: contents += line + "\n";
123: }
124: } catch (FileNotFoundException e) {
125: log.warn(e.getMessage());
126: } catch (IOException e) {
127: log.warn(e.getMessage());
128: } finally {
129: if (br != null) {
130: try {
131: br.close();
132: } catch (IOException ex1) {
133: log.warn(ex1.getMessage());
134: }
135: }
136: if (fr != null) {
137: try {
138: fr.close();
139: } catch (IOException ex1) {
140: log.warn(ex1.getMessage());
141: }
142: }
143: }
144: return contents;
145:
146: }
147:
148: }
|