001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.page;
018:
019: import java.io.File;
020: import java.io.FileFilter;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.Writer;
027: import java.nio.channels.FileChannel;
028: import java.util.Iterator;
029: import java.util.Map;
030:
031: import javax.xml.parsers.SAXParserFactory;
032: import javax.xml.transform.Transformer;
033: import javax.xml.transform.TransformerConfigurationException;
034: import javax.xml.transform.TransformerException;
035: import javax.xml.transform.TransformerFactory;
036: import javax.xml.transform.sax.SAXTransformerFactory;
037: import javax.xml.transform.stream.StreamResult;
038: import javax.xml.transform.stream.StreamSource;
039:
040: import org.apache.commons.lang.StringUtils;
041: import org.apache.jetspeed.util.DirectoryHelper;
042:
043: /**
044: * @author ddam
045: *
046: */
047: public class DirectoryXMLTransform extends DirectoryHelper {
048: private SAXTransformerFactory transformerFactory;
049:
050: private SAXParserFactory saxFactory;
051:
052: private Map xsltMapping;
053:
054: public DirectoryXMLTransform(File base, Map extensionToXslt) {
055: super (base);
056: this .xsltMapping = extensionToXslt;
057: System.setProperty("javax.xml.transform.TransformerFactory",
058: "org.apache.xalan.processor.TransformerFactoryImpl");
059: System.setProperty("javax.xml.parsers.SAXParserFactory",
060: "org.apache.xerces.jaxp.SAXParserFactoryImpl");
061: System.setProperty("org.xml.sax.driver",
062: "org.apache.xerces.parsers.SAXParser");
063: transformerFactory = (SAXTransformerFactory) TransformerFactory
064: .newInstance();
065: saxFactory = SAXParserFactory.newInstance();
066: saxFactory.setValidating(false);
067:
068: }
069:
070: protected void setBaseDirectory(File directory) {
071: if (!directory.exists()) {
072: directory.mkdirs();
073: }
074:
075: if (!directory.isDirectory()) {
076: throw new IllegalArgumentException(
077: "DirectoryHelper(File) requires directory not a file.");
078: }
079: this .directory = directory;
080:
081: }
082:
083: private Transformer getXSLTForFile(File f) {
084: String extension = StringUtils.substringAfterLast(f.getName(),
085: ".");
086:
087: if (!StringUtils.isEmpty(extension)
088: && xsltMapping.containsKey(extension.toLowerCase())) {
089:
090: Object t_obj = xsltMapping.get(extension.toLowerCase());
091: if (t_obj instanceof Transformer) {
092: return (Transformer) t_obj;
093: }
094: if (t_obj instanceof String) {
095: String t_path = (String) t_obj;
096: Transformer transformer;
097: try {
098: transformer = transformerFactory
099: .newTransformer(new StreamSource(t_path));
100: xsltMapping.put(extension, transformer);
101: return transformer;
102: } catch (TransformerConfigurationException e) {
103:
104: }
105: }
106: }
107:
108: return null;
109: }
110:
111: /**
112: * <p>
113: * copyFrom
114: * </p>
115: *
116: * @see org.apache.jetspeed.util.FileSystemHelper#copyFrom(java.io.File, java.io.FileFilter)
117: * @param directory
118: * @param fileFilter
119: * @throws IOException
120: */
121: public void copyFromAndTransform(File srcDirectory,
122: FileFilter fileFilter) throws IOException {
123: if (!srcDirectory.isDirectory()) {
124: throw new IllegalArgumentException(
125: "DirectoryHelper.copyFrom(File) requires directory not a file.");
126: }
127: copyFilesAndTransform(srcDirectory, directory, fileFilter);
128:
129: }
130:
131: /**
132: *
133: * <p>
134: * copyFiles
135: * </p>
136: *
137: * @param srcDir Source directory to copy from.
138: * @param dstDir Destination directory to copy to.
139: * @throws IOException
140: * @throws FileNotFoundException
141:
142: */
143: protected void copyFilesAndTransform(File srcDir, File dstDir,
144: FileFilter fileFilter) throws IOException {
145: FileChannel srcChannel = null;
146: FileChannel dstChannel = null;
147:
148: try {
149: File[] children = srcDir.listFiles(fileFilter);
150: for (int i = 0; i < children.length; i++) {
151: File child = children[i];
152: if (child.isFile()) {
153: File toFile = new File(dstDir, child.getName());
154:
155: toFile.createNewFile();
156: srcChannel = new FileInputStream(child)
157: .getChannel();
158:
159: Transformer transformer = getXSLTForFile(child);
160: if (transformer != null) {
161: FileOutputStream f_out = new FileOutputStream(
162: toFile);
163: try {
164: transformer.transform(new StreamSource(
165: child), new StreamResult(f_out));
166: f_out.flush();
167: f_out.close();
168: } catch (TransformerException e) {
169: System.out
170: .println("Error transforming file "
171: + child.getCanonicalPath());
172: }
173:
174: } else {
175: dstChannel = new FileOutputStream(toFile)
176: .getChannel();
177: dstChannel.transferFrom(srcChannel, 0,
178: srcChannel.size());
179: dstChannel.close();
180: }
181:
182: srcChannel.close();
183:
184: } else {
185: File newSubDir = new File(dstDir, child.getName());
186: newSubDir.mkdir();
187: copyFilesAndTransform(child, newSubDir, fileFilter);
188: }
189: }
190: } finally {
191: if (srcChannel != null && srcChannel.isOpen()) {
192: try {
193: srcChannel.close();
194: } catch (Exception e) {
195:
196: }
197: }
198: if (dstChannel != null && dstChannel.isOpen()) {
199: try {
200: dstChannel.close();
201: } catch (Exception e) {
202:
203: }
204: }
205: }
206: }
207:
208: public void transform(Transformer transformer, InputStream in,
209: Writer out) throws TransformerException {
210: transformer.transform(new StreamSource(in), new StreamResult(
211: out));
212: }
213: }
|