Home » tiles-2.2.1-src » org.apache.tiles.compat.definition.digester » [javadoc | source]

    1   /*
    2    * $Id: CompatibilityDigesterDefinitionsReader.java 736275 2009-01-21 09:58:20Z apetrelli $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    * http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   
   22   package org.apache.tiles.compat.definition.digester;
   23   
   24   import org.apache.commons.digester.Digester;
   25   import org.apache.tiles.definition.digester.DigesterDefinitionsReader;
   26   
   27   /**
   28    * Digester reader that can read Tiles 1.1, 1.2, 1.3, 1.4 and 2.0 files.
   29    *
   30    * @version $Rev: 736275 $ $Date: 2009-01-21 10:58:20 +0100 (mer, 21 gen 2009) $
   31    * @since 2.1.0
   32    */
   33   public class CompatibilityDigesterDefinitionsReader extends
   34           DigesterDefinitionsReader {
   35       /**
   36        * The set of public identifiers, and corresponding resource names for the
   37        * versions of the configuration file DTDs we know about. There <strong>MUST</strong>
   38        * be an even number of Strings in this list!
   39        *
   40        * @since 2.1.0
   41        */
   42       protected String[] registrations;
   43   
   44       /** {@inheritDoc} */
   45       @Override
   46       protected void initSyntax(Digester digester) {
   47           super.initSyntax(digester);
   48           initDigesterForComponentsDefinitionsSyntax(digester);
   49           initDigesterForInstancesSyntax(digester);
   50           initDigesterForTilesDefinitionsSyntax(digester);
   51       }
   52   
   53       /**
   54        * Init digester for components syntax. This is an old set of rules, left
   55        * for backward compatibility.
   56        *
   57        * @param digester Digester instance to use.
   58        */
   59       private void initDigesterForComponentsDefinitionsSyntax(Digester digester) {
   60           // Common constants
   61           String definitionTag = "component-definitions/definition";
   62   
   63           String putTag = definitionTag + "/put";
   64   
   65           String listTag = definitionTag + "/putList";
   66   
   67           String addListElementTag = listTag + "/add";
   68   
   69           // syntax rules
   70           digester.addObjectCreate(definitionTag, DEFINITION_HANDLER_CLASS);
   71           digester.addRule(definitionTag, new FillDefinitionRule());
   72           digester.addSetNext(definitionTag, "addDefinition",
   73                   DEFINITION_HANDLER_CLASS);
   74           // put / putAttribute rules
   75           digester.addObjectCreate(putTag, PUT_ATTRIBUTE_HANDLER_CLASS);
   76           digester.addRule(putTag, new FillAttributeRule());
   77           digester.addRule(putTag, new PutAttributeRule());
   78           digester.addCallMethod(putTag, "setBody", 0);
   79           // list rules
   80           digester.addObjectCreate(listTag, LIST_HANDLER_CLASS);
   81           digester.addSetProperties(listTag);
   82           digester.addRule(listTag, new PutAttributeRule());
   83           // list elements rules
   84           // We use Attribute class to avoid rewriting a new class.
   85           // Name part can't be used in listElement attribute.
   86           digester.addObjectCreate(addListElementTag, PUT_ATTRIBUTE_HANDLER_CLASS);
   87           digester.addRule(addListElementTag, new FillAttributeRule());
   88           digester.addSetNext(addListElementTag, "add",
   89                   PUT_ATTRIBUTE_HANDLER_CLASS);
   90           digester.addCallMethod(addListElementTag, "setBody", 0);
   91       }
   92   
   93       /**
   94        * Init digester for Tiles syntax. Same as components, but with first
   95        * element = tiles-definitions
   96        *
   97        * @param digester Digester instance to use.
   98        */
   99       private void initDigesterForTilesDefinitionsSyntax(Digester digester) {
  100           // Common constants
  101           String definitionTag = "tiles-definitions/definition";
  102   
  103           String putTag = definitionTag + "/put";
  104   
  105           // String LIST_TAG = DEFINITION_TAG + "/putList";
  106           // List tag value
  107           String listTag = "putList";
  108           String definitionListTag = definitionTag + "/" + listTag;
  109           // Tag value for adding an element in a list
  110           String addListElementTag = "*/" + listTag + "/add";
  111   
  112           // put / putAttribute rules
  113           // Rules for a same pattern are called in order, but rule.end() are
  114           // called
  115           // in reverse order.
  116           // SetNext and CallMethod use rule.end() method. So, placing SetNext in
  117           // first position ensure it will be called last (sic).
  118           digester.addObjectCreate(putTag, PUT_ATTRIBUTE_HANDLER_CLASS);
  119           digester.addRule(putTag, new FillAttributeRule());
  120           digester.addRule(putTag, new PutAttributeRule());
  121           digester.addCallMethod(putTag, "setBody", 0);
  122           // Definition level list rules
  123           // This is rules for lists nested in a definition
  124           digester.addObjectCreate(definitionListTag, LIST_HANDLER_CLASS);
  125           digester.addSetProperties(definitionListTag);
  126           digester.addRule(definitionListTag, new PutAttributeRule());
  127           // list elements rules
  128           // We use Attribute class to avoid rewriting a new class.
  129           // Name part can't be used in listElement attribute.
  130           digester.addObjectCreate(addListElementTag, PUT_ATTRIBUTE_HANDLER_CLASS);
  131           digester.addRule(addListElementTag, new FillAttributeRule());
  132           digester.addSetNext(addListElementTag, "add",
  133                   PUT_ATTRIBUTE_HANDLER_CLASS);
  134           digester.addCallMethod(addListElementTag, "setBody", 0);
  135   
  136           // nested list elements rules
  137           // Create a list handler, and add it to parent list
  138           String nestedList = "*/" + listTag + "/" + listTag;
  139           digester.addObjectCreate(nestedList, LIST_HANDLER_CLASS);
  140           digester.addSetProperties(nestedList);
  141           digester.addSetNext(nestedList, "add", PUT_ATTRIBUTE_HANDLER_CLASS);
  142       }
  143   
  144       /**
  145        * Init digester in order to parse instances definition file syntax.
  146        * Instances is an old name for "definition". This method is left for
  147        * backwards compatibility.
  148        *
  149        * @param digester Digester instance to use.
  150        */
  151       private void initDigesterForInstancesSyntax(Digester digester) {
  152           // Build a digester to process our configuration resource
  153           String instanceTag = "component-instances/instance";
  154   
  155           String putTag = instanceTag + "/put";
  156           String putAttributeTag = instanceTag + "/putAttribute";
  157   
  158           String listTag = instanceTag + "/putList";
  159   
  160           String addListElementTag = listTag + "/add";
  161   
  162           // component instance rules
  163           digester.addObjectCreate(instanceTag, DEFINITION_HANDLER_CLASS);
  164           digester.addRule(instanceTag, new FillDefinitionRule());
  165           digester
  166                   .addSetNext(instanceTag, "addDefinition", DEFINITION_HANDLER_CLASS);
  167           // put / putAttribute rules
  168           digester.addObjectCreate(putAttributeTag, PUT_ATTRIBUTE_HANDLER_CLASS);
  169           digester.addRule(putTag, new FillAttributeRule());
  170           digester.addRule(putTag, new PutAttributeRule());
  171           // put / putAttribute rules
  172           digester.addObjectCreate(putTag, PUT_ATTRIBUTE_HANDLER_CLASS);
  173           digester.addSetProperties(putTag);
  174           digester.addRule(putTag, new PutAttributeRule());
  175           // list rules
  176           digester.addObjectCreate(listTag, PUT_ATTRIBUTE_HANDLER_CLASS);
  177           digester.addSetProperties(listTag);
  178           digester.addRule(listTag, new PutAttributeRule());
  179           // list elements rules
  180           // We use Attribute class to avoid rewriting a new class.
  181           // Name part can't be used in listElement attribute.
  182           digester.addObjectCreate(addListElementTag, PUT_ATTRIBUTE_HANDLER_CLASS);
  183           digester.addRule(addListElementTag, new FillAttributeRule());
  184           digester.addSetNext(addListElementTag, "add", PUT_ATTRIBUTE_HANDLER_CLASS);
  185       }
  186   
  187       /** {@inheritDoc} */
  188       protected String[] getRegistrations() {
  189           if (registrations == null) {
  190               registrations = new String[] {
  191                   "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN",
  192                   "/org/apache/tiles/resources/tiles-config_2_0.dtd",
  193                   "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN",
  194                   "/org/apache/tiles/resources/tiles-config_2_1.dtd",
  195                   "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN",
  196                   "/org/apache/tiles/compat/resources/tiles-config_1_1.dtd",
  197                   "-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN",
  198                   "/org/apache/tiles/compat/resources/tiles-config_1_3.dtd",
  199                   "-//Apache Software Foundation//DTD Tiles Configuration 1.4//EN",
  200                   "/org/apache/tiles/compat/resources/tiles-config_1_4.dtd"};
  201           }
  202           return registrations;
  203       }
  204   }

Home » tiles-2.2.1-src » org.apache.tiles.compat.definition.digester » [javadoc | source]