db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r910600 [10/29] - in /db/torque/torque4/trunk: maven-torque-gf-plugin/ maven-torque-gf-plugin/src/ maven-torque-gf-plugin/src/main/ maven-torque-gf-plugin/src/main/java/ maven-torque-gf-plugin/src/main/java/org/ maven-torque-gf-plugin/src/m...
Date Tue, 16 Feb 2010 17:16:02 GMT
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/WrapReservedJavaWords.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/WrapReservedJavaWords.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/WrapReservedJavaWords.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/WrapReservedJavaWords.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,150 @@
+package org.apache.torque.gf.processor.string;
+
+/*
+ * 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.util.HashSet;
+import java.util.Set;
+
+/**
+ * Processes an input String as follows: If the input String is a reserved word
+ * in java, a suffix and/or prefix is appended to the input String and the
+ * result is returned; otherwise the input string is returned unchanged.
+ */
+public class WrapReservedJavaWords implements StringProcessor
+{
+    /**
+     * The prefix which should be prepended to a reserved word, not null.
+     */
+    private String prependWhenReserved = "_";
+
+    /**
+     * The suffix which should be prepended to a reserved word, not null.
+     */
+    private String appendWhenReserved = "";
+
+    /**
+     * the set of reserved words.
+     */
+    private static final Set<String> RESERVED_WORDS = new HashSet<String>();
+
+    static
+    {
+        RESERVED_WORDS.add("abstract");
+        RESERVED_WORDS.add("assert");
+        RESERVED_WORDS.add("boolean");
+        RESERVED_WORDS.add("break");
+        RESERVED_WORDS.add("byte");
+        RESERVED_WORDS.add("case");
+        RESERVED_WORDS.add("catch");
+        RESERVED_WORDS.add("char");
+        RESERVED_WORDS.add("class");
+        RESERVED_WORDS.add("const");
+        RESERVED_WORDS.add("continue");
+        RESERVED_WORDS.add("default");
+        RESERVED_WORDS.add("do");
+        RESERVED_WORDS.add("double");
+        RESERVED_WORDS.add("else");
+        RESERVED_WORDS.add("enum");
+        RESERVED_WORDS.add("extends");
+        RESERVED_WORDS.add("final");
+        RESERVED_WORDS.add("finally");
+        RESERVED_WORDS.add("float");
+        RESERVED_WORDS.add("for");
+        RESERVED_WORDS.add("goto");
+        RESERVED_WORDS.add("if");
+        RESERVED_WORDS.add("implements");
+        RESERVED_WORDS.add("import");
+        RESERVED_WORDS.add("instanceof");
+        RESERVED_WORDS.add("int");
+        RESERVED_WORDS.add("interface");
+        RESERVED_WORDS.add("long");
+        RESERVED_WORDS.add("native");
+        RESERVED_WORDS.add("new");
+        RESERVED_WORDS.add("package");
+        RESERVED_WORDS.add("private");
+        RESERVED_WORDS.add("protected");
+        RESERVED_WORDS.add("public");
+        RESERVED_WORDS.add("return");
+        RESERVED_WORDS.add("short");
+        RESERVED_WORDS.add("static");
+        RESERVED_WORDS.add("strictfp");
+        RESERVED_WORDS.add("super");
+        RESERVED_WORDS.add("switch");
+        RESERVED_WORDS.add("synchronized");
+        RESERVED_WORDS.add("this");
+        RESERVED_WORDS.add("throw");
+        RESERVED_WORDS.add("throws");
+        RESERVED_WORDS.add("transient");
+        RESERVED_WORDS.add("try");
+        RESERVED_WORDS.add("void");
+        RESERVED_WORDS.add("volatile");
+        RESERVED_WORDS.add("while");
+    }
+
+    /**
+     * Sets the prefix to be prepended if the input is a reserved word.
+     *
+     * @param prefix the new prefix, not null.
+     *
+     * @throws NullPointerException if prefix is null.
+     */
+    public void setPrependWhenReserved(String prefix)
+    {
+        if (prefix == null)
+        {
+            throw new NullPointerException("prefix must not be null");
+        }
+        this.prependWhenReserved = prefix;
+    }
+
+    /**
+     * Sets the suffix to be appended if the input is a reserved word.
+     *
+     * @param suffix the new suffix, not null.
+     *
+     * @throws NullPointerException if suffix is null.
+     */
+    public void setAppendWhenReserved(String suffix)
+    {
+        if (suffix == null)
+        {
+            throw new NullPointerException("suffix must not be null");
+        }
+        this.appendWhenReserved = suffix;
+    }
+
+    /**
+     * Checks whether the input is a reserved java word. If yes,
+     * prefix and suffix are prepended and appended, and the result is returned.
+     * If no, the input is returned unchanged.
+     *
+     * @param toProcess the input.
+     *
+     * @return the output.
+     */
+    public String process(String toProcess)
+    {
+        if (RESERVED_WORDS.contains(toProcess))
+        {
+            return prependWhenReserved + toProcess + appendWhenReserved;
+        }
+        return toProcess;
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/package.html
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/package.html?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/package.html (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/processor/string/package.html Tue Feb 16 17:15:43 2010
@@ -0,0 +1,25 @@
+<!--
+ Copyright 2001-2006 The Apache Software Foundation.
+
+ Licensed 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.
+-->
+<html>
+  <head>
+    <title>Torque-gf string processors</title>
+  </head>
+  <body>
+    <p>
+      This package contains classes which transform a sting into another string.
+    </p>
+  </body>
+</html>

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/Namespace.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/Namespace.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/Namespace.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/Namespace.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,297 @@
+package org.apache.torque.gf.qname;
+
+/*
+ * 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.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * An instance of this class represents a hierarchical namespace. The hierarchy
+ * parts are separated by dots.
+ *
+ * A namespace is in another namespace if it starts with all the components
+ * of the the other namespace (it may contain other components afterwards).
+ * For example, the namespace &quot;org.apache.torque&quot; is in the namespace
+ * &quot;org.apache&quot;.
+ *
+ * Note that the components and their order need to be equal, it does not
+ * suffice if a namespace starts with another namespace. For example,
+ * the namespace &quot;org.apache.torque&quot; is not in the namespace
+ * &quot;org.ap&quot;.
+ *
+ * Instances of this class are immutable. To guard against mutable subclasses,
+ * this class is final.
+ */
+public final class Namespace
+{
+    /**
+     * The separator between the hierachical parts of a namespace.
+     */
+    public static final char SEPARATOR = '.';
+
+    /**
+     * The root namespace.
+     */
+    public static final Namespace ROOT_NAMESPACE
+            = new Namespace(StringUtils.EMPTY);
+
+    /**
+     * The String representation of the namespace. Is never null.
+     */
+    private String namespace;
+
+    /**
+     * Constructs a namespace from its string representation.
+     * @param namespace teh string representation of the namespace.
+     *        May not be null, and may not contain colons(:).
+     *
+     * @throws NullPointerException if namespace is null.
+     * @throws IllegalArgumentException if namespace contains colons.
+     */
+    public Namespace(String namespace)
+    {
+        if (namespace == null)
+        {
+            throw new NullPointerException("namespace must not be null");
+        }
+        this.namespace = namespace;
+    }
+
+    /**
+     * Copy-Contructor.
+     *
+     * @param toCopy the namespace to copy, not null.
+     * @throws NullPointerException if toCopy is null.
+     */
+    public Namespace(Namespace toCopy)
+    {
+        if (toCopy == null)
+        {
+            throw new NullPointerException("toCopy must not be null");
+        }
+        this.namespace = toCopy.namespace;
+    }
+
+    /**
+     * Creates a namespace from a hierarchical List of namespace parts.
+     *
+     * @param namespaceParts the parts of the namespace.
+     */
+    public Namespace(List<String> namespaceParts)
+    {
+        if (namespaceParts == null)
+        {
+            throw new NullPointerException("namespaceParts must not be null");
+        }
+        StringBuffer assembledNamespace = new StringBuffer();
+        for (Iterator<String> partIt
+                = namespaceParts.iterator(); partIt.hasNext();)
+        {
+            String part = partIt.next();
+
+            assembledNamespace.append(part);
+            if (partIt.hasNext())
+            {
+                assembledNamespace.append(SEPARATOR);
+            }
+        }
+        this.namespace = assembledNamespace.toString();
+    }
+
+
+    /**
+     * Returns the parts of the namespace in hierachical order.
+     * The most significant part, i.e the leftmost side, is returned first.
+     *
+     * @return the parts of the namespace, never null. An empty list
+     *         is returned for the root namespace.
+     */
+    public List<String> getParts()
+    {
+        if (StringUtils.EMPTY.equals(namespace))
+        {
+            return new ArrayList<String>();
+        }
+        String[] partArray
+                = namespace.split("\\" + SEPARATOR);
+        return Arrays.asList(partArray);
+    }
+
+    /**
+     * Returns the parent of the given namespace. If this namespace's
+     * parent namespace is the root namespace, or this namespace is the root
+     * namespace, the root namespace is returned.
+     *
+     * @return the parent namespace of the namespace, never null
+     */
+    public Namespace getParent()
+    {
+        int separatorPos = namespace.lastIndexOf(SEPARATOR);
+        if (separatorPos == -1)
+        {
+            return ROOT_NAMESPACE;
+        }
+
+        String parentNamespace = namespace.substring(0, separatorPos);
+        return new Namespace(parentNamespace);
+    }
+
+    /**
+     * Returns if this namespace is visible to another namespace.
+     * This is true if this namespace is a "child" of the other namespace
+     * or equal to the other namespace.
+     * Note that for being a child, all the parts of the namespace separated
+     * by a dot must be equal.
+     * For example, &quot;org.apache.torque&quot; is visible to the namespace
+     * quot;org.apache&quot; and &quot;org&quot; but not to
+     * &quot;org.ap&quot;, as the second parts, &quot;apache&quot; and
+     * &quot;ap&quot; are not equal.
+     *
+     * @param otherNamespace the namespace against this namespace
+     *        should be checked, not null.
+     * @return true if this namespace is visible to the given namespace,
+     *         false otherwise.
+     * @throws NullPointerException if otherNamespace is null.
+     */
+    public boolean isVisibleTo(Namespace otherNamespace)
+    {
+        if (otherNamespace == null)
+        {
+            throw new NullPointerException("otherNamespace must not be null");
+        }
+        // All namespaces are in the root namespace.
+        if (ROOT_NAMESPACE.equals(otherNamespace))
+        {
+            return true;
+        }
+
+        // The root namespace is not contained in an non-root namespace.
+        if (ROOT_NAMESPACE.equals(this))
+        {
+            return false;
+        }
+
+        if (!this.namespace.startsWith(otherNamespace.namespace))
+        {
+            return false;
+        }
+
+        if (this.namespace.equals(otherNamespace.namespace))
+        {
+            return true;
+        }
+
+        if (this.namespace.charAt(otherNamespace.namespace.length())
+                == SEPARATOR)
+        {
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * Returns if this namespace is visible from another namespace.
+     * This is true if the other namespace is a "child" of this namespace.
+     * Note that for being a child, all the parts of the namespace separated
+     * by a dot must be equal.
+     * For example, &quot;org.apache.torque&quot; is visible from the namespace
+     * quot;org.apache.torque.generator&quot;, but not from
+     * &quot;org.apache&quot.
+     *
+     * @param otherNamespace the namespace against this namespace
+     *        should be checked, not null.
+     * @return true if this namespace is visible from the other namespace,
+     *         false otherwise.
+     * @throws NullPointerException if otherNamespace is null.
+     */
+    public boolean isVisibleFrom(Namespace otherNamespace)
+    {
+        if (otherNamespace == null)
+        {
+            throw new NullPointerException("otherNamespace is null "
+                    + "(this namespace is " + this + ")");
+        }
+        return otherNamespace.isVisibleTo(this);
+    }
+
+    /**
+     * Returns whether this namespace is the root namespace.
+     *
+     * @return true if this namespace is the root namespace, false otherwise.
+     */
+    public boolean isRoot()
+    {
+        return ROOT_NAMESPACE.namespace.equals(namespace);
+    }
+
+    /**
+     * Returns if this object is equal to another object.
+     * This is true if and only if the other object is a namespace, and their
+     * string representations are equal.
+     *
+     * @param o the object to check equality.
+     * @return true if the object is equal to this namespace, false otherwise.
+     *
+     * @see java.lang.Object#equals(Object)
+     */
+    @Override
+    public boolean equals(Object o)
+    {
+        if (!(o instanceof Namespace))
+        {
+            return false;
+        }
+
+        Namespace otherNamespace = (Namespace) o;
+        return otherNamespace.namespace.equals(this.namespace);
+    }
+
+    /**
+     * Returns a hash code for this namespace. The hash code is consistent
+     * with <code>equals()</code>.
+     *
+     * @return a hach code for this object.
+     *
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode()
+    {
+        return namespace.hashCode();
+    }
+
+    /**
+     * Returns a String representation of this namespace.
+     *
+     * @return a String representation of this namespace.
+     *
+     * @see java.lang.Object#toString()
+     */
+    @Override
+    public String toString()
+    {
+        return namespace;
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedName.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedName.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedName.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedName.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,298 @@
+package org.apache.torque.gf.qname;
+
+/*
+ * 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 org.apache.commons.lang.StringUtils;
+
+
+/**
+ * This class contains a name which has a namespace. The namespace defines the
+ * scope where the name is valid, and is made up of hierarchical bits
+ * separated by a dot (<code>SEPARATOR<code>).
+ * A qualified name is in the scope of its own namespace and all children
+ * namespaces of its own namespace (i.e. the namespaces "deeper down"
+ * the hierarchy). For example, the namespace org.apache.torque
+ * is in the scope of org.apache but not in the scope of org.apache.torque.gf.
+ * As a special case, if the namespace is the empty String,
+ * the name is valid in all namespaces.
+ *
+ * The namespace and the name are also separated by a dot
+ * (<code>SEPARATOR<code>).
+ *
+ * Instances of this class are immutable. To prevent mutable subclasses,
+ * this class is final.
+ */
+public final class QualifiedName
+{
+    /**
+     * The separator between namespace parts and between namespacee
+     * and local name in the string representation of a QualifiedName.
+     */
+    public static final char SEPARATOR = '.';
+
+    /**
+     * The local name part of the qualifiedName. Is never null or empty.
+     */
+    private String name;
+
+    /**
+     * The namespace of the qualifiedName. Is never null.
+     */
+    private Namespace namespace;
+
+    /**
+     * Creates a QualifiedName from its string representation, which is
+     * namespace:name. The namespace part may be empty (in which case
+     * the default namespace is used), but not the name part.
+     *
+     * @param qualifiedName the string representation of the QualifiedName,
+     *        must contain a non-empty name part.
+     * @throws NullPointerException if qualifiedName is null
+     */
+    public QualifiedName(String qualifiedName)
+    {
+        this(qualifiedName, (Namespace) null);
+    }
+
+    /**
+     * Creates a QualifiedName from a fully qualified name or the name and the
+     * namespace parts of a QualifiedName.
+     *
+     * @param nameOrQualifiedName either the name part of the QualifiedName,
+     *        or a fully qualified name. May not be null and must contain
+     *        an non-empty name part.
+     * @param defaultNamespace the namespace of the QualifiedName,
+     *        may be null if nameOrQualifiedName has a namespace part.
+     *
+     * @throws IllegalArgumentException if name has an empty name part, or
+     *         if no namespace is found (nameOrQualifiedName has no namespace
+     *         part and defaultNamespace is null)
+     * @throw NullPointerException if name is null.
+     */
+    public QualifiedName(String nameOrQualifiedName, String defaultNamespace)
+    {
+        this(nameOrQualifiedName,
+                defaultNamespace != null
+                    ? new Namespace(defaultNamespace)
+                    : null);
+   }
+
+    /**
+     * Creates a QualifiedName from a fully qualified name or the name and the
+     * namespace parts of a QualifiedName.
+     *
+     * @param nameOrQualifiedName either the name part of the QualifiedName,
+     *        or a fully qualified name. May not be null and must contain
+     *        an non-empty name part.
+     * @param defaultNamespace the namespace of the QualifiedName,
+     *        may be null if nameOrQualifiedName has a namespace part.
+     *
+     * @throws IllegalArgumentException if name has an empty name part, or
+     *         if no namespace is found (nameOrQualifiedName has no namespace
+     *         part and defaultNamespace is null)
+     * @throw NullPointerException if name is null.
+     */
+    public QualifiedName(String nameOrQualifiedName, Namespace defaultNamespace)
+    {
+        if (nameOrQualifiedName == null)
+        {
+            throw new NullPointerException(
+                    "nameOrQualifiedName must not be null");
+        }
+        int dotIndex = nameOrQualifiedName.lastIndexOf(SEPARATOR);
+        if (dotIndex == -1)
+        {
+            if (defaultNamespace == null)
+            {
+                defaultNamespace = Namespace.ROOT_NAMESPACE;
+            }
+            setName(nameOrQualifiedName);
+            this.namespace = defaultNamespace;
+        }
+        else
+        {
+            setName(nameOrQualifiedName.substring(dotIndex + 1).trim());
+            String namespacePart
+                    = nameOrQualifiedName.substring(0, dotIndex).trim();
+            setNamespace(namespacePart);
+        }
+    }
+
+    /**
+     * Returns the name part of the QualifiedName.
+     *
+     * @return the name part of the QualifiedName, never null.
+     */
+    public String getName()
+    {
+        return name;
+    }
+
+    /**
+     * Sets the name part of this Qualified name.
+     * This method is private because this object is immutable.
+     *
+     * @param name the name, not blank,
+     *        must not contain a dot (<code>SEPARATOR</code>).
+     */
+    private void setName(String name)
+    {
+        if (name.indexOf(SEPARATOR) != -1)
+        {
+            throw new IllegalArgumentException(
+                    "name \"" + name + "\" must not contain "
+                        + SEPARATOR);
+        }
+        if (StringUtils.isBlank(name))
+        {
+            throw new IllegalArgumentException("name must not be blank");
+        }
+        this.name = name;
+    }
+
+    /**
+     * Sets the namespace part of this Qualified name.
+     * This method is private because this object is immutable.
+     *
+     * @param namespace the name, not null,
+     *        must not contain <code>NAMESPACE_NAME_SEPARATOR</code>
+     */
+    private void setNamespace(String namespace)
+    {
+        this.namespace = new Namespace(namespace);
+    }
+
+    /**
+     * Returns the namespace of the QualifiedName.
+     *
+     * @return the namespace of the QualifiedName, may be null.
+     */
+    public Namespace getNamespace()
+    {
+        return namespace;
+    }
+
+    /**
+     * Returns if this qualified name is visible from another namespace.
+     * This is true if the namespace is a "child" of this
+     * qualified name's  namespace.
+     * Note that for being a child, all the parts of the namespace separated
+     * by a dot must be equal.
+     * For example, &quot;org.apache.torque:name&quot; is visible from the
+     * namespaces quot;org.apache.torque.generator&quot; and
+     * quot;org.apache.torque&quot;, but not from &quot;org.apache&quot.
+     *
+     * @param otherNamespace the namespace against this QualifiedName
+     *        should be checked, not null.
+     *
+     * @return true if this QualifiedName is visible from the given namespace,
+     *         false otherwise.
+     *
+     * @throws NullPointerException if otherNamespace is null.
+     */
+    public boolean isVisibleFrom(Namespace otherNamespace)
+    {
+        return namespace.isVisibleFrom(otherNamespace);
+    }
+
+    /**
+     * Returns the parent of the Qualified name's namespace.
+     * If this qualified name is in the root namespace, the root namespace
+     * is returned.
+     *
+     * @return the parent namespace of the qualified name's namespace,
+     *         never null.
+     *
+     * @see Namespace#getParent()
+     */
+    public Namespace getParentNamespace()
+    {
+        return namespace.getParent();
+    }
+
+    /**
+     * Returns if the namespace of this qualified name is the root namespace.
+     *
+     * @return true if the namespace is the root namespace, false otherwise.
+     */
+    public boolean isInRootNamespace()
+    {
+        return namespace.isRoot();
+    }
+
+    /**
+     * Returns the String representation of the QualifiedName,
+     * which is namespace.name if namespace is not empty,
+     * or name if namespace is empty.
+     *
+     * @return a String representation of the QualifiedName.
+     */
+    @Override
+    public String toString()
+    {
+        if (isInRootNamespace())
+        {
+            return name;
+        }
+        return namespace.toString() + SEPARATOR + name;
+    }
+
+    /**
+     * Returns if this object equals another object. This is the case if
+     * the other object is also a QualifiedName and its name and namespace
+     * are the same as this object's name and namespace.
+     *
+     * @param o the other object to compare this object to.
+     * @return true if this object equals the other object, false otherwise.
+     *
+     * @see java.lang.Object#equals(Object)
+     */
+    @Override
+    public boolean equals(Object o)
+    {
+        if (!(o instanceof QualifiedName))
+        {
+            return false;
+        }
+
+        QualifiedName qualifiedName = (QualifiedName) o;
+
+        if (!qualifiedName.name.equals(name))
+        {
+            return false;
+        }
+
+        return qualifiedName.namespace.equals(namespace);
+    }
+
+    /**
+     * Returns a hashcode for the QualifiedName. The hashcode is consistent
+     * with equals.
+     *
+     * @return a hashcode for the qualified name.
+     *
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode()
+    {
+        return toString().hashCode();
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedNameMap.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedNameMap.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedNameMap.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/QualifiedNameMap.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,528 @@
+package org.apache.torque.gf.qname;
+
+/*
+ * 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.Serializable;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * A special map with a QualifiedName as a key. This map acts as a normal map,
+ * except that it has an additional method, <code>getInNamespace()</code>,
+ * which respects the visibility rules in the namespace hierarchy. Null cannot
+ * be stored as a key for this map.
+ *
+ * @param <T> The class of the object which should be stored in the map.
+ */
+public class QualifiedNameMap<T>
+    implements Map<QualifiedName, T>, Serializable
+{
+    /**
+     * SerialVersionUid for serializing.
+     */
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * The backing hashMap. Must not contain a mapping for the key null,
+     * because null as a qualified name does not make sense.
+     */
+    private HashMap<QualifiedName, T> map;
+
+    /**
+     * Constructs an empty QualifiedNameMap.
+     */
+    public QualifiedNameMap()
+    {
+        map = new HashMap<QualifiedName, T>();
+    }
+
+    /**
+     * Constructs a shallow copy of the supplied map.
+     *
+     * @param map the map to copy, not null.
+     */
+    public QualifiedNameMap(Map<? extends QualifiedName, ? extends T> map)
+    {
+        if (map.containsKey(null))
+        {
+            throw new IllegalArgumentException(
+                    "map must not contain a mapping for the key null");
+        }
+        this.map = new HashMap<QualifiedName, T>(map);
+    }
+
+    /**
+     * Returns the most specific entry in the map which is visible
+     * for the provided key. I.e. not only the namespace of the provided
+     * QualifiedName is searched, but also all the parent namespaces of the
+     * provided QualifiedName.
+     *
+     * Note that if the most specific matching key is mapped to null,
+     * then null is returned, even if another less specific entry exists
+     * which is not mapped to null.
+     *
+     * If only exact matches for the QualifiedName should be returned,
+     * use the <code>get(Object)</code> method.
+     *
+     * @param key the qualified name for which the entries in the map should
+     *        be searched, not null.
+     * @return The entry which is visible to the supplied QualifiedName,
+     *         or null if no such entry exists.
+     *         If more such entries exist, the most specific one
+     *         (i.e. the one with the longest namespace) is returned.
+     *
+     * @throws NullPointerException if key is null.
+     *
+     * @see #get(Object)
+     */
+    public T getInHierarchy(QualifiedName key)
+    {
+       while (true)
+        {
+            if (map.containsKey(key))
+            {
+                return get(key);
+            }
+
+            // the default namespace is not a part of any other namespace,
+            // whe have reached the end of the recursion
+            if (Namespace.ROOT_NAMESPACE.equals(key.getNamespace()))
+            {
+                return null;
+            }
+
+            key = new QualifiedName(
+                    key.getName(),
+                    key.getNamespace().getParent());
+        }
+    }
+
+    /**
+     * Returns the most specific key in the map which is visible
+     * for the provided key. I.e. not only the namespace of the provided
+     * QualifiedName is searched, but also all the parent namespaces of the
+     * provided QualifiedName. If no such key exists, null is returned.
+     *
+     * @param key the qualified name for which the entries in the map should
+     *        be searched, not null.
+     * @return The key which is visible to the supplied QualifiedName,
+     *         or null if no such key exists.
+     *         If more such keys exist, the most specific one
+     *         (i.e. the one with the longest namespace) is returned.
+     *
+     * @throws NullPointerException if key is null.
+     */
+    public QualifiedName getKeyInHierarchy(QualifiedName key)
+    {
+       while (true)
+        {
+            if (map.containsKey(key))
+            {
+                return key;
+            }
+
+            // the default namespace is not a part of any other namespace,
+            // we have reached the end of the recursion
+            if (Namespace.ROOT_NAMESPACE.equals(key.getNamespace()))
+            {
+                return null;
+            }
+
+            key = new QualifiedName(
+                    key.getName(),
+                    key.getNamespace().getParent());
+        }
+    }
+
+
+    /**
+     * Returns all mappings which live in the given namespace.
+     * If one mapping hides another mapping, i.e. if one mapping
+     * is a more specialized version of another, both
+     * mappings are present in the returned map.
+     *
+     * For example, if the map contains mappings for org.apache.torque:name1,
+     * org.apache:name1, org.apache.torque:name2 and
+     * org.apache.torque.generator:name1, and the queried namespace is
+     * org.apache.torque, then the mappings for
+     * org.apache.torque:name1 and org.apache.torque:name2 and org.apache:name1
+     * are returned. The mapping for org.apache.torque.generator:name1 is not
+     * returned, because it is not in the target namespace.
+     *
+     * @param namespace the namespace in which the desired objects
+     *        should be visible.
+     * @return all mappings in the desired Namespace, not null.
+     */
+    public QualifiedNameMap<T> getAllInHierarchy(Namespace namespace)
+    {
+        QualifiedNameMap<T> result = new QualifiedNameMap<T>();
+        for (Map.Entry<QualifiedName, T> entry : entrySet())
+        {
+            QualifiedName qualifiedName = entry.getKey();
+            if (!qualifiedName.isVisibleFrom(namespace))
+            {
+                continue;
+            }
+            result.put(qualifiedName, entry.getValue());
+        }
+        return result;
+    }
+
+    /**
+     * Returns all mappings which live in the given namespace.
+     * If one mapping hides another mapping, i.e. if one mapping
+     * is a more specialized version of another, the hidden mapping
+     * is NOT returned.
+     *
+     * For example, if the map contains mappings for org.apache.torque:name1,
+     * org.apache:name1, org.apache.torque:name2 and
+     * org.apache.torque.generator:name1, and the queried namespace is
+     * org.apache.torque, then the mappings for
+     * org.apache.torque:name1 and org.apache.torque:name2 are returned.
+     * The mapping for org.apache:name1 is hidden by org.apache.torque:name1,
+     * and org.apache.torque.generator:name1 is not in the target namespace.
+     *
+     * @param namespace the namespace in which the desired objects
+     *        should be visible.
+     * @return all mappings in the desired Namespace, except the mappings
+     *         which are hidden by another mapping, not null.
+     */
+    public QualifiedNameMap<T> getInHierarchy(Namespace namespace)
+    {
+        QualifiedNameMap<T> result = new QualifiedNameMap<T>();
+        for (Map.Entry<QualifiedName, T> entry : entrySet())
+        {
+            QualifiedName qualifiedName = entry.getKey();
+            T value = entry.getValue();
+            if (!qualifiedName.isVisibleFrom(namespace))
+            {
+                continue;
+            }
+
+            Iterator<Map.Entry<QualifiedName, T>> resultEntryIt
+                = result.entrySet().iterator();
+            while (resultEntryIt.hasNext())
+            {
+                Map.Entry<QualifiedName, T> resultEntry
+                    = resultEntryIt.next();
+                QualifiedName resultQName = resultEntry.getKey();
+                if (!resultQName.getName().equals(qualifiedName.getName()))
+                {
+                    // the qualified names do not have the same name,
+                    // they can not hide each other
+                    continue;
+                }
+                if (resultQName.isVisibleFrom(qualifiedName.getNamespace()))
+                {
+                    // resultQName is hidden by qualifiedName
+                    resultEntryIt.remove();
+                }
+                else
+                {
+                    // resultQName hides qualifiedName.
+                    // Do not add the mapping for qualifiedName.
+                    qualifiedName = resultQName;
+                    value = resultEntry.getValue();
+                    break;
+                }
+            }
+            result.put(qualifiedName, value);
+        }
+        return result;
+    }
+
+    /**
+     * Returns the object which is mapped to the given key.
+     * Only Objects of class Namespace are used as keys in this map.
+     *
+     * @param key the key for which the mapped object should be returned.
+     *
+     * @return the object mapped to the given key, or null if no mapping exists
+     *         for the key or null is mapped to the key.
+     *
+     * @see Map#get(java.lang.Object)
+     * @see #getInHierarchy(QualifiedName)
+     */
+    public T get(Object key)
+    {
+        return map.get(key);
+    }
+
+    /**
+     * Creates or overwrites a mapping in the map. Null as key is not supported.
+     *
+     * @param key the key for the mapping, not null.
+     * @param value the object mapped to the key.
+     *
+     * @return the previous object which was mapped to the given key,
+     *         or null if no mapping existed for the given key.
+     *
+     * @see Map#put(Object, Object)
+     */
+    public T put(QualifiedName key, T value)
+    {
+        if (key == null)
+        {
+            throw new NullPointerException("key must not be null");
+        }
+        return map.put(key, value);
+    }
+
+    /**
+     * Creates or overrides mappings for all the mappings in the supplied
+     * map. The supplied map must not contain a mapping for the key
+     * <code>null</code>.
+     *
+     * @param toPut the Map whcih mappings should be added to this map,
+     *        not null.
+     *
+     * @throws NullPointerException if toPut is null.
+     * @throws IllegalArgumentException if toPut contains a mapping for
+     *         the key <code>null</code>.
+     *
+     * @see Map#putAll(java.util.Map)
+     */
+    public void putAll(Map<? extends QualifiedName, ? extends T> toPut)
+    {
+        if (toPut.containsKey(null))
+        {
+            throw new IllegalArgumentException(
+                    "toPut must not contain a mapping for the key null");
+        }
+        map.putAll(toPut);
+    }
+
+    /**
+     * Removes the mapping for the supplied key, if this mapping exists.
+     *
+     * @param key the key for which the mapping should be removed.
+     *
+     * @return the object which was previously mapped to the key, or null
+     *         if no mapping existed for the provided key.
+     *
+     * @see Map#remove(java.lang.Object)
+     */
+    public T remove(Object key)
+    {
+        return map.remove(key);
+    }
+
+    /**
+     * Removes all mappings from this map.
+     *
+     * @see Map#clear()
+     */
+    public void clear()
+    {
+        map.clear();
+    }
+
+    /**
+     * Returns if this Map contains a mapping for the given key.
+     *
+     * @param key the key for which the existence of a mapping should be
+     *        checked.
+     *
+     * @return true if the map contains a mapping for the given key,
+     *         false otherwise.
+     *
+     * @see Map#containsKey(java.lang.Object)
+     */
+    public boolean containsKey(Object key)
+    {
+        return map.containsKey(key);
+    }
+
+    /**
+     * Returns all Entries in this map.
+     *
+     * @return a Set containing all mappings in this map.
+     *
+     * @see Map#entrySet()
+     */
+    public Set<Map.Entry<QualifiedName, T>> entrySet()
+    {
+        return map.entrySet();
+    }
+
+    /**
+     * Checks if any key is mapped to the given value.
+     *
+     * @param value the value which existence should be checked in the map.
+     *
+     * @return true if any key is mapped to the given value, false otherwise.
+     *
+     * @see Map#containsValue(java.lang.Object)
+     */
+    public boolean containsValue(Object value)
+    {
+        return map.containsValue(value);
+    }
+
+    /**
+     * Returns all values in this map.
+     *
+     * @return a collection containing all values in this map.
+     *
+     * @see Map#values()
+     */
+    public Collection<T> values()
+    {
+        return map.values();
+    }
+
+    /**
+     * Counts all mappings in the map.
+     *
+     * @return the number of mappings in the map.
+     *
+     * @see Map#size()
+     */
+    public int size()
+    {
+        return map.size();
+    }
+
+    /**
+     * Returns if the map contains any mappings at all.
+     *
+     * @return true if the map contains mappings, false otherwise.
+     *
+     * @see Map#isEmpty()
+     */
+    public boolean isEmpty()
+    {
+        return map.isEmpty();
+    }
+
+    /**
+     * Returns all keys of the map.
+     *
+     * @return a Collection containing all keys of the map, never null.
+     *
+     * @see Map#keySet()
+     */
+    public Set<QualifiedName> keySet()
+    {
+        return map.keySet();
+    }
+
+    /**
+     * Returns the more specific object out of two objects (the object
+     * which hides the other object).
+     *
+     * If one of the two objects is null, the other is returned.
+     * If both objects is null, null is returned.
+     * If both objects are in the same namespace, object1 is returned.
+     *
+     * It is assumed that namespace1 is an ancestor namespace of
+     * namespace2 or that namespace2 is in an ancestor namespace of
+     * namespace1 or both namespaces are the same.
+     *
+     * @param object1 the first object to compare.
+     * @param qualifiedName1 the qualified name of the first object,
+ *         must not be null if object1 is not null.
+     * @param object2 the second object to compare.
+     * @param qualifiedName2 the namepsace of the second object,
+     *         must not be null, if object2 is not null.
+     * @return the more specific object, or null if both object1 and
+     *         object2 are null.
+     * @throws NullPointerException if object1 and object2 are not null and
+     *          namespace1 or namespace2 are null.
+     */
+    public T getMoreSpecific(
+            T object1,
+            QualifiedName qualifiedName1,
+            T object2,
+            QualifiedName qualifiedName2)
+    {
+        if (object1 == null)
+        {
+            return object2;
+        }
+        if (object2 == null)
+        {
+            return object1;
+        }
+        Namespace namespace1 = qualifiedName1.getNamespace();
+        Namespace namespace2 = qualifiedName2.getNamespace();
+        if (namespace2.isVisibleFrom(namespace1))
+        {
+            // qualifiedName1 is more specific or in the same namespace
+            // as qualifiedName2, so return object1
+            return object1;
+        }
+        else
+        {
+            // qualifiedName2 is more specific than qualifiedName1
+            return object2;
+        }
+    }
+
+    /**
+     * Checks if this QualifiedNameMap is equal to another object.
+     * This is true if and only if the other object is a QualifiedNameMap,
+     * and contains the same mappings as this QualifiedNameMap.
+     *
+     * @param object the object to check whether it is equal to this object.
+     *
+     * @return true if this QualifiedNameMap is equal to the object,
+     *         false otherwise.
+     */
+    @Override
+    public boolean equals(Object object)
+    {
+        if (!(object instanceof QualifiedNameMap<?>))
+        {
+            return false;
+        }
+
+        QualifiedNameMap<?> other = (QualifiedNameMap<?>) object;
+
+        return map.equals(other.map);
+    }
+
+    /**
+     * Returns a hashCode for this object. The returned hashCode is consistent
+     * with equals().
+     *
+     * @return a hashcode for this QualifiedNameMap.
+     */
+    @Override
+    public int hashCode()
+    {
+        return map.hashCode();
+    }
+
+    /**
+     * Returns a string representation of this map for debugging
+     * purposes.
+     *
+     * @return a string representation of this map, not null.
+     */
+    @Override
+    public String toString()
+    {
+        return map.toString();
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/package.html
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/package.html?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/package.html (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/qname/package.html Tue Feb 16 17:15:43 2010
@@ -0,0 +1,38 @@
+<!--
+ Copyright 2001-2006 The Apache Software Foundation.
+
+ Licensed 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.
+-->
+<html>
+  <head>
+    <title>Torque-gf qualified names</title>
+  </head>
+  <body>
+    <p>
+      This package contains classes which provide the implementation
+      of a qualified name, i.e. a name whiches lives in a hierarchical
+      namespace. "Hierarchical" means that a name is visible in its own
+      namespace and all children of this namespace. All objects in the root
+      namespace (Namespace.ROOT_NAMESPACE) are visible in all other namespaces.
+      All objects living in a namespace are contained in a QualifiedNameMap,
+      which contains qualified names as key and the objects which are assigned
+      to the name as values. 
+    </p>
+    
+    <p>  
+      The keys in this map are objects of type QualifiedName. A QualifiedName
+      consists of a Namespace (represented by a Namespace Object), and a name.
+      Each QualifiedName must have a namespace, and a non-empty name.
+    </p>
+  </body>
+</html>

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSource.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSource.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSource.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSource.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,200 @@
+package org.apache.torque.gf.source;
+
+/*
+ * 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.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.torque.gf.configuration.ConfigurationException;
+import org.apache.torque.gf.source.filter.SourceFilter;
+import org.apache.torque.gf.source.xml.SourceToXml;
+
+/**
+ * A Source which uses a file as input.
+ */
+public class FileSource extends SourceImpl
+{
+    /**
+     * The log of the class.
+     */
+    private static Log log = LogFactory.getLog(FileSource.class);
+
+    /**
+     * The type of the file.
+     */
+    private SourceType type;
+
+    /**
+     * The path of the file.
+     */
+    private File path;
+
+    /**
+     * Constructor.
+     *
+     * @param type the source type, not null.
+     * @param path the path to the file to read, not null.
+     * @param elements the element which should be used as input, or null
+     *         if the root element should be used.
+     * @param sourceFilter the source filter class for the input.
+     *
+     * @throws ConfigurationException if the SourceFilter cannot be
+     *         instantiated.
+     * @throws IllegalArgumentException if the source type is unknown.
+     * @throws NullPointerException if path is null.
+     */
+    public FileSource(
+            SourceType type,
+            File path,
+            String elements,
+            SourceFilter sourceFilter,
+            List<TransformerDefinition> transformerDefinitions)
+        throws ConfigurationException
+    {
+        super(elements, sourceFilter, transformerDefinitions);
+        if (path == null)
+        {
+            throw new NullPointerException("path must not be null");
+        }
+        if (type == null)
+        {
+            throw new NullPointerException("type must not be null");
+        }
+        this.type = type;
+        this.path = path;
+    }
+
+    /**
+     * Returns the type of the source.
+     *
+     * @return the source type, not null.
+     */
+    public SourceType getType()
+    {
+        return type;
+    }
+
+    /**
+     * Returns the path to the source file.
+     *
+     * @return the path to the source file, not null.
+     */
+    public File getPath()
+    {
+        return path;
+    }
+
+    /**
+     * 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;
+        InputStream inputStream = null;
+        try
+        {
+            inputStream = new FileInputStream(path);
+            log.debug("Reading file "
+                    + path.getAbsolutePath()
+                    + " of type "
+                    + type.getKey());
+            result = type.getParser().parse(inputStream);
+        }
+        catch (FileNotFoundException e)
+        {
+            throw new SourceException(
+                    "File not found: " + path.getAbsolutePath(),
+                    e);
+        }
+        finally
+        {
+            if (inputStream != null)
+            {
+                try
+                {
+                    inputStream.close();
+                }
+                catch (IOException e)
+                {
+                    log.error("error closing input stream", e);
+                }
+            }
+        }
+
+
+        if (log.isDebugEnabled())
+        {
+            log.debug("finished creating root Element, source is\n"
+                    + new SourceToXml().toXml(result));
+        }
+        return result;
+    }
+
+    /**
+     * Returns the path of the file as a description.
+     *
+     * @return path of the file, not null.
+     *
+     * @see Source#getDescription()
+     */
+    public String getDescription()
+    {
+        return path.getAbsolutePath();
+    }
+
+    /**
+     * Returns the source file, if it exists.
+     *
+     * @return the source file, or null if the source is not read from a file.
+     * @return
+     */
+    public File getSourceFile()
+    {
+        return path;
+    }
+
+
+    @Override
+    public String toString()
+    {
+        StringBuffer result = new StringBuffer();
+        result.append("(path=").append(path)
+                .append(",type=").append(type)
+                .append(",startElementsPath=").append(getStartElementsPath())
+                .append(",transformerDefinitions=")
+                .append(getTransformerDefinitions());
+        return result.toString();
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/FileSourcesImpl.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,391 @@
+package org.apache.torque.gf.source;
+
+/*
+ * 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.io.FileFilter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.io.IOCase;
+import org.apache.commons.lang.StringUtils;
+import org.apache.torque.gf.configuration.ConfigurationException;
+import org.apache.torque.gf.configuration.ConfigurationHandlers;
+
+/**
+ * A collection of Sources, which all have the same elements,
+ * Source filters and transformerDefinitions and which are read from files
+ * in the file system.
+ *
+ * $Id: $
+ */
+public class FileSourcesImpl extends SimpleSourcesImpl
+{
+    /**
+     * The type of the file(s), or null if the type should be determined
+     * from the file name extension.
+     */
+    private SourceType type;
+
+    /**
+     * The path to the file(s).
+     */
+    private String path;
+
+    /** The paths of all contained Files. */
+    private List<File> paths;
+
+    /** The iterator over all contained Files. */
+    private Iterator<File> pathIt;
+
+    /**
+     * All known configuration handlers.
+     */
+    private ConfigurationHandlers configurationHandlers;
+
+    /**
+     * Constructor.
+     *
+     * @param type the source type, or null if the source type should
+     *        be determined from the file extension.
+     * @param path the path to the file(s) to read, not null.
+     * @param elements the path to the source elements to read, or null
+     *        if the root element should be used.
+     * @param sourceFilter the fully qualified class name of the source filter,
+     *        or null. The referenced class must have a standard constructor.
+     * @param configurationHandlers All known configuration handlers, not null.
+     * @param sourcesDir The directory where the sources should be read from,
+     *        not null.
+     *
+     * @throws IllegalArgumentException if the source type is unknown.
+     * @throws NullPointerException if path is null.
+     * @throws ConfigurationException if the source filter cannot be
+     *         instantiated.
+     */
+    public FileSourcesImpl(
+                    SourceType type,
+                    String path,
+                    String elements,
+                    String sourceFilter,
+                    List<TransformerDefinition> transformerDefinitions,
+                    ConfigurationHandlers configurationHandlers,
+                    File sourcesDir)
+            throws ConfigurationException
+    {
+        super(elements, sourceFilter, transformerDefinitions);
+        if (path == null)
+        {
+            throw new NullPointerException("path must not be null");
+        }
+        if (configurationHandlers == null)
+        {
+            throw new NullPointerException(
+                    "configurationHandlers must not be null");
+        }
+        if (sourcesDir == null)
+        {
+            throw new NullPointerException("sourcesDir must not be null");
+        }
+        this.type = type;
+        this.path = path;
+        this.configurationHandlers = configurationHandlers;
+
+        int baseDirSeparatorPos = getBaseDirSeparatorPos(path);
+        File baseDir = new File(
+                sourcesDir,
+                getBaseDir(path, baseDirSeparatorPos));
+        String wildcardPattern = getWildcardPattern(path, baseDirSeparatorPos);
+        String[] wildcardParts = StringUtils.split(wildcardPattern, "\\/");
+        List<String> wildcardList = Arrays.asList(wildcardParts);
+
+        paths = getFiles(baseDir, wildcardList);
+        pathIt = paths.iterator();
+    }
+
+    List<File> getFiles(File baseDir, List<String> pathParts)
+            throws ConfigurationException
+    {
+        List<String> pathPartsCopy = new ArrayList<String>(pathParts);
+        String partToProcess = pathPartsCopy.remove(0);
+        if (pathPartsCopy.size() == 0)
+        {
+            File[] result = baseDir.listFiles(
+                    new WildcardFilter(partToProcess, false, true));
+            if (result == null)
+            {
+                throw new ConfigurationException(
+                        "Could not list files in the following Directory "
+                            + "while reading the sources: "
+                            + baseDir.getAbsolutePath());
+            }
+            return Arrays.asList(result);
+        }
+        if ("..".equals(partToProcess))
+        {
+            return getFiles(baseDir.getParentFile(), pathPartsCopy);
+        }
+        File[] matchingDirs = baseDir.listFiles(
+                new WildcardFilter(partToProcess, true, false));
+        List<File> result = new ArrayList<File>();
+        for (File dir : matchingDirs)
+        {
+            result.addAll(getFiles(dir, pathPartsCopy));
+        }
+        return result;
+    }
+
+    /**
+     * Returns the position of the separator which separates the base part
+     * of the path which does not contain any wildcards from the rest.
+     * Example:
+     * <ul>
+     *   <li>*.txt returns -1(no separator)</li>
+     *   <li>schema*.xml returns -1(no separator)</li>
+     *   <li>xml\schema*.xml returns 3 (backslash position)</li>
+     *   <li>/xml/???/schema*.xml returns 4 (middle slash position)</li>
+     * </ul>
+     * @param path the path to compute the position from, not null.
+     * @return the separator position, -1 if no base part exists.
+     */
+    static int getBaseDirSeparatorPos(String path)
+    {
+        int asteriskIndex = path.indexOf("*");
+        int questionMarkIndex = path.indexOf("?");
+        if (asteriskIndex != -1)
+        {
+            if (questionMarkIndex != -1)
+            {
+                int min = Math.min(asteriskIndex, questionMarkIndex);
+                return getLargestSeparatorPos(path, min);
+            }
+            else
+            {
+                return getLargestSeparatorPos(path, asteriskIndex);
+            }
+        }
+        return getLargestSeparatorPos(path, questionMarkIndex);
+    }
+
+    /**
+     * Returns the largest position of a path separator within the path
+     * which is smaller than endIndex. An endIndex of -1 means that
+     * the largest separator pos should be given. -1 is returned if no
+     * separator is present in the region.
+     * <ul>
+     *   <li>getBaseDir("/xml/xxxx", 5) returns 4</li>
+     *   <li>getBaseDir("/xml/x/y", -1) returns 6</li>
+     *   <li>getBaseDir("/xml/x/y/", -1) returns 8</li>
+     * </ul>
+     */
+    private static int getLargestSeparatorPos(String path, int maxIndex)
+    {
+        String baseString;
+        if (maxIndex == -1)
+        {
+            baseString = path;
+        }
+        else
+        {
+            baseString = path.substring(0, maxIndex);
+        }
+        return FilenameUtils.indexOfLastSeparator(baseString);
+    }
+
+    static String getBaseDir(String path, int baseDirSeparatorPos)
+    {
+        if (baseDirSeparatorPos == -1)
+        {
+            return ".";
+        }
+        else
+        {
+            String resultString = path.substring(0, baseDirSeparatorPos);
+            if (StringUtils.EMPTY.equals(resultString))
+            {
+                resultString = ".";
+            }
+            return resultString;
+        }
+
+    }
+
+    static String getWildcardPattern(String path, int baseDirSeparatorPos)
+    {
+        String resultString = path.substring(baseDirSeparatorPos + 1);
+        return resultString;
+    }
+
+    public boolean hasNext()
+    {
+        return pathIt.hasNext();
+    }
+
+    public Source next()
+    {
+        File currentPath = pathIt.next();
+        SourceType currentType = type;
+        if (currentType == null)
+        {
+            Set<SourceType> sourceTypes
+                    = configurationHandlers.getSourceTypes();
+            for (SourceType sourceType : sourceTypes)
+            {
+                String filenameExtension = sourceType.getFilenameExtension();
+                if (filenameExtension != null
+                        && currentPath.getName().endsWith(
+                                "." + filenameExtension))
+                {
+                    currentType = sourceType;
+                    break;
+                }
+            }
+        }
+        if (currentType == null)
+        {
+            throw new RuntimeException(
+                    "Type is not set and file extension is unknown for file "
+                        + currentPath.getName());
+        }
+        try
+        {
+            return new FileSource(
+                    currentType,
+                    currentPath,
+                    getStartElementsPath(),
+                    getSourceFilter(),
+                    getTransformerDefinitions());
+        }
+        catch (ConfigurationException e)
+        {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void remove()
+    {
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuffer result = new StringBuffer();
+        result.append("(path=").append(path)
+                .append(",type=").append(type)
+                .append(",sourceFilter=").append(getSourceFilter())
+                .append(",startElementsPath=").append(getStartElementsPath())
+                .append(",transformerDefinitions=")
+                .append(getTransformerDefinitions())
+                .append(")");
+        return result.toString();
+    }
+
+    public SourceType getType()
+    {
+        return type;
+    }
+
+    public String getPath()
+    {
+        return path;
+    }
+
+    public List<File> getPaths()
+    {
+        return Collections.unmodifiableList(paths);
+    }
+
+    /**
+     * A filter evaluating a file name against a wildcard expression.
+     */
+    private static class WildcardFilter implements FileFilter
+    {
+        /**
+         * The wildcard expression against which the file names are
+         * checked.
+         */
+        private String expression;
+
+        /**
+         * Whether directories are accepted at all.
+         */
+        private boolean acceptDir;
+
+        /**
+         * Whether files are accepted at all.
+         */
+        private boolean acceptFile;
+
+        /**
+         * Constructor.
+         *
+         * @param expression The wildcard expression against which
+         *        the file names are checked.
+         * @param acceptDir Whether directories are accepted at all.
+         * @param acceptFile Whether files are accepted at all.
+         */
+        public WildcardFilter(
+                String expression,
+                boolean acceptDir,
+                boolean acceptFile)
+        {
+            this.expression = expression;
+            this.acceptDir = acceptDir;
+            this.acceptFile = acceptFile;
+        }
+
+        /**
+         * Returns whether a file matches the criteria of this filter.
+         * If the file is a directory and <code>acceptDir</code> is false,
+         * the file is rejected.
+         * If the file is regular file and <code>acceptFile</code> is false,
+         * the file is rejected.
+         * If the filename does not match the wildcard filter, the file is
+         * rejected.
+         * If none of the above applies, the file is accepted.
+         *
+         * @return false if the file is rejected, true if it is accepted.
+         */
+        public boolean accept(File file)
+        {
+            if (!acceptDir && file.isDirectory())
+            {
+                return false;
+            }
+            if (!acceptFile && file.isFile())
+            {
+                return false;
+            }
+            if (FilenameUtils.wildcardMatch(
+                    file.getName(),
+                    expression,
+                    IOCase.SENSITIVE))
+            {
+                return true;
+            }
+            return false;
+        }
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/RichSourceElement.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/RichSourceElement.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/RichSourceElement.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/RichSourceElement.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,93 @@
+package org.apache.torque.gf.source;
+
+/*
+ * 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.util.List;
+
+/**
+ * A source element which has useful shortcut methods for usage in templates.
+ *
+ */
+public interface RichSourceElement extends SourceElement
+{
+    /**
+     * Returns whether children with the given name exist.
+     * @param name the name of the child element, not null.
+     * @return true if children with the given name exist, false otherwise.
+     * @throws NullPointerException if name is null.
+     */
+    boolean hasChild(String name);
+
+    /**
+     * Returns whether a following element exists as a child of the parent of
+     * this element.
+     * @return true if a following element exists, false if not.
+     */
+    boolean hasFollowing();
+
+    /**
+     * Returns whether an preceding exists as a child of the parent of
+     * this element.
+     * @return true if a preceding element exists, false if not.
+     */
+    boolean hasPreceding();
+
+    /**
+     * Returns whether a following element exists as a child of the parent of
+     * this element, which has the same name as this source element.
+     * @return true if a following sibling exists, false if not.
+     */
+    boolean hasFollowingSibling();
+
+    /**
+     * Returns whether an preceding exists as a child of the parent of
+     * this element, which has the same name as this source element.
+     * @return true if a preceding sibling exists, false if not.
+     */
+    boolean hasPrecedingSibling();
+
+    /**
+     * Returns all the preceding elements before this element
+     * with the given name.
+     * If name is null, all preceding elements are returned.
+     * If this element has no parent, an empty list is returned.
+     * @param name the name of the preceding elements to select,
+     *        or null to select all preceding elements.
+     * @return a list containing the following elements with the given name,
+     *         never null.
+     *
+     * @see <a href="http://www.w3.org/TR/xpath#axes"></a>
+     */
+    List<SourceElement> getPreceding(String name);
+
+    /**
+     * Returns all the following elements after this element
+     * with the given name.
+     * If name is null, all following elements are returned.
+     * If this element has no parent, an empty list is returned.
+     * @param name the name of the following elements to select,
+     *        or null to select all following elements.
+     * @return a list containing the following elements with the given name,
+     *         never null.
+     *
+     * @see <a href="http://www.w3.org/TR/xpath#axes"></a>
+     */
+    List<SourceElement> getFollowing(String name);
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message