vgritsenko 2003/06/10 19:59:36
Modified: src/java/org/apache/cocoon Main.java
src/java/org/apache/cocoon/bean CocoonBean.java
Log:
Some rework of the CocoonBean:
* remove setDest() method
* Add toString and hashcode to the Target class
* destDir in Main can be specified either via argument or in the conf file
Revision Changes Path
1.5 +80 -70 cocoon-2.1/src/java/org/apache/cocoon/Main.java
Index: Main.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/Main.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Main.java 4 Jun 2003 19:29:07 -0000 1.4
+++ Main.java 11 Jun 2003 02:59:36 -0000 1.5
@@ -55,25 +55,21 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Iterator;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.DocumentBuilder;
-
import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.CommandLine;
-import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.cocoon.Constants;
-
import org.apache.cocoon.bean.CocoonBean;
/**
@@ -290,21 +286,24 @@
long startTimeMillis = System.currentTimeMillis();
- CocoonBean cocoon = new CocoonBean();
- String destDir = null;
-
Main.setOptions();
- CommandLineParser parser = new PosixParser();
- CommandLine line = parser.parse( options, args );
+ CommandLine line = new PosixParser().parse( options, args );
+ CocoonBean cocoon = new CocoonBean();
if (line.hasOption(HELP_OPT)) {
printUsage();
- }
- if (line.hasOption(VERSION_OPT)) {
+ } else if (line.hasOption(VERSION_OPT)) {
printVersion();
}
+
+ String destDir = null;
+ if (line.hasOption(DEST_DIR_OPT)) {
+ destDir = line.getOptionValue(DEST_DIR_OPT);
+ }
+
if (line.hasOption(XCONF_OPT)) {
- Main.processXConf(cocoon, line.getOptionValue(XCONF_OPT));
+ // destDir from command line overrides one in xconf file
+ destDir = Main.processXConf(cocoon, line.getOptionValue(XCONF_OPT),
destDir);
}
if (line.hasOption(VERBOSE_OPT)) {
cocoon.setVerbose(true);
@@ -312,9 +311,7 @@
if (line.hasOption(PRECOMPILE_ONLY_OPT)) {
cocoon.setPrecompileOnly(true);
}
- if (line.hasOption(DEST_DIR_OPT)) {
- destDir = line.getOptionValue(DEST_DIR_OPT);
- }
+
if (line.hasOption(WORK_DIR_OPT)) {
cocoon.setWorkDir(line.getOptionValue(WORK_DIR_OPT));
}
@@ -346,10 +343,10 @@
cocoon.setBrokenLinkReportFile(line.getOptionValue(BROKEN_LINK_FILE_OPT));
}
if (line.hasOption(URI_FILE_OPT)) {
- cocoon.addTargets(processURIFile(line.getOptionValue(URI_FILE_OPT)));
+ cocoon.addTargets(processURIFile(line.getOptionValue(URI_FILE_OPT)),
destDir);
}
if (line.hasOption(FOLLOW_LINKS_OPT)) {
- cocoon.setFollowLinks(yesno(line.getOptionValue(FOLLOW_LINKS_OPT)));
+ cocoon.setFollowLinks(yesno(line.getOptionValue(FOLLOW_LINKS_OPT)));
}
if (line.hasOption(CONFIRM_EXTENSIONS_OPT)) {
cocoon.setConfirmExtensions(yesno(line.getOptionValue(CONFIRM_EXTENSIONS_OPT, "yes")));
@@ -357,9 +354,8 @@
if (line.hasOption(LOAD_CLASS_OPT)){
cocoon.addLoadedClasses(Arrays.asList(line.getOptionValues(LOAD_CLASS_OPT)));
}
- for (Iterator i = line.getArgList().iterator(); i.hasNext();) {
- cocoon.addTarget((String)i.next(), destDir);
- }
+
+ cocoon.addTargets(line.getArgList(), destDir);
System.out.println(getProlog());
@@ -413,15 +409,13 @@
* @param cocoon a <code>CocoonBean</code> that will be configured by the xconf
file
* @param filename a <code>String</code> value
*/
- private static void processXConf(CocoonBean cocoon, String filename) {
+ private static String processXConf(CocoonBean cocoon, String filename, String
destDir) {
try {
final DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
-
final Document conf = builder.parse(new
File(filename).toURL().toExternalForm());
Node root = conf.getDocumentElement();
-
if (!NODE_ROOT.equals(root.getNodeName())) {
throw new IllegalArgumentException("Expected root node of "+
NODE_ROOT);
}
@@ -438,9 +432,13 @@
if (Main.hasAttribute(root, ATTR_CONFIRM_EXTENSIONS)) {
cocoon.setConfirmExtensions(Main.getBooleanAttributeValue(root,
ATTR_CONFIRM_EXTENSIONS));
}
- NodeList nodes = root.getChildNodes();
- for (int i=0; i<nodes.getLength();i++) {
+ if (destDir == null || destDir.length() == 0) {
+ destDir = getNodeValue(root, NODE_DEST_DIR);
+ }
+
+ NodeList nodes = root.getChildNodes();
+ for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType()== Node.ELEMENT_NODE) {
String nodeName = node.getNodeName();
@@ -460,7 +458,7 @@
cocoon.setConfigFile(getNodeValue(node));
} else if (nodeName.equals(NODE_DEST_DIR)) {
- cocoon.setDestDir(getNodeValue(node));
+ // Ignore
} else if (nodeName.equals(NODE_WORK_DIR)) {
cocoon.setWorkDir(getNodeValue(node));
@@ -475,20 +473,21 @@
cocoon.setDefaultFilename(getNodeValue(node));
} else if (nodeName.equals(NODE_URI)) {
- Main.parseURINode(cocoon, node);
+ Main.parseURINode(cocoon, node, destDir);
} else if (nodeName.equals(NODE_URI_FILE)) {
- cocoon.addTargets(Main.processURIFile(getNodeValue(node)));
+ cocoon.addTargets(Main.processURIFile(getNodeValue(node)),
destDir);
} else {
throw new IllegalArgumentException("Unknown element: " +
nodeName);
}
}
}
-
} catch (Exception e) {
System.out.println("ERROR: "+e.getMessage());
}
+
+ return destDir;
}
private static void parseLoggingNode(CocoonBean cocoon, Node node) throws
IllegalArgumentException {
@@ -525,45 +524,55 @@
throw new IllegalArgumentException("Unexpected children of
"+NODE_LOGGING+" node");
}
}
- private static void parseURINode(CocoonBean cocoon, Node node) throws
IllegalArgumentException {
- String type = null;
- String root = null;
- String src = null;
- String dest = null;
-
- if (node.getAttributes().getLength()==0) {
- cocoon.addTarget(getNodeValue(node));
- return;
- }
- if (Main.hasAttribute(node, ATTR_URI_TYPE)) {
- type = Main.getAttributeValue(node, ATTR_URI_TYPE);
- }
- if (Main.hasAttribute(node, ATTR_URI_SOURCEPREFIX)) {
- root = Main.getAttributeValue(node, ATTR_URI_SOURCEPREFIX);
- }
- if (Main.hasAttribute(node, ATTR_URI_DESTURI)) {
- dest = Main.getAttributeValue(node, ATTR_URI_DESTURI);
- }
- if (Main.hasAttribute(node, ATTR_URI_SOURCEURI)) {
- src = Main.getAttributeValue(node, ATTR_URI_SOURCEURI);
- } else {
- throw new IllegalArgumentException("Missing src attribute on uri node");
+
+ private static void parseURINode(CocoonBean cocoon, Node node, String destDir)
throws IllegalArgumentException {
+ NodeList nodes = node.getChildNodes();
+ if (nodes.getLength() != 0) {
+ throw new IllegalArgumentException("Unexpected children of <" +
NODE_LOGGING + "> node");
}
- if (src == null) {
- throw new IllegalArgumentException("Missing src attribute in <uri>
node");
- } else if (root != null && type!=null & dest!=null) {
- cocoon.addTarget(type, root, src, dest);
- } else if (root!=null & dest!=null) {
- cocoon.addTarget(root, src, dest);
- } else if (dest!=null) {
- cocoon.addTarget(src, dest);
+
+ if (node.getAttributes().getLength() == 0) {
+ cocoon.addTarget(getNodeValue(node), destDir);
} else {
- cocoon.addTarget(src);
+ String src = Main.getAttributeValue(node, ATTR_URI_SOURCEURI);
+
+ String type = null;
+ if (Main.hasAttribute(node, ATTR_URI_TYPE)) {
+ type = Main.getAttributeValue(node, ATTR_URI_TYPE);
+ }
+ String root = null;
+ if (Main.hasAttribute(node, ATTR_URI_SOURCEPREFIX)) {
+ root = Main.getAttributeValue(node, ATTR_URI_SOURCEPREFIX);
+ }
+ String dest = null;
+ if (Main.hasAttribute(node, ATTR_URI_DESTURI)) {
+ dest = Main.getAttributeValue(node, ATTR_URI_DESTURI);
+ }
+
+ if (root != null && type != null & dest != null) {
+ cocoon.addTarget(type, root, src, dest);
+ } else if (root != null & dest != null) {
+ cocoon.addTarget(root, src, dest);
+ } else if (dest != null) {
+ cocoon.addTarget(src, dest);
+ } else {
+ cocoon.addTarget(src, destDir);
+ }
}
- NodeList nodes = node.getChildNodes();
- if (nodes.getLength()!=0) {
- throw new IllegalArgumentException("Unexpected children of
"+NODE_LOGGING+" node");
+ }
+
+ private static String getNodeValue(Node root, String name) throws
IllegalArgumentException {
+ NodeList nodes = root.getChildNodes();
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Node node = nodes.item(i);
+ if (node.getNodeType()== Node.ELEMENT_NODE) {
+ String nodeName = node.getNodeName();
+ if (nodeName.equals(name)) {
+ return getNodeValue(node);
+ }
+ }
}
+ return null;
}
private static String getNodeValue(Node node) throws IllegalArgumentException {
@@ -571,7 +580,7 @@
NodeList children = node.getChildNodes();
boolean found = false;
- for (int i=0; i< children.getLength(); i++) {
+ for (int i = 0; i < children.getLength(); i++) {
Node child = children.item(i);
if (child.getNodeType() == Node.TEXT_NODE) {
s.append(child.getNodeValue());
@@ -590,11 +599,11 @@
NamedNodeMap nodes = node.getAttributes();
if (nodes != null) {
Node attribute = nodes.getNamedItem(attr);
- if (attribute != null) {
+ if (attribute != null && attribute.getNodeValue() != null) {
return attribute.getNodeValue();
}
}
- throw new IllegalArgumentException("Missing "+attr+" attribute");
+ throw new IllegalArgumentException("Missing " + attr + " attribute on <" +
node.getNodeName() + "> node");
}
private static boolean hasAttribute(Node node, String attr) {
@@ -605,6 +614,7 @@
}
return false;
}
+
private static boolean getBooleanAttributeValue(Node node, String attr) {
NamedNodeMap nodes = node.getAttributes();
if (nodes != null) {
1.10 +91 -96 cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java
Index: CocoonBean.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/java/org/apache/cocoon/bean/CocoonBean.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- CocoonBean.java 6 Jun 2003 12:21:10 -0000 1.9
+++ CocoonBean.java 11 Jun 2003 02:59:36 -0000 1.10
@@ -143,7 +143,6 @@
// Internal Objects
private CommandLineContext cliContext;
private Cocoon cocoon;
- private String destDir;
private static Logger log;
private Map attributes;
private HashMap empty;
@@ -162,12 +161,6 @@
public void initialize() throws Exception {
// @todo@ when does the logger get initialised? uv
// @todo@ these should log then throw exceptions back to the caller, not
use system.exit()
- if (destDir.equals("")) {
- String error = "Careful, you must specify a destination dir when using
the -d/--destDir argument";
- log.fatalError(error);
- System.out.println(error);
- System.exit(1);
- }
if (contextDir.equals("")) {
String error = "Careful, you must specify a configuration file when
using the -c/--contextDir argument";
@@ -288,12 +281,11 @@
/**
* Try loading the configuration file from a single location
*/
- private static File tryConfigurationFile(String filename) throws IOException {
- File conf;
+ private static File tryConfigurationFile(String filename) {
if (log.isDebugEnabled()) {
log.debug("Trying configuration file at: " + filename);
}
- conf = new File(filename);
+ File conf = new File(filename);
if (conf.canRead()) {
return conf;
} else {
@@ -405,10 +397,6 @@
this.workDir = workDir;
}
- public void setDestDir(String destDir) {
- this.destDir = destDir;
- }
-
public void setConfigFile(String configFile) {
this.configFile = configFile;
}
@@ -465,26 +453,35 @@
this.classList.addAll(classList);
}
- public void addTarget(String sourceURI) {
- targets.add(new Target(sourceURI, destDir));
- }
-
- public void addTarget(String type, String root, String sourceURI, String
destURI){
+ /**
+ * Adds a target for processing
+ *
+ * @param type Type of target - append, replace, insert.
+ * @param root
+ * @param sourceURI URI of the starting page
+ * @param destURI URI specifying destination for the generated pages.
+ * @throws IllegalArgumentException if destURI is missing
+ */
+ public void addTarget(String type, String root, String sourceURI, String
destURI)
+ throws IllegalArgumentException {
targets.add(new Target(type, root, sourceURI, destURI));
}
- public void addTarget(String type, String sourceURI, String destURI){
+ public void addTarget(String type, String sourceURI, String destURI)
+ throws IllegalArgumentException {
targets.add(new Target(type, sourceURI, destURI));
}
- public void addTarget(String sourceURI, String destURI){
+ public void addTarget(String sourceURI, String destURI)
+ throws IllegalArgumentException {
targets.add(new Target(sourceURI, destURI));
}
- public void addTargets(List uris) {
+ public void addTargets(List uris, String destURI)
+ throws IllegalArgumentException {
Iterator i = uris.iterator();
while (i.hasNext()) {
- Target target = new Target((String)i.next(), destDir);
+ Target target = new Target((String)i.next(), destURI);
targets.add(target);
}
}
@@ -573,37 +570,31 @@
if (!initialized){
initialize();
}
+
attributes = new HashMap();
empty = new HashMap();
allProcessedLinks = new HashMap();
allTranslatedLinks = new HashMap();
- int nCount = 0;
- Target target;
-
- HashMap targetMap = new java.util.HashMap();
+ Map targetMap = new HashMap();
Iterator i = targets.iterator();
while (i.hasNext()) {
- target = (Target)i.next();
- if (!targetMap.containsKey(target.getHashCode())) {
- targetMap.put(target.getHashCode(), target);
- }
+ Target target = (Target)i.next();
+ targetMap.put(target, target);
}
+ int nCount = 0;
while (targetMap.size() > 0) {
- String hashCode = (String)(new ArrayList(targetMap.keySet()).get(0));
- target = (Target)targetMap.get(hashCode);
+ Target target = (Target)targetMap.keySet().iterator().next();
try {
- if (allProcessedLinks.get(hashCode) == null) {
+ if (!allProcessedLinks.containsKey(target)) {
if (precompileOnly) {
- this.processXSP(target.getSourceURI());
+ processXSP(target.getSourceURI());
} else if (this.followLinks) {
i = processTarget(target).iterator();
while (i.hasNext()) {
target = (Target)i.next();
- if (!targetMap.containsKey(target.getHashCode())) {
- targetMap.put(target.getHashCode(), target);
- }
+ targetMap.put(target, target);
}
} else {
processTarget(target);
@@ -613,7 +604,7 @@
printBroken (target.getSourceURI(), rnfe.getMessage());
}
- targetMap.remove(target.getHashCode());
+ targetMap.remove(target);
nCount++;
if (log.isInfoEnabled()) {
@@ -626,7 +617,6 @@
recursivelyPrecompile(context, context);
}
outputBrokenLinks();
-
}
/**
@@ -711,6 +701,7 @@
* processing continues until all processing is complete
* </li>
* </ul>
+ *
* @param target a <code>Target</code> target to process
* @return a <code>Collection</code> containing all links found, as
* Target objects.
@@ -757,7 +748,7 @@
filename = suri;
}
// Store processed URI list to avoid eternal loop
- allProcessedLinks.put(target.getHashCode(),target);
+ allProcessedLinks.put(target, target);
if ("".equals(filename)) {
return new ArrayList();
@@ -1161,15 +1152,17 @@
File[] libraries = root.listFiles();
Arrays.sort(libraries);
for (int i = 0; i < libraries.length; i++) {
- // FIXME: endsWith(".jar") or .zip
- buildClassPath.append(File.pathSeparatorChar)
- .append(IOUtils.getFullFilename(libraries[i]));
+ if (libraries[i].getAbsolutePath().endsWith(".jar")) {
+ buildClassPath.append(File.pathSeparatorChar)
+ .append(IOUtils.getFullFilename(libraries[i]));
+ }
}
}
buildClassPath.append(File.pathSeparatorChar)
.append(System.getProperty("java.class.path"));
+ // Extra class path is necessary for non-classloader-aware java compilers
to compile XSPs
// buildClassPath.append(File.pathSeparatorChar)
// .append(getExtraClassPath(context));
@@ -1180,31 +1173,41 @@
}
public class Target {
- private static final String DEFAULT_TYPE = "default";
- private static final String REPLACE_TYPE = "replace";
+ // Defult type is append
private static final String APPEND_TYPE = "append";
+ private static final String REPLACE_TYPE = "replace";
private static final String INSERT_TYPE = "insert";
- private String type;
- private String root;
- private String sourceURI;
- private String destURI;
- public Target(String type, String root, String sourceURI, String destURI){
+ private final String type;
+ private final String root;
+ private final String sourceURI;
+ private final String destURI;
+
+ private transient int _hashCode;
+ private transient String _toString;
+
+ public Target(String type, String root, String sourceURI, String destURI)
throws IllegalArgumentException {
this.type = type;
this.root = root;
this.sourceURI = NetUtils.normalize(sourceURI);
+ if (destURI == null || destURI.length() == 0) {
+ throw new IllegalArgumentException("You must specify a destination
directory when defining a target");
+ }
+ if (!destURI.endsWith("/")) {
+ destURI += "/";
+ }
this.destURI = destURI;
}
- public Target(String type, String sourceURI, String destURI){
+ public Target(String type, String sourceURI, String destURI) throws
IllegalArgumentException {
this(type, "", sourceURI, destURI);
}
- public Target(String sourceURI, String destURI){
- this(DEFAULT_TYPE, "", sourceURI, destURI);
+ public Target(String sourceURI, String destURI) throws
IllegalArgumentException {
+ this(APPEND_TYPE, "", sourceURI, destURI);
}
- public Target getDerivedTarget(String newURI) throws ProcessingException {
+ public Target getDerivedTarget(String newURI) throws
IllegalArgumentException {
if (!newURI.startsWith(root)) {
return null;
}
@@ -1218,62 +1221,54 @@
}
actualSourceURI = actualSourceURI.substring(root.length());
- if (DEFAULT_TYPE.equals(this.type)){
- return this.getFinalURIWithDefault(actualSourceURI);
- }
- else if (REPLACE_TYPE.equals(this.type)){
- return this.getFinalURIWithReplace(actualSourceURI);
- }
- else if (APPEND_TYPE.equals(this.type)){
- return this.getFinalURIWithAppend(actualSourceURI);
- }
- else if (INSERT_TYPE.equals(this.type)){
- return this.getFinalURIWithInsert(actualSourceURI);
+ if (APPEND_TYPE.equals(this.type)){
+ return destURI + actualSourceURI;
+ } else if (REPLACE_TYPE.equals(this.type)){
+ return destURI;
+ } else if (INSERT_TYPE.equals(this.type)){
+ int starPos = destURI.indexOf("*");
+ if (starPos == -1) {
+ throw new ProcessingException("Missing * in replace mapper
uri");
+ } else if (starPos == destURI.length()-1) {
+ return destURI.substring(0,starPos) + actualSourceURI;
+ } else {
+ return destURI.substring(0,starPos) + actualSourceURI +
destURI.substring(starPos+1);
+ }
} else {
throw new ProcessingException("Unknown mapper type: " + this.type);
}
}
- private String getFinalURIWithDefault(String actualSourceURI){
- return destDir + "/" + actualSourceURI;
- }
-
- private String getFinalURIWithAppend(String actualSourceURI){
- return destURI + actualSourceURI;
- }
-
- private String getFinalURIWithReplace(String actualSourceURI){
- return destURI;
- }
-
- private String getFinalURIWithInsert(String actualSourceURI) throws
ProcessingException {
- int starPos = destURI.indexOf("*");
- if (starPos == -1) {
- throw new ProcessingException("Missing * in replace mapper uri");
- } else if (starPos == destURI.length()-1) {
- return destURI.substring(0,starPos) + actualSourceURI;
- } else {
- return destURI.substring(0,starPos) + actualSourceURI +
destURI.substring(starPos+1);
- }
- }
-
public String getSourceURI() {
return root + sourceURI;
}
- // @todo@ this is misusing the 'hashCode' name - hashCodes should be
integer it seems, uv
- public String getHashCode() {
- return type + "|" + root +"|" + sourceURI + "|" + destURI;
- }
-
public OutputStream getOutputStream(String filename) throws IOException,
ProcessingException {
final String finalDestinationURI = this.getFinalURI(filename);
- Source src = (Source) sourceResolver.resolveURI(finalDestinationURI);
+ Source src = sourceResolver.resolveURI(finalDestinationURI);
if (!(src instanceof ModifiableSource)) {
throw new ProcessingException("Source is not Modifiable: " +
finalDestinationURI);
}
ModifiableSource outputSource = (ModifiableSource) src;
return outputSource.getOutputStream();
+ }
+
+ public boolean equals(Object o) {
+ return (o instanceof Target) && o.toString().equals(toString());
+ }
+
+ public int hashCode() {
+ if (_hashCode == 0) {
+ return _hashCode = toString().hashCode();
+ }
+ return _hashCode;
+ }
+
+ public String toString() {
+ if (_toString == null) {
+ return _toString = "<" + type + "|" + root +"|" + sourceURI + "|" +
destURI + ">";
+ }
+ return _toString;
}
}
}