001: /*
002: HttpdBase4J: An embeddable Java web server framework that supports HTTP, HTTPS,
003: templated content and serving content from inside a jar or archive.
004: Copyright (C) 2007 Donald Munro
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; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not,see http://www.gnu.org/licenses/lgpl.txt
018: */
019:
020: package net.homeip.donaldm.httpdbase4j;
021:
022: import java.io.BufferedReader;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026:
027: import org.antlr.stringtemplate.StringTemplate;
028: import org.antlr.stringtemplate.StringTemplateGroup;
029:
030: import de.schlichtherle.io.File;
031: import de.schlichtherle.io.FileInputStream;
032:
033: /**
034: * StringTemplateGroup (from org.antlr.stringtemplate) subclass to load templates from
035: * inside an archive file using TrueZip.
036: */
037: public class ArchiveStringTemplateGroup extends StringTemplateGroup
038: //=============================================================
039: {
040:
041: private File m_jarDirectory = null;
042:
043: public ArchiveStringTemplateGroup(String name, File jarDirectory)
044: //-----------------------------------------------------------
045: {
046: super (name,
047: (jarDirectory.getEnclArchive() == null) ? jarDirectory
048: .getAbsolutePath() : jarDirectory
049: .getEnclArchive().getAbsolutePath());
050: m_jarDirectory = jarDirectory;
051: }
052:
053: public ArchiveStringTemplateGroup(String name,
054: java.io.File jarDirectory)
055: //--------------------------------------------------------------------
056: {
057: super (name, jarDirectory.getAbsolutePath());
058: m_jarDirectory = new File(jarDirectory);
059: }
060:
061: public ArchiveStringTemplateGroup(String name,
062: java.io.File archiveFile, String archiveDir)
063: //-----------------------------------------------------------
064: {
065: super (name, archiveFile.getAbsolutePath());
066: m_jarDirectory = new File(archiveFile, archiveDir);
067: }
068:
069: public ArchiveStringTemplateGroup(String name, File archiveFile,
070: String archiveDir)
071: //-----------------------------------------------------------
072: {
073: super (name, archiveFile.getAbsolutePath());
074: m_jarDirectory = new File(archiveFile, archiveDir);
075: }
076:
077: /**
078: * @inheritDoc
079: */
080: @Override
081: protected StringTemplate loadTemplate(String templateName,
082: String fileName)
083: //-----------------------------------------------------------------
084: {
085: String archivePath = (m_jarDirectory.getEnclArchive() == null) ? m_jarDirectory
086: .getAbsolutePath()
087: : m_jarDirectory.getEnclArchive().getAbsolutePath();
088: String archiveDir = "/";
089: int p = m_jarDirectory.getAbsolutePath().indexOf(archivePath);
090: if (p >= 0)
091: archiveDir = m_jarDirectory.getAbsolutePath().substring(
092: p + archivePath.length());
093: if (archiveDir.length() == 0)
094: archiveDir = "/";
095: p = fileName.indexOf(archivePath);
096: if (p >= 0)
097: fileName = fileName.substring(p + archivePath.length());
098: File f = new File(m_jarDirectory, fileName);
099: if (!f.exists()) {
100: System.err.println("WARNING:" + fileName + " not found in "
101: + archivePath);
102: p = fileName.indexOf(archiveDir);
103: if (p >= 0)
104: fileName = fileName.substring(p + archiveDir.length());
105: else
106: fileName = archiveDir + "/" + fileName;
107: f = new File(m_jarDirectory, fileName);
108: if (!f.exists()) {
109: System.err.println("WARNING:" + fileName
110: + " not found in " + archivePath);
111: p = fileName.lastIndexOf(File.separatorChar);
112: if (p < 0)
113: p = fileName.lastIndexOf('/');
114: if ((p >= 0) && (++p < fileName.length())) {
115: fileName = fileName.substring(p);
116: f = new File(m_jarDirectory, fileName);
117: if (!f.exists()) {
118: System.err.println("ERROR:" + fileName
119: + " not found in " + archivePath);
120: return null;
121: }
122: }
123: }
124: }
125: BufferedReader br = null;
126: StringTemplate template = null;
127: InputStream fin = null;
128: InputStreamReader isr = null;
129: try {
130: fin = new FileInputStream(f);
131: isr = getInputStreamReader(fin);
132: br = new BufferedReader(isr);
133: template = loadTemplate(templateName, br);
134: } catch (IOException ioe) {
135: ioe.printStackTrace(System.err);
136: } finally {
137: if (br != null)
138: try {
139: br.close();
140: } catch (Exception e) {
141: }
142: if (isr != null)
143: try {
144: isr.close();
145: } catch (Exception e) {
146: }
147: if (fin != null)
148: try {
149: fin.close();
150: } catch (Exception e) {
151: }
152: }
153: return template;
154: }
155: }
|