001: /*
002: * Builder.java February 2006
003: *
004: * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General
016: * Public License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
018: * Boston, MA 02111-1307 USA
019: */
020:
021: package simple.page.translate;
022:
023: import simple.page.Workspace;
024: import simple.http.serve.FileContext;
025: import simple.http.serve.Context;
026: import java.io.IOException;
027: import java.io.FileReader;
028: import java.io.Writer;
029: import java.io.File;
030: import java.util.Stack;
031:
032: /**
033: * The <code>Builder</code> object is used to build a document definition
034: * for the specified JSP source file. This performs the lexical analysis
035: * and parsing of the JSP document, and constructs a definition. This
036: * definition contains all details for the JSP source, including the
037: * package name, runtime language, source path, and code segments, which
038: * can be used to generate a source file to compile.
039: *
040: * @author Niall Gallagher
041: */
042: final class Builder {
043:
044: /**
045: * Represents the workspace used to translate and compile sources.
046: */
047: private Workspace project;
048:
049: /**
050: * Constructor for the <code>Builder</code> object. This is used
051: * to build a document definition for specified sources. This will
052: * load all JSP source requested from the workspace source path.
053: *
054: * @param project this is the workspace used by the builder
055: */
056: public Builder(Workspace project) {
057: this .project = project;
058: }
059:
060: /**
061: * This method is used to build the document definition for the
062: * specified JSP source file. This must be given an absolute URI
063: * path, which references a file within the workspace source path.
064: * This method creates the root JSP document definition.
065: *
066: * @param name this is the URI path reference to the source
067: *
068: * @return this returns the document definition for the source
069: */
070: public Definition build(String name) throws IOException {
071: return build(name, new Definition(project, name));
072: }
073:
074: /**
075: * This method is used to add to an existing document definition.
076: * This method is used to build the root JSP document and included
077: * JSP sources. This can be given a relative URI path or an absolute
078: * URI path. For example "../path/File.jsp" can be used. Once the
079: * document and its includes have been evaluated the definition is
080: * returned, which contains all code segments required by the JSP.
081: *
082: * @param name this is the location of the source to be evaluated
083: * @param source this is the document definition to be augmented
084: */
085: public Definition build(String name, Definition source)
086: throws IOException {
087: Stack stack = source.getContext();
088:
089: if (!name.startsWith("/")) {
090: name = "" + stack.peek() + name;
091: }
092: String path = project.getDirectory(name);
093: File file = project.getSourceFile(name);
094:
095: try {
096: source.addInclude(name);
097: stack.push(path);
098: return build(file, source);
099: } finally {
100: stack.pop();
101: }
102: }
103:
104: /**
105: * This method is used to add to an existing document definition.
106: * This method is used to build the root JSP document and included
107: * JSP sources. This can be given a relative URI path or an absolute
108: * URI path. For example "../path/File.jsp" can be used. Once the
109: * document and its includes have been evaluated the definition is
110: * returned, which contains all code segments required by the JSP.
111: *
112: * @param file this is the location of the source to be evaluated
113: * @param source this is the document definition to be augmented
114: */
115: private Definition build(File file, Definition source)
116: throws IOException {
117: Processor out = new Processor(source, this );
118: FileReader data = new FileReader(file);
119: char[] text = new char[512];
120:
121: while (true) {
122: int count = data.read(text);
123: if (count < 0) {
124: break;
125: }
126: out.write(text, 0, count);
127: }
128: out.close();
129: return source;
130: }
131:
132: }
|