package org.apache.torque.generator.source.stream;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.torque.generator.source.SourceElement;
import org.apache.torque.generator.source.SourceException;
import org.apache.torque.generator.source.SourceImpl;
/**
* A source which uses several files as input and combines them into one file.
* The source tree will look as follows:
* <source>
* <file path="path/to/file1">
* <rootOfFile1>
* ...
* </rootOfFile1>
* </file>
* <file path="path/to/file2">
* <rootOfFile2>
* ...
* </rootOfFile2>
* </file>
* ...
* </source>
*/
public class CombinedFileSource extends SourceImpl
{
/** The name of the root element of the produced source tree. */
public static final String ROOT_ELEMENT_NAME = "source";
/** The name of the root element's children of the produced source tree. */
public static final String FILE_ELEMENT_NAME = "file";
/** The name of the path attribute of the file elements. */
public static final String PATH_ATTRIBUTE_NAME = "path";
/**
* The log of the class.
*/
private static Log log = LogFactory.getLog(CombinedFileSource.class);
/**
* The list of contained file sources, not null..
*/
private List<FileSource> fileSources;
/**
* Constructor.
*
* @param fileSources the file sources, not null.
*
* @throws NullPointerException if path or format is null.
*/
public CombinedFileSource(
Collection<FileSource> fileSources)
{
if (fileSources == null)
{
throw new NullPointerException("path must not be null");
}
this.fileSources = new ArrayList<FileSource>(fileSources);
}
/**
* Reads and parses the input file and creates the element tree from it.
*
* @throws SourceException if the input file cannot be read or parsed.
*
* @return the root element of the element tree.
*/
@Override
public SourceElement createRootElement() throws SourceException
{
if (log.isDebugEnabled())
{
log.debug("start creating root Element");
}
SourceElement result = new SourceElement(ROOT_ELEMENT_NAME);
for (FileSource fileSource : fileSources)
{
SourceElement fileElement = new SourceElement(FILE_ELEMENT_NAME);
fileElement.setAttribute(
PATH_ATTRIBUTE_NAME,
fileSource.getPath().getPath());
fileElement.getChildren().add(fileSource.getRootElement());
result.getChildren().add(fileElement);
}
if (log.isDebugEnabled())
{
log.debug("finished creating root Element, source is\n"
+ new SourceToXml().toXml(result, true));
}
return result;
}
/**
* Returns the path of the files as a description.
*
* @return path of the files,separated by a semicolon, not null.
*
* @see org.apache.torque.generator.source.Source#getDescription()
*/
public String getDescription()
{
StringBuilder result = new StringBuilder();
for (FileSource fileSource : fileSources)
{
if (result.length() > 0)
{
result.append(';');
}
result.append(fileSource.getPath());
}
return result.toString();
}
/**
* Returns the source file, if applicable. As no single source file
* exists, the method returns always null.
*
* @return null.
*/
public File getSourceFile()
{
return null;
}
@Override
public String toString()
{
return getDescription();
}
}