You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2010/02/16 18:16:02 UTC
svn commit: r910600 [12/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...
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/properties/package.html
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/properties/package.html?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/properties/package.html (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/properties/package.html Tue Feb 16 17:15:43 2010
@@ -0,0 +1,26 @@
+<!--
+ 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 properties sources</title>
+ </head>
+ <body>
+ <p>
+ This package contains the handling of properties source files
+ in Torque-gf.
+ </p>
+ </body>
+</html>
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/AttributeTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/AttributeTransformer.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/AttributeTransformer.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/AttributeTransformer.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,1001 @@
+package org.apache.torque.gf.source.transform;
+
+/*
+ * 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.BufferedReader;
+import java.io.IOException;
+import java.io.Reader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.torque.gf.control.ControllerState;
+import org.apache.torque.gf.source.SourceElement;
+
+/**
+ * Adds or replaces attributes in a SourceElement according to a definition
+ * file. An example for a line in the definition file would be:
+ * <br/><br/>
+ * ${attribute(override):newAttribute}=prefix${attribute:oldAttribute}${option:suffix}
+ * <br/><br/>
+ * This would add the attribute newAttribute to the current sourceElement,
+ * and its content would be (in java notation)
+ * "prefix"
+ * + (currentElement.getAttribute("oldAttribute") == null
+ * ? ""
+ * : currentElement.getAttribute("oldAttribute"))
+ * + options.getOption("suffix")
+ * <br/><br/>
+ * On the left hand side, one can use attribute(override) (which replaces
+ * the attribute if its already there) and attribute(noOverride) which
+ * preserves the attribute if its already there.
+ * On the right hand side, you can use either plain text,
+ * ${attribute:attributeName} and ${option:optionName} in any combination.
+ * Escape character is the backslash. Lines starting with # and empty lines
+ * are disregarded.
+ */
+public class AttributeTransformer implements SourceTransformer
+{
+ /**
+ * The character separating the source from the target in the definition
+ * file.
+ */
+ private static final char SOURCE_TARGET_SEPARATION_CHAR = '=';
+
+ /**
+ * The character which starts a comment in the definition file.
+ */
+ private static final String COMMENT_CHAR = "#";
+
+ /**
+ * The escape character in the definition file.
+ */
+ private static final char ESCAPE_CHAR = '\\';
+
+ /**
+ * The character which starts a replacement in the definition file.
+ */
+ private static final char DEFINITION_CHAR = '$';
+
+ /**
+ * The character which starts the definition of a replacement
+ * in the definition file.
+ */
+ private static final char DEFINITION_START_CHAR = '{';
+
+ /**
+ * The character which ends a replacement in the definition file.
+ */
+ private static final char DEFINITION_END_CHAR = '}';
+
+ /**
+ * The character which is used as separation in a replacement definition
+ * in the definition file
+ */
+ private static final char DEFINITION_TYPE_SEPARATION_CHAR = ':';
+
+ /** The logger of the class. */
+ private static Log log = LogFactory.getLog(AttributeTransformer.class);
+
+ /**
+ * The list of transform rules which is created from the definition file.
+ */
+ private List<TransformRule> transformRules = new ArrayList<TransformRule>();
+
+ /**
+ * Constructor.
+ *
+ * @param transformDefinition the Reader from which the transformDefinition
+ * is read. The Reader is closed after processing.
+ *
+ * @throws SourceTransformerException if an error occurs while reading or
+ * parsing the transformDefinition.
+ */
+ public AttributeTransformer(Reader transformDefinition)
+ throws SourceTransformerException
+ {
+ BufferedReader reader = new BufferedReader(transformDefinition);
+
+ try
+ {
+ String line = reader.readLine();
+ while (line != null)
+ {
+ try
+ {
+ if (line.trim().startsWith(COMMENT_CHAR)
+ || line.trim().length() == 0)
+ {
+ continue;
+ }
+ int equalsPos
+ = line.indexOf(SOURCE_TARGET_SEPARATION_CHAR);
+ String target = line.substring(0, equalsPos).trim();
+ String source = line.substring(equalsPos + 1).trim();
+
+ List<Definition> targetDefinitions = parse(target, false);
+ if (targetDefinitions.size() != 1)
+ {
+ log.error("While parsing \"" + line + "\" : "
+ + targetDefinitions.size()
+ + " definitions found before "
+ + SOURCE_TARGET_SEPARATION_CHAR
+ + " at position "
+ + equalsPos
+ + ", must be 1");
+ }
+
+ List<Definition> sourceDefinitions = parse(source, true);
+ TransformRule transformRule = new TransformRule(
+ targetDefinitions.get(0),
+ sourceDefinitions);
+ transformRules.add(transformRule);
+ }
+ finally
+ {
+ line = reader.readLine();
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ throw new SourceTransformerException(
+ "Could not read transformDefinition",
+ e);
+ }
+ finally
+ {
+ if (reader != null)
+ {
+ try
+ {
+ reader.close();
+ }
+ catch (IOException e)
+ {
+ log.warn("Could not close reader", e);
+ }
+ }
+ }
+ }
+
+
+ /**
+ * Adds attributes to the sourceElement according to the
+ * transformDefinition.
+ *
+ * @param toTransform the sourceElement to transform. Must implement
+ * EditableSourceElement.
+ *
+ * @param controllerState the current state of the controller, not null.
+ *
+ * @return the source element with the additional attributes added.
+ *
+ * @throws SourceTransformerException if an error occurs during
+ * transforming.
+ */
+ public SourceElement transform(
+ SourceElement toTransform,
+ ControllerState controllerState)
+ throws SourceTransformerException
+ {
+ for (TransformRule transformRule : transformRules)
+ {
+ StringBuilder sourceResult = new StringBuilder();
+ for (Definition definition : transformRule.getSource())
+ {
+ Definition.Type type = definition.getType();
+ if (Definition.Type.PLAIN == type)
+ {
+ sourceResult.append(definition.getContent());
+ }
+ else if (Definition.Type.ATTRIBUTE == type)
+ {
+ Object attributeValue = toTransform.getAttribute(
+ definition.getContent());
+ if (attributeValue != null)
+ {
+ sourceResult.append(attributeValue.toString());
+ }
+ }
+ else if (Definition.Type.OPTION == type)
+ {
+ Object optionValue = controllerState.getOption(
+ definition.getContent());
+ if (optionValue != null)
+ {
+ sourceResult.append(optionValue.toString());
+ }
+ }
+ else
+ {
+ throw new SourceTransformerException(
+ "Unknown definition Type" + type + " in source");
+ }
+ }
+ Definition target = transformRule.getTarget();
+ if (Definition.Type.ATTRIBUTE_NO_OVERRIDE == target.getType())
+ {
+ String attributeName = target.getContent();
+ if (toTransform.getAttribute(attributeName) == null)
+ {
+ toTransform.setAttribute(
+ attributeName,
+ sourceResult.toString());
+ if (log.isTraceEnabled())
+ {
+ log.trace("Setting Attribute "
+ + attributeName
+ + " on element "
+ + toTransform.getName()
+ + " to "
+ + sourceResult);
+ }
+ }
+ else
+ {
+ if (log.isTraceEnabled())
+ {
+ log.trace("Attribute "
+ + attributeName
+ + " already set on element "
+ + toTransform.getName()
+ + " skipping");
+ }
+ }
+ }
+ else if (Definition.Type.ATTRIBUTE_OVERRIDE == target.getType())
+ {
+ String attributeName = target.getContent();
+ if (toTransform.getAttribute(attributeName) == null)
+ {
+ if (log.isTraceEnabled())
+ {
+ log.trace("Setting Attribute "
+ + attributeName
+ + " on element "
+ + toTransform.getName()
+ + " to "
+ + sourceResult);
+ }
+ }
+ else
+ {
+ if (log.isTraceEnabled())
+ {
+ log.trace("Overriding Attribute "
+ + attributeName
+ + " on element "
+ + toTransform.getName()
+ + " with value "
+ + sourceResult);
+ }
+ }
+ toTransform.setAttribute(
+ attributeName,
+ sourceResult.toString());
+ }
+ else
+ {
+ throw new SourceTransformerException("Unknown target type "
+ + target.getType());
+ }
+ }
+ return toTransform;
+ }
+
+ /**
+ * Parses a String into a List of Definitions.
+ *
+ * @param toParse the String to parse, not null.
+ * @param source whether the String is the source part (rhs) (true) or
+ * the target part (lhs) (false) of the transform rule to parse.
+ *
+ * @return the list of definitions, not null.
+ *
+ * @throws SourceTransformerException if the syntax of the string is
+ * incorrect.
+ */
+ List<Definition> parse(String toParse, boolean source)
+ throws SourceTransformerException
+ {
+ List<Definition> result = new ArrayList<Definition>();
+ ParseState parseState = new ParseState();
+ for (int position = 0; position < toParse.length(); ++position)
+ {
+ char current = toParse.charAt(position);
+
+ if (parseState.isDefinitionStart())
+ {
+ parseAfterDefinitionStart(
+ current,
+ parseState,
+ toParse,
+ position);
+ continue;
+ }
+ if (parseState.isEscape())
+ {
+ parseAfterEscape(current, parseState);
+ continue;
+ }
+ if (current == ESCAPE_CHAR)
+ {
+ parseState.setEscape(true);
+ continue;
+ }
+ if (current == DEFINITION_CHAR)
+ {
+ if (parseState.isDefinitionStart())
+ {
+ log.error("While parsing \"" + toParse + "\" : "
+ + DEFINITION_CHAR
+ + " must be escaped inside Definition"
+ + " at Position "
+ + position);
+ throw new SourceTransformerException(
+ DEFINITION_CHAR
+ + " must be followed by "
+ + DEFINITION_START_CHAR);
+ }
+ if (parseState.getDefinitionValue().length() > 0)
+ {
+ checkTypeAllowed(parseState.getDefinitionType(), source);
+ result.add(new Definition(
+ parseState.getDefinitionType(),
+ parseState.getDefinitionValue().toString()));
+ parseState.resetDefinitionValue();
+ }
+ parseState.setDefinitionStart(true);
+ continue;
+ }
+ if (!parseState.isInDefinition())
+ {
+ parseState.getDefinitionValue().append(current);
+ continue;
+ }
+ // we are inside a definition
+ if (current == DEFINITION_END_CHAR)
+ {
+ Definition definition = parseDefinitionEnd(
+ parseState,
+ source,
+ toParse,
+ position);
+ result.add(definition);
+ continue;
+ }
+ if (parseState.isInType())
+ {
+ parseInType(current, parseState, source, toParse, position);
+ continue;
+ }
+ parseState.getDefinitionValue().append(current);
+ }
+ if (Definition.Type.PLAIN != parseState.getDefinitionType())
+ {
+ log.error("While parsing \"" + toParse + "\" : "
+ + "Definition not closed at end of String.");
+ throw new SourceTransformerException(
+ "Definition not closed at end of String.");
+ }
+ if (parseState.getDefinitionValue().length() > 0)
+ {
+ checkTypeAllowed(parseState.getDefinitionType(), source);
+ result.add(
+ new Definition(
+ parseState.getDefinitionType(),
+ parseState.getDefinitionValue().toString()));
+ }
+
+ return result;
+ }
+
+
+ /**
+ * Parse a character while inside a definition type.
+ *
+ * @param current the character to parse.
+ * @param parseState the parsing state, not null.
+ * @param source whether the String is the source part (rhs) (true) or
+ * the target part (lhs) (false) of the transform rule to parse.
+ * @param toParse the complete string to parse, for error messages.
+ * @param position the parsing position, for error messages.
+ *
+ * @throws SourceTransformerException if a charter is encountered
+ * which is not valid at this position.
+ */
+ private void parseInType(
+ char current,
+ ParseState parseState,
+ boolean source,
+ String toParse,
+ int position)
+ throws SourceTransformerException
+ {
+ if (current != DEFINITION_TYPE_SEPARATION_CHAR)
+ {
+ parseState.getDefinitionTypeBuffer().append(current);
+ }
+ else
+ {
+ String typeString
+ = parseState.getDefinitionTypeBuffer().toString();
+ Definition.Type type = null;
+ for (Definition.Type possibleType : Definition.Type.values())
+ {
+ if (possibleType.getName().equals(typeString))
+ {
+ checkTypeAllowed(possibleType, source);
+ type = possibleType;
+ break;
+ }
+ }
+ if (type == null)
+ {
+ log.error("While parsing \"" + toParse + "\" : "
+ + "Unknown definition type "
+ + typeString
+ + " at Position "
+ + position);
+ throw new SourceTransformerException(
+ "Unknown definition type");
+ }
+ parseState.setDefinitionType(type);
+ parseState.resetDefinitionTypeBuffer();
+ parseState.setInType(false);
+ }
+ }
+
+ /**
+ * Parse the definition end character.
+ *
+ * @param parseState the parsing state, not null.
+ * @param source whether the String is the source part (rhs) (true) or
+ * the target part (lhs) (false) of the transform rule to parse.
+ * @param toParse the complete string to parse, for error messages.
+ * @param position the parsing position, for error messages.
+ *
+ * @return the Definition which parsing was just ended.
+ *
+ * @throws SourceTransformerException if a charter is encountered
+ * which is not valid at this position.
+ */
+ private Definition parseDefinitionEnd(
+ ParseState parseState,
+ boolean source,
+ String toParse,
+ int position)
+ throws SourceTransformerException
+ {
+ if (parseState.isInType())
+ {
+ log.error("While parsing \"" + toParse + "\" : "
+ + "Seen "
+ + DEFINITION_END_CHAR
+ + " before "
+ + DEFINITION_TYPE_SEPARATION_CHAR
+ + " at Position "
+ + position);
+ throw new SourceTransformerException(
+ "Seen "
+ + DEFINITION_END_CHAR
+ + " before "
+ + DEFINITION_TYPE_SEPARATION_CHAR);
+ }
+ checkTypeAllowed(parseState.getDefinitionType(), source);
+ Definition result = new Definition(
+ parseState.getDefinitionType(),
+ parseState.getDefinitionValue().toString());
+ parseState.setDefinitionType(Definition.Type.PLAIN);
+ parseState.resetDefinitionValue();
+ parseState.setInDefinition(false);
+ return result;
+ }
+
+ /**
+ * Parse the character after the escape character.
+ *
+ * @param current the character to parse.
+ * @param parseState the parsing state, not null.
+ */
+ private void parseAfterEscape(char current, ParseState parseState)
+ {
+ if (parseState.isInType())
+ {
+ parseState.getDefinitionTypeBuffer().append(current);
+ }
+ else
+ {
+ parseState.getDefinitionValue().append(current);
+ }
+ parseState.setEscape(false);
+ }
+
+
+ /**
+ * Parses a character if definitionStart is true in parseState.
+ *
+ * @param current the current character to parse.
+ * @param parseState the current parse state.
+ * @param toParse the complete string to parse (for error messages).
+ * @param position the parse position (for error messages).
+ *
+ * @throws SourceTransformerException if an illegal character is
+ * encountered.
+ */
+ private void parseAfterDefinitionStart(
+ char current,
+ ParseState parseState,
+ String toParse,
+ int position)
+ throws SourceTransformerException
+ {
+ if (current != DEFINITION_START_CHAR)
+ {
+ log.error("While parsing \"" + toParse + "\" : "
+ + DEFINITION_CHAR
+ + " must be followed by "
+ + DEFINITION_START_CHAR
+ + " at Position "
+ + position);
+ throw new SourceTransformerException(
+ DEFINITION_CHAR
+ + " must be followed by "
+ + DEFINITION_START_CHAR);
+ }
+ parseState.setDefinitionStart(false);
+ parseState.setInDefinition(true);
+ parseState.setInType(true);
+ }
+
+ /**
+ * Checks whether the type is allowed in the current part of the transform
+ * rule.
+ *
+ * @param type The type to check, not null.
+ * @param source whether the type is in the source part (rhs) (true) or
+ * the target part (lhs) (false) of the transform rule to parse.
+ *
+ * @throws SourceTransformerException if the type is not allowed in its
+ * current place.
+ */
+ private void checkTypeAllowed(Definition.Type type, boolean source)
+ throws SourceTransformerException
+ {
+ if (source && !type.inSource)
+ {
+ log.error("Definition type "
+ + type
+ + " may not occur in source ");
+ throw new SourceTransformerException(
+ "definition type not allowed in source");
+ }
+ if (!source && !type.inTarget)
+ {
+ log.error("Definition type "
+ + type
+ + " may not occur in target ");
+ throw new SourceTransformerException(
+ "definition type not allowed in target");
+ }
+ }
+
+ /**
+ * One single element in the left hand side or right hand side of a
+ * transform rule.
+ */
+ static class Definition
+ {
+ /** The possible types of the elements. */
+ enum Type
+ {
+ /**
+ * The value of an attribute from the current Source Element
+ * should be inserted
+ */
+ ATTRIBUTE("attribute", true, false),
+ /**
+ * The value of an option should be inserted
+ */
+ OPTION("option", true, false),
+ /**
+ * Plain text should be inserted
+ */
+ PLAIN("plain", true, false),
+ /**
+ * An attribute should be set and overridden if it already
+ * exists
+ */
+ ATTRIBUTE_OVERRIDE("attribute(override)", false, true),
+ /**
+ * An attribute should be set, but not be overridden if it already
+ * exists
+ */
+ ATTRIBUTE_NO_OVERRIDE("attribute(noOverride)", false, true);
+
+ /** The name of the type. */
+ private String name;
+
+ /**
+ * Whether this type can occur in the right hand side (the source)
+ * of a definition.
+ */
+ private boolean inSource;
+
+ /**
+ * Whether this type can occur in the left hand side (the target)
+ * of a definition.
+ */
+ private boolean inTarget;
+
+
+ /**
+ * Constructor.
+ *
+ * @param name the name of the type.
+ * @param inSource whether this type can occur in the right hand
+ * side (the source) of a definition
+ * @param inTarget whether this type can occur in the left hand side
+ * (the target) of a definition.
+ */
+ private Type(String name, boolean inSource, boolean inTarget)
+ {
+ this.name = name;
+ this.inSource = inSource;
+ this.inTarget = inTarget;
+ }
+
+ /**
+ * Returns the name (i.e. what should be in the definition).
+ *
+ * @return the name, not null.
+ */
+ public String getName()
+ {
+ return name;
+ }
+
+ /**
+ * Returns whether the definition can occur in the source part.
+ *
+ * @return true if the definition can occur in the source part,
+ * false otherwise.
+ */
+ public boolean isInSource()
+ {
+ return inSource;
+ }
+
+ /**
+ * Returns whether the definition can occur in the target part.
+ *
+ * @return true if the definition can occur in the target part,
+ * false otherwise.
+ */
+ public boolean isInTarget()
+ {
+ return inTarget;
+ }
+
+ /**
+ * Returns a String for debugging the type.
+ *
+ * @return the name of the type.
+ */
+ @Override
+ public String toString()
+ {
+ return name;
+ }
+ }
+
+ /**
+ * The type of the element.
+ */
+ private Type type;
+
+ /**
+ * The content of the element (which can be the name of the attribute,
+ * option, or the plain text itself)
+ */
+ private String content;
+
+ /**
+ * Constructor.
+ *
+ * @param type the type of the definition, not null.
+ * @param content the content (i.e name of the attribute, option,
+ * or the plain text itself).
+ */
+ public Definition(Type type, String content)
+ {
+ if (type == null)
+ {
+ throw new NullPointerException("type is null");
+ }
+ if (content == null)
+ {
+ throw new NullPointerException("content is null");
+ }
+ this.type = type;
+ this.content = content;
+ }
+
+ /**
+ * Returns the type of the definition.
+ *
+ * @return the type, not null.
+ */
+ public Type getType()
+ {
+ return type;
+ }
+
+ /**
+ * Returns the content (i.e name of the attribute, option, or the plain
+ * text itself) of the definition.
+ *
+ * @return the content, not null.
+ */
+ public String getContent()
+ {
+ return content;
+ }
+ }
+
+ /**
+ * Keeps the current parsing state.
+ */
+ static class ParseState
+ {
+ /** Whether the following character is escaped. */
+ private boolean escape = false;
+
+ /** Whether we are just after DEFINITION_CHAR */
+ private boolean definitionStart = false;
+
+ /** Whether we are inside a definition. */
+ private boolean inDefinition = false;
+
+ /**
+ * The string builder for the definition type.
+ */
+ private StringBuilder definitionTypeBuffer = new StringBuilder();
+
+ /**
+ * The parsed definition type.
+ */
+ private Definition.Type definitionType = Definition.Type.PLAIN;
+
+ /** The string builder for the definition value. */
+ private StringBuilder definitionValue = new StringBuilder();
+ /**
+ * Whether we are before DEFINITION_TYPE_SEPARATION_CHAR
+ * in a definition.
+ */
+ private boolean inType = false;
+
+ /**
+ * Returns whether the following character is escaped.
+ *
+ * @return whether the following character is escaped.
+ */
+ public boolean isEscape()
+ {
+ return escape;
+ }
+
+ /**
+ * Sets whether the following character is escaped.
+ *
+ * @param escape whether the following character is escaped.
+ */
+ public void setEscape(boolean escape)
+ {
+ this.escape = escape;
+ }
+
+ /**
+ * Returns whether we are just after <code>DEFINITION_CHAR</code>.
+ *
+ * @return whether we are just after <code>DEFINITION_CHAR</code>.
+ */
+ public boolean isDefinitionStart()
+ {
+ return definitionStart;
+ }
+
+ /**
+ * Sets whether we are just after <code>DEFINITION_CHAR</code>.
+ *
+ * @param definitionStart whether we are just after
+ * <code>DEFINITION_CHAR</code>.
+ */
+ public void setDefinitionStart(boolean definitionStart)
+ {
+ this.definitionStart = definitionStart;
+ }
+
+ /**
+ * Returns whether we are inside a definition.
+ *
+ * @return whether we are inside a definition.
+ */
+ public boolean isInDefinition()
+ {
+ return inDefinition;
+ }
+
+ /**
+ * Sets whether we are inside a definition.
+ *
+ * @param inDefinition whether we are inside a definition.
+ */
+ public void setInDefinition(boolean inDefinition)
+ {
+ this.inDefinition = inDefinition;
+ }
+
+ /**
+ * Returns whether we are before
+ * <code>DEFINITION_TYPE_SEPARATION_CHAR</code> in a definition.
+ *
+ * @return whether we are before
+ * <code>DEFINITION_TYPE_SEPARATION_CHAR</code> in a definition.
+ */
+ public boolean isInType()
+ {
+ return inType;
+ }
+
+ /**
+ * Sets whether we are before
+ * <code>DEFINITION_TYPE_SEPARATION_CHAR</code> in a definition.
+ *
+ * @param inType whether we are before
+ * <code>DEFINITION_TYPE_SEPARATION_CHAR</code> in a definition.
+ */
+ public void setInType(boolean inType)
+ {
+ this.inType = inType;
+ }
+
+ /**
+ * Returns the string builder for the definition type.
+ *
+ * @return the string builder for the definition type, not null.
+ */
+ public StringBuilder getDefinitionTypeBuffer()
+ {
+ return definitionTypeBuffer;
+ }
+
+ /**
+ * Resets the string builder for the definition type buffer.
+ * This is done by using a new string builder, i. e. the old one
+ * is discarded.
+ */
+ public void resetDefinitionTypeBuffer()
+ {
+ this.definitionTypeBuffer = new StringBuilder();
+ }
+
+ /**
+ * Returns the parsed definition type.
+ *
+ * @return the parsed definition type.
+ */
+ public Definition.Type getDefinitionType()
+ {
+ return definitionType;
+ }
+
+ /**
+ * Sets the parsed definition type.
+ *
+ * @param definitionType the parsed definition type.
+ */
+ public void setDefinitionType(Definition.Type definitionType)
+ {
+ this.definitionType = definitionType;
+ }
+
+ /**
+ * Returns the string builder for the definition value.
+ *
+ * @return the string builder for the definition value, not null.
+ */
+ public StringBuilder getDefinitionValue()
+ {
+ return definitionValue;
+ }
+
+ /**
+ * Resets the string builder for the definition value.
+ * This is done by using a new string builder, i. e. the old one
+ * is discarded.
+ */
+ public void resetDefinitionValue()
+ {
+ this.definitionValue = new StringBuilder();
+ }
+ }
+
+ /**
+ * A single transform rule.
+ */
+ static class TransformRule
+ {
+ /**
+ * What should be set in which manner ?
+ */
+ private Definition target;
+
+ /**
+ * The rule of how to compose the new content.
+ */
+ private List<Definition> source;
+
+ /**
+ * Constructor.
+ *
+ * @param target What should be set in which manner; not null.
+ *
+ * @param source how to compose the new content, not null.
+ */
+ public TransformRule(Definition target, List<Definition> source)
+ {
+ if (target == null)
+ {
+ throw new NullPointerException("target is null");
+ }
+ if (source == null)
+ {
+ throw new NullPointerException("source is null");
+ }
+ this.target = target;
+ this.source = source;
+ }
+
+ /**
+ * Returns what should be set if the rule is applied.
+ *
+ * @return the target of the rule, not null.
+ */
+ public Definition getTarget()
+ {
+ return target;
+ }
+
+ /**
+ * Returns the rule how the new content can be composed.
+ * The Definitions should each be applied, the results should
+ * compose a string in the order of the definitions.
+ *
+ * @return the source of the rule, not null.
+ */
+ public List<Definition> getSource()
+ {
+ return source;
+ }
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/BeanPropertyMethodNameTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/BeanPropertyMethodNameTransformer.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/BeanPropertyMethodNameTransformer.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/BeanPropertyMethodNameTransformer.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,166 @@
+package org.apache.torque.gf.source.transform;
+
+/*
+ * 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;
+import org.apache.log4j.Logger;
+import org.apache.torque.gf.control.ControllerState;
+import org.apache.torque.gf.processor.string.Camelbacker;
+import org.apache.torque.gf.source.SourceElement;
+
+/**
+ * Converts a source element Attribute such that it can be used as method name.
+ * The base of the method name is the attribute content, with special characters
+ * removed and case corrected where necessary. Optionally,
+ * a prefix and/or suffix can be added. The result is stored into another
+ * attribute of the same source element.
+ *
+ * @version $Id: $
+ */
+public class BeanPropertyMethodNameTransformer
+ extends SourceTargetAttributeTransformer
+{
+ /** The logger of the class. */
+ private static Logger log
+ = Logger.getLogger(BeanPropertyMethodNameTransformer.class);
+
+ /** The camelbacker used to convert the input string. */
+ private Camelbacker camelbacker = new Camelbacker();
+
+ /** The prefix which is added in front of the input string. */
+ private String prefix = "";
+
+ /** The suffix which is added after the input string. */
+ private String suffix = "";
+
+ /**
+ * Constructor.
+ */
+ public BeanPropertyMethodNameTransformer()
+ {
+ super("name", null);
+ }
+
+ /**
+ * Returns the prefix which is added in front of the input string.
+ *
+ * @return the prefix for the result, not null.
+ */
+ public String getTargetNamePrefix()
+ {
+ return prefix;
+ }
+
+ /**
+ * Sets the prefix which is added in front of the input string.
+ *
+ * @param prefix the prefix for the result, not null.
+ *
+ * @throws NullPointerException if prefix is null.
+ */
+ public void setPrefix(String prefix)
+ {
+ if (prefix == null)
+ {
+ throw new NullPointerException("prefix must not be null");
+ }
+ this.prefix = prefix;
+ log.debug("prefix set to " + prefix);
+ }
+
+ /**
+ * Returns the suffix which is added after the input string.
+ *
+ * @return the suffix for the result, not null.
+ */
+ public String getSuffix()
+ {
+ return suffix;
+ }
+
+ /**
+ * Sets the suffix which is added after of the input string.
+ *
+ * @param prefix the suffix for the result, not null.
+ *
+ * @throws NullPointerException if suffix is null.
+ */
+ public void setSuffix(String suffix)
+ {
+ this.suffix = suffix;
+ log.debug("suffix set to " + suffix);
+ }
+
+ /**
+ * Fills the target attribute according to the settings.
+ *
+ * @param sourceElement the source element to modify, not null.
+ * @param controllerState the controller state.
+ *
+ * @return the modified source element, not null.
+ *
+ * @throws IllegalStateException if targetAttributeName was not set.
+ */
+ public SourceElement transform(
+ SourceElement sourceElement,
+ ControllerState controllerState)
+ {
+ String targetAttributeName = getTargetAttributeName();
+ if (targetAttributeName == null)
+ {
+ throw new IllegalStateException("targetAttributeName is not set");
+ }
+
+ String sourceAttributeName = getSourceAttributeName();
+ Object attributeValue = sourceElement.getAttribute(sourceAttributeName);
+ if (attributeValue == null)
+ {
+ log.debug("Attribute " + sourceAttributeName
+ + " is not set, no changes made");
+ return sourceElement;
+ }
+ String attributeValueString = attributeValue.toString();
+ if (StringUtils.isBlank(attributeValueString))
+ {
+ log.debug("Attribute " + sourceAttributeName
+ + " is blank, no changes made");
+ return sourceElement;
+ }
+
+ if (!isOverwrite()
+ && sourceElement.getAttribute(targetAttributeName) != null)
+ {
+ log.trace("Attribute " + targetAttributeName
+ + " is filled and overwrite is false, no changes made");
+ return sourceElement;
+ }
+
+ camelbacker.setDefaultLowerCase(false);
+ if (StringUtils.isEmpty(prefix))
+ {
+ camelbacker.setFirstCharUppercase(false);
+ }
+ String result = prefix
+ + camelbacker.process(attributeValueString)
+ + suffix;
+ sourceElement.setAttribute(targetAttributeName, result);
+ return sourceElement;
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/CopyAttributeTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/CopyAttributeTransformer.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/CopyAttributeTransformer.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/CopyAttributeTransformer.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,81 @@
+package org.apache.torque.gf.source.transform;
+
+/*
+ * 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.log4j.Logger;
+import org.apache.torque.gf.control.ControllerState;
+import org.apache.torque.gf.source.SourceElement;
+
+/**
+ * Fills an attribute with the content of another attribute.
+ * It can be configured whether the target attribute is overwritten
+ * if it is not set.
+ *
+ * $Id: $
+ */
+public class CopyAttributeTransformer extends SourceTargetAttributeTransformer
+{
+ /** The logger of the class. */
+ private static Logger log
+ = Logger.getLogger(CopyAttributeTransformer.class);
+
+
+ /**
+ * Fills the target attribute according to the settings.
+ *
+ * @param sourceElement the source element to modify, not null.
+ * @param controllerState the controller state.
+ *
+ * @return the modified source element, not null.
+ *
+ * @throws IllegalStateException if sourceAttributeName or
+ * targetAttributeName was not set.
+ */
+ public SourceElement transform(
+ SourceElement sourceElement,
+ ControllerState controllerState)
+ {
+ String sourceAttributeName = getSourceAttributeName();
+ if (sourceAttributeName == null)
+ {
+ throw new IllegalStateException("sourceAttributeName is not set");
+ }
+
+ String targetAttributeName = getTargetAttributeName();
+ if (targetAttributeName == null)
+ {
+ throw new IllegalStateException("targetAttributeName is not set");
+ }
+
+ Object attributeValue = sourceElement.getAttribute(sourceAttributeName);
+ if (attributeValue == null)
+ {
+ log.debug("Attribute " + sourceAttributeName
+ + " is not set, no changes made");
+ return sourceElement;
+ }
+ if (isOverwrite()
+ || sourceElement.getAttribute(targetAttributeName) == null)
+ {
+ sourceElement.setAttribute(targetAttributeName, attributeValue);
+ }
+ return sourceElement;
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/GetterSetterNameTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/GetterSetterNameTransformer.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/GetterSetterNameTransformer.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/GetterSetterNameTransformer.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,180 @@
+package org.apache.torque.gf.source.transform;
+
+/*
+ * 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;
+import org.apache.log4j.Logger;
+import org.apache.torque.gf.control.ControllerState;
+import org.apache.torque.gf.processor.string.Camelbacker;
+import org.apache.torque.gf.source.SourceElement;
+
+/**
+ * Takes a property name as input from a source element attribute
+ * and generates a getter and a setter name from it.
+ *
+ * @version $Id: $
+ */
+public class GetterSetterNameTransformer implements SourceTransformer
+{
+ /** The logger of the class. */
+ private static Logger log
+ = Logger.getLogger(GetterSetterNameTransformer.class);
+
+ /** The camelbacker to convert the property name. */
+ private static Camelbacker camelbacker = new Camelbacker();
+
+ static
+ {
+ camelbacker.setDefaultLowerCase(false);
+ }
+
+ /** The name of the source element attribute. Default is "name". */
+ private String attributeName = "name";
+
+ /**
+ * The name of the target attribute for the getter name.
+ * Default is "getterName".
+ */
+ private String getterNameAttribute = "getterName";
+
+ /**
+ * The name of the target attribute for the setter name.
+ * Default is "setterName".
+ */
+ private String setterNameAttribute = "setterName";
+
+ /**
+ * Returns the name of the source element attribute.
+ *
+ * @return the name of the source attribute, not null.
+ */
+ public String getAttributeName()
+ {
+ return attributeName;
+ }
+
+ /**
+ * Sets the name of the source element attribute.
+ *
+ * @param attributeName the name of the source attribute, not null.
+ *
+ * @throws NullPointerException if attributeName is null.
+ */
+ public void setAttributeName(String attributeName)
+ {
+ if (attributeName == null)
+ {
+ throw new NullPointerException("attributeName must not be null");
+ }
+ this.attributeName = attributeName;
+ }
+
+ /**
+ * Returns the name of the target element attribute for the getter name.
+ *
+ * @return the name of the getter name target attribute, not null.
+ */
+ public String getGetterNameAttribute()
+ {
+ return getterNameAttribute;
+ }
+
+ /**
+ * Sets the name of the target element attribute for the getter name.
+ *
+ * @param getterNameAttribute the name of the getter name target attribute,
+ * not null.
+ *
+ * @throws NullPointerException if getterNameAttribute is null.
+ */
+ public void setGetterNameAttribute(String getterNameAttribute)
+ {
+ if (getterNameAttribute == null)
+ {
+ throw new NullPointerException(
+ "getterNameAttribute must not be null");
+ }
+ this.getterNameAttribute = getterNameAttribute;
+ }
+
+ /**
+ * Returns the name of the target element attribute for the setter name.
+ *
+ * @return the name of the setter name target attribute, not null.
+ */
+ public String getSetterNameAttribute()
+ {
+ return setterNameAttribute;
+ }
+
+ /**
+ * Sets the name of the target element attribute for the setter name.
+ *
+ * @param getterNameAttribute the name of the setter name target attribute,
+ * not null.
+ *
+ * @throws NullPointerException if setterNameAttribute is null.
+ */
+ public void setSetterNameAttribute(String setterNameAttribute)
+ {
+ if (setterNameAttribute == null)
+ {
+ throw new NullPointerException(
+ "setterNameAttribute must not be null");
+ }
+ this.setterNameAttribute = setterNameAttribute;
+ }
+
+ /**
+ * Fills the target attributes according to the settings.
+ *
+ * @param sourceElement the source element to modify, not null.
+ * @param controllerState the controller state.
+ *
+ * @return the modified source element, not null.
+ *
+ * @throws IllegalStateException if sourceAttributeName or
+ * targetAttributeName was not set.
+ */
+ public SourceElement transform(
+ SourceElement sourceElement,
+ ControllerState controllerState)
+ {
+ Object attributeValue = sourceElement.getAttribute(attributeName);
+ if (attributeValue == null)
+ {
+ log.debug("Attribute " + attributeName
+ + " is not set, no changes made");
+ return sourceElement;
+ }
+ String attributeValueString = attributeValue.toString();
+ if (StringUtils.isBlank(attributeValueString))
+ {
+ log.debug("Attribute " + attributeName
+ + " is blank, no changes made");
+ return sourceElement;
+ }
+ String getterName = "get" + camelbacker.process(attributeValueString);
+ sourceElement.setAttribute(getterNameAttribute, getterName);
+ String setterName = "set" + camelbacker.process(attributeValueString);
+ sourceElement.setAttribute(setterNameAttribute, setterName);
+ return sourceElement;
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTargetAttributeTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTargetAttributeTransformer.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTargetAttributeTransformer.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTargetAttributeTransformer.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,171 @@
+package org.apache.torque.gf.source.transform;
+
+/*
+ * 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.log4j.Logger;
+import org.apache.torque.gf.control.ControllerState;
+import org.apache.torque.gf.source.SourceElement;
+
+/**
+ * A transformer which has a source attribute and a target attribute.
+ *
+ * @version $Id: $
+ */
+public abstract class SourceTargetAttributeTransformer
+ implements SourceTransformer
+{
+ /** The logger of the class. */
+ private static final Logger log
+ = Logger.getLogger(SourceTargetAttributeTransformer.class);
+
+ /** The name of the attribute which is read as Input. */
+ private String sourceAttributeName;
+
+ /** The name of the attribute into which the result is stored. */
+ private String targetAttributeName;
+
+ /**
+ * Whether the content of the target attribute should be overwritten
+ * if it exists. Default is true.
+ */
+ private boolean overwrite = true;
+
+ /**
+ * Standard constructor.
+ */
+ public SourceTargetAttributeTransformer()
+ {
+ }
+
+ /**
+ * Constructor used to set default attribute names.
+ *
+ * @param sourceAttributeName the default sourceAttributeName.
+ * @param targetAttributeName the default targetAttributeName.
+ */
+ public SourceTargetAttributeTransformer(
+ String sourceAttributeName,
+ String targetAttributeName)
+ {
+ this.sourceAttributeName = sourceAttributeName;
+ this.targetAttributeName = targetAttributeName;
+ }
+
+ /**
+ * Returns the name of the attribute which is read as Input.
+ *
+ * @return the name of the attribute which is read as Input.
+ */
+ public String getSourceAttributeName()
+ {
+ return sourceAttributeName;
+ }
+
+ /**
+ * Sets the name of the attribute which is read as Input.
+ *
+ * @param sourceAttributeName the name of the attribute which is read
+ * as Input, not null.
+ *
+ * @throws NullPointerException if sourceAttributeName is null.
+ */
+ public void setSourceAttributeName(String sourceAttributeName)
+ {
+ if (sourceAttributeName == null)
+ {
+ throw new NullPointerException(
+ "sourceAttributeName must not be null");
+ }
+ this.sourceAttributeName = sourceAttributeName;
+ log.debug("sourceAttributeName set to " + sourceAttributeName);
+ }
+
+ /**
+ * Returns the name of the attribute into which the result is stored.
+ *
+ * @return the name of the attribute into which the result is stored.
+ */
+ public String getTargetAttributeName()
+ {
+ return targetAttributeName;
+ }
+
+ /**
+ * Sets the name of the attribute into which the result is stored.
+ *
+ * @param targetAttributeName the name of the attribute into which
+ * the result is stored, not null.
+ *
+ * @throws NullPointerException if targetAttributeName is null.
+ */
+ public void setTargetAttributeName(String targetAttributeName)
+ {
+ if (targetAttributeName == null)
+ {
+ throw new NullPointerException(
+ "targetAttributeName must not be null");
+ }
+ this.targetAttributeName = targetAttributeName;
+ log.debug("targetAttributeName set to " + targetAttributeName);
+ }
+
+ /**
+ * Returns whether the content of the target attribute is overwritten
+ * if it exists.
+ *
+ * @return true if the target is overwritten, false if it is only filled
+ * when not set.
+ */
+ public boolean isOverwrite()
+ {
+ return overwrite;
+ }
+
+ /**
+ * Sets whether the content of the target attribute should be overwritten
+ * if it exists.
+ *
+ * @param overwrite true if the target is overwritten, false if it is
+ * only filled when not set.
+ */
+ public void setOverwrite(boolean overwrite)
+ {
+ this.overwrite = overwrite;
+ log.debug("overwrite set to " + overwrite);
+ }
+
+ /**
+ * Performs the transformation.
+ *
+ * @param toTransformRoot the root element of the source tree
+ * to transform, not null.
+ * @param controllerState the state of the controller, not null.
+ *
+ * @return the root element of the transformed source tree, not null.
+ *
+ * @throws SourceTransformerException if the source cannot be transformed.
+ *
+ * @see SourceTransformer#transform(SourceElement, ControllerState)
+ */
+ public abstract SourceElement transform(
+ SourceElement toTransformRoot,
+ ControllerState controllerState)
+ throws SourceTransformerException;
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformer.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformer.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformer.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,47 @@
+package org.apache.torque.gf.source.transform;
+
+/*
+ * 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.torque.gf.control.ControllerState;
+import org.apache.torque.gf.source.SourceElement;
+
+/**
+ * Transforms one source tree into another source tree.
+ */
+public interface SourceTransformer
+{
+ /**
+ * Transforms one source tree into another source tree.
+ * It is allowed to modify the toTransformRoot tree and
+ * return the same tree.
+ *
+ * @param toTransformRoot the root element of the source tree
+ * to transform, not null.
+ * @param controllerState the state of the controller, not null.
+ *
+ * @return the root element of the transformed source tree, not null.
+ *
+ * @throws SourceTransformerException if the source cannot be transformed.
+ */
+ SourceElement transform(
+ SourceElement toTransformRoot,
+ ControllerState controllerState)
+ throws SourceTransformerException;
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformerException.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformerException.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformerException.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/SourceTransformerException.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,79 @@
+package org.apache.torque.gf.source.transform;
+
+/*
+ * 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.
+ */
+
+/**
+ * This exception is the superclass of all Exceptions thrown by
+ * SourceTransformers.
+ */
+public class SourceTransformerException extends Exception
+{
+ /**
+ * The version of the class
+ * (for serialization and deserialization purposes).
+ */
+ private static final long serialVersionUID = 1L;
+
+ /**
+ * Constructs a SourceTransformerException without error message.
+ */
+ public SourceTransformerException()
+ {
+ super();
+ }
+
+ /**
+ * Constructs a SourceTransformerException with the given error message.
+ *
+ * @param message the error message
+ *
+ * @see Exception#Exception(java.lang.String)
+ */
+ public SourceTransformerException(String message)
+ {
+ super(message);
+ }
+
+ /**
+ * Constructs a SourceTransformerException which wraps another exception.
+ *
+ * @param cause The root message.
+ *
+ * @see Exception#Exception(java.lang.Throwable)
+ */
+ public SourceTransformerException(Throwable cause)
+ {
+ super(cause);
+ }
+
+ /**
+ * Constructs a SourceTransformerException which wraps another exception,
+ * and which has its own error message.
+ *
+ * @param message The error message.
+ * @param cause The exception to wrap.
+ *
+ * @see Exception#Exception(java.lang.String, java.lang.Throwable)
+ */
+ public SourceTransformerException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/package.html
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/package.html?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/package.html (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/transform/package.html Tue Feb 16 17:15:43 2010
@@ -0,0 +1,26 @@
+<!--
+ 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 source transformers</title>
+ </head>
+ <body>
+ <p>
+ This package contains the definition of source transformers
+ and some implementations.
+ </p>
+ </body>
+</html>
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/SourceToXml.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/SourceToXml.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/SourceToXml.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/SourceToXml.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,269 @@
+package org.apache.torque.gf.source.xml;
+
+/*
+ * 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.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.torque.gf.source.SourceElement;
+
+/**
+ * Creates an xml String out of a source tree.
+ */
+public class SourceToXml
+{
+ /**
+ * the id of the last created element.
+ */
+ private int idCounter = 0;
+
+ /**
+ * The String by which a child is indented relative to its parent.
+ */
+ private String indent = " ";
+
+ /**
+ * the name of the Id attribute.
+ */
+ private static final String ID_ATTRIBUTE = "id";
+
+ /**
+ * The name of the refid Attribute.
+ */
+ private static final String REFID_ATTRIBUTE = "refid";
+
+ /**
+ * The map with XML special characters as key and their replacement
+ * as value.
+ */
+ private static final Map<Character, String> ESCAPE_MAP;
+ static
+ {
+ Map<Character, String> escapeMap = new HashMap<Character, String>();
+ escapeMap.put('&', "&");
+ escapeMap.put('>', ">");
+ escapeMap.put('<', "<");
+ escapeMap.put('"', """);
+ escapeMap.put('\'', "&apo;");
+ ESCAPE_MAP = Collections.unmodifiableMap(escapeMap);
+ }
+
+ /**
+ * Renders a source element and its children as XML.
+ *
+ * @param rootElement the element to render.
+ *
+ * @return the created XML.
+ */
+ public String toXml(SourceElement rootElement)
+ {
+ Map<SourceElement, Integer> knownElements
+ = new HashMap<SourceElement, Integer>();
+ StringBuilder result = new StringBuilder();
+ String currentIndent = "";
+ outputElement(rootElement, knownElements, result, currentIndent, true);
+ result.append("\n"); // end with newline
+ return result.toString();
+ }
+
+ /**
+ * Renders a source element and its children as XML.
+ *
+ * @param currentElement the element to render.
+ * @param knownElements a map containing the elements which have already
+ * been rendered as key, and their id as value
+ * @param result the buffer to which the XML should be appended.
+ * @param currentIndent the current indentation.
+ */
+ private void outputElement(
+ SourceElement currentElement,
+ Map<SourceElement, Integer> knownElements,
+ StringBuilder result,
+ String currentIndent,
+ boolean identAtStart)
+ {
+ // check whether element is already known, output reference if yes
+ Integer currentId;
+ {
+ Integer knownElementId = knownElements.get(currentElement);
+
+ if (knownElementId != null)
+ {
+ result.append(currentIndent)
+ .append("<")
+ .append(currentElement.getName())
+ .append(" ")
+ .append(REFID_ATTRIBUTE)
+ .append("=\"")
+ .append(knownElementId)
+ .append("\"/>");
+ return;
+ }
+ currentId = getId();
+ knownElements.put(currentElement, currentId);
+ }
+
+ // output start tag
+ if (identAtStart)
+ {
+ result.append(currentIndent);
+ }
+ result.append("<")
+ .append(currentElement.getName());
+ boolean hasTextAttribute = false;
+ for (String attributeName : currentElement.getAttributeNames())
+ {
+ if (attributeName == null) // null is text node
+ {
+ hasTextAttribute = true;
+ }
+ else
+ {
+ result.append(" ")
+ .append(attributeName)
+ .append("=\"");
+ appendWithEscaping(
+ currentElement.getAttribute(attributeName).toString(),
+ result);
+ result.append("\"");
+ }
+ }
+ result.append(" ")
+ .append(ID_ATTRIBUTE)
+ .append("=\"")
+ .append(currentId);
+ boolean hasChildren = !currentElement.getChildren().isEmpty();
+ if (hasChildren || hasTextAttribute)
+ {
+ result.append("\">");
+ if (hasTextAttribute)
+ {
+ appendWithEscaping(
+ currentElement.getAttribute((String) null).toString(),
+ result);
+ }
+ }
+ else
+ {
+ result.append("\"/>");
+ }
+
+ // process children
+ {
+ String childIndent = currentIndent + indent;
+ SourceElement previousChild = null;
+ for (SourceElement child : currentElement.getChildren())
+ {
+ if (!hasTextAttribute)
+ {
+ result.append("\n");
+ }
+ outputElement(
+ child,
+ knownElements,
+ result,
+ childIndent,
+ previousChild != null || !hasTextAttribute);
+ previousChild = child;
+ }
+ if (previousChild != null)
+ {
+ result.append("\n");
+ }
+ }
+
+ if (hasChildren || hasTextAttribute)
+ {
+ //output end tag
+ if (hasChildren)
+ {
+ result.append(currentIndent);
+ }
+ result.append("</")
+ .append(currentElement.getName())
+ .append(">");
+ }
+ }
+
+ /**
+ * returns a unique id.
+ *
+ * @return a unique id.
+ */
+ private synchronized Integer getId()
+ {
+ ++idCounter;
+ return idCounter;
+ }
+
+ /**
+ * Returns the String by which a child element is indented relative to
+ * its parent.
+ *
+ * @return the indent String.
+ */
+ public String getIndent()
+ {
+ return indent;
+ }
+
+ /**
+ * Sets the String by which a child element is indented relative to
+ * its parent.
+ *
+ * @param indent the indent String to set.
+ */
+ public void setIndent(String indent)
+ {
+ if (indent == null)
+ {
+ throw new NullPointerException("indent is null");
+ }
+ this.indent = indent;
+ }
+
+ /**
+ * Appends a string to a string builder. XML special characters in the
+ * string are escaped.
+ *
+ * @param toAppend the string to append to the string builder,
+ * not null.
+ * @param stringBuilder the string builder to append to, not null.
+ */
+ private void appendWithEscaping(
+ String toAppend,
+ StringBuilder stringBuilder)
+ {
+ for (int pos = 0; pos < toAppend.length(); ++pos)
+ {
+ char current = toAppend.charAt(pos);
+ String escapeSequence = ESCAPE_MAP.get(current);
+ if (escapeSequence != null)
+ {
+ stringBuilder.append(escapeSequence);
+ }
+ else
+ {
+ stringBuilder.append(current);
+ }
+ }
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceParser.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceParser.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceParser.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceParser.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,100 @@
+package org.apache.torque.gf.source.xml;
+
+/*
+ * 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.IOException;
+import java.io.InputStream;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.apache.torque.gf.source.SourceElement;
+import org.apache.torque.gf.source.SourceException;
+import org.apache.torque.gf.source.SourceParser;
+import org.xml.sax.SAXException;
+
+/**
+ * Parses a stream containing xml data and creates a source element hierarchy
+ * from it.
+ */
+public class XmlSourceParser implements SourceParser
+{
+ /**
+ * The factory from which the parser is created.
+ */
+ private static final SAXParserFactory SAX_FACTORY;
+
+ static
+ {
+ SAX_FACTORY = SAXParserFactory.newInstance();
+ SAX_FACTORY.setValidating(true);
+ }
+
+ /**
+ * Parses a stream containing xml data and creates a source element
+ * hierarchy from it.
+ *
+ * @param inputStream the stream containing the xml data.
+ *
+ * @return the root element of the created hierarchy.
+ *
+ * @throws SourceException if an error occurs reading the input stream,
+ * parsing the XML data or if the SAX parser is not configured
+ * correctly.
+ */
+ public SourceElement parse(InputStream inputStream)
+ throws SourceException
+ {
+ if (inputStream == null)
+ {
+ throw new NullPointerException("No Input path specified");
+ }
+ try
+ {
+ SAXParser parser = SAX_FACTORY.newSAXParser();
+
+ XmlSourceSaxHandler handler = new XmlSourceSaxHandler();
+
+ parser.parse(inputStream, handler);
+ return handler.getRoot();
+ }
+ catch (IOException e)
+ {
+ throw new SourceException(
+ "Error reading XML source file: " + e.getMessage(),
+ e);
+ }
+ catch (SAXException e)
+ {
+ throw new SourceException(
+ "Error parsing XML source file: " + e.getMessage(),
+ e);
+ }
+ catch (ParserConfigurationException e)
+ {
+ throw new SourceException(
+ "Parser configuration error parsing Properties"
+ + " source file: "
+ + e.getMessage(),
+ e);
+ }
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceSaxHandler.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceSaxHandler.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceSaxHandler.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceSaxHandler.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,131 @@
+package org.apache.torque.gf.source.xml;
+
+/*
+ * 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.torque.gf.source.RichSourceElementImpl;
+import org.apache.torque.gf.source.SourceElement;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * A SAX Handler for creating a source element tree from xml.
+ */
+public class XmlSourceSaxHandler extends DefaultHandler
+{
+ /**
+ * the root element of the tree.
+ */
+ private SourceElement element;
+
+ /**
+ * the currently parsed element.
+ */
+ private SourceElement root;
+
+ @Override
+ public void startElement(String uri, String localName,
+ String qName, Attributes attributes)
+ throws SAXException
+ {
+ SourceElement current = new RichSourceElementImpl(qName);
+ for (int i = 0; i < attributes.getLength(); ++i)
+ {
+ current.setAttribute(
+ attributes.getQName(i), attributes.getValue(i));
+ }
+ if (element != null)
+ {
+ element.addChild(current);
+ }
+ else
+ {
+ root = current;
+ }
+ element = current;
+ }
+
+ @Override
+ public void endElement(String uri, String localName, String qName)
+ throws SAXException
+ {
+ element = element.getParent();
+ }
+
+ @Override
+ public void characters(char[] ch, int start, int length)
+ throws SAXException
+ {
+ String characterData = new String(ch, start, length);
+ if (isWhitespace(characterData))
+ {
+ return;
+ }
+ String textContent = (String) element.getAttribute((String) null);
+ if (textContent != null)
+ {
+ textContent = textContent + characterData;
+ }
+ else
+ {
+ textContent = characterData;
+ }
+ element.setAttribute((String) null, textContent);
+ }
+
+ @Override
+ public void ignorableWhitespace(char[] ch, int start, int length)
+ throws SAXException
+ {
+
+ }
+
+ /**
+ * returns the root element of the tree.
+ *
+ * @return the root element (may be null if no elements were in the xml)
+ * or null if the xml was not yet parsed.
+ */
+ public SourceElement getRoot()
+ {
+ return root;
+ }
+
+ /**
+ * Checks if a String consists only of whitespace
+ *
+ * @param toCheck the String to check.
+ *
+ * @return true if the String only contains whitespace characters, false
+ * if it contains other characters..
+ */
+ private boolean isWhitespace(String toCheck)
+ {
+ for (int i = 0; i < toCheck.length(); ++i)
+ {
+ char ch = toCheck.charAt(i);
+ if (ch != ' ' && ch != '\r' && ch != '\n' && ch != '\t')
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+}
Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceType.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceType.java?rev=910600&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceType.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/gf/source/xml/XmlSourceType.java Tue Feb 16 17:15:43 2010
@@ -0,0 +1,101 @@
+package org.apache.torque.gf.source.xml;
+
+/*
+ * 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.torque.gf.source.SourceParser;
+import org.apache.torque.gf.source.SourceType;
+
+/**
+ * The source type representing an XML source.
+ *
+ * $Id: $
+ */
+public final class XmlSourceType implements SourceType
+{
+ /** The key for a XML source. */
+ private static final String KEY = "xml";
+
+ /** The filename extension for a XML source. */
+ private static final String FILENAME_EXTENSION = "xml";
+
+ /**
+ * Returns an unique key for the source type.
+ *
+ * @return "xml".
+ */
+ public String getKey()
+ {
+ return KEY;
+ }
+
+ /**
+ * Gets the filename extension this source type typically has.
+ *
+ * @return "xml".
+ */
+ public String getFilenameExtension()
+ {
+ return FILENAME_EXTENSION;
+ }
+
+ /**
+ * Returns a parser for this type of source.
+ *
+ * @return a source parser, not null.
+ */
+ public SourceParser getParser()
+ {
+ return new XmlSourceParser();
+ }
+
+ /**
+ * Returns a hash code of this instance consistent with equals..
+ * As all instances of this class are equal to each other,
+ * the hash code is always the same.
+ *
+ * @return 1.
+ */
+ @Override
+ public int hashCode()
+ {
+ return 1;
+ }
+
+ /**
+ * Checks whether other is equal to this instance.
+ * All instances of this class are equal to each other.
+ *
+ * @return true if <code>other</code> is a XmlSourceType, false
+ * otherwise.
+ */
+ @Override
+ public boolean equals(Object other)
+ {
+ if (other == null)
+ {
+ return false;
+ }
+ if (!other.getClass().equals(XmlSourceType.class))
+ {
+ return false;
+ }
+ return true;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org