StylesheetTest xref

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j.rule;
    9
    10  import junit.textui.TestRunner;
    11
    12  import org.dom4j.AbstractTestCase;
    13  import org.dom4j.Document;
    14  import org.dom4j.DocumentHelper;
    15  import org.dom4j.Node;
    16  import org.dom4j.xpath.DefaultXPath;
    17
    18  /***
    19   * A test harness to test the use of the Stylesheet and the XSLT rule engine.
    20   *
    21   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    22   * @version $Revision: 1.4 $
    23   */
    24  public class StylesheetTest extends AbstractTestCase {
    25      protected String[] templates = {
    26              "/",
    27              "*",
    28              "root",
    29              "author",
    30              "@name",
    31              "root/author",
    32              "author[@location='UK']",
    33              "root/author[@location='UK']",
    34              "root//author[@location='UK']"};
    35
    36      protected String[] templates2 = {"/", "title", "para", "*"};
    37
    38      protected Stylesheet stylesheet;
    39
    40      public static void main(String[] args) {
    41          TestRunner.run(StylesheetTest.class);
    42      }
    43
    44      // Test case(s)
    45      // -------------------------------------------------------------------------
    46      public void testRules() throws Exception {
    47          for (int i = 0, size = templates.length; i < size; i++) {
    48              addTemplate(templates[i]);
    49          }
    50
    51          log("");
    52          log("........................................");
    53          log("");
    54          log("Running stylesheet");
    55
    56          stylesheet.run(document);
    57
    58          log("Finished");
    59      }
    60
    61      public void testLittleDoc() throws Exception {
    62          for (int i = 0, size = templates2.length; i < size; i++) {
    63              addTemplate(templates2[i]);
    64          }
    65          Document doc = getDocument("/xml/test/littledoc.xml");
    66
    67          stylesheet = new Stylesheet();
    68          stylesheet.setValueOfAction(new Action() {
    69              public void run(Node node) {
    70                  log("Default ValueOf action on node: " + node);
    71                  log("........................................");
    72              }
    73          });
    74
    75          stylesheet.run(doc);
    76      }
    77
    78      public void testFireRuleForNode() throws Exception {
    79          final StringBuffer b = new StringBuffer();
    80
    81          final Stylesheet s = new Stylesheet();
    82          Pattern pattern = DocumentHelper.createPattern("url");
    83          Action action = new Action() {
    84              public void run(Node node) throws Exception {
    85                  b.append("url");
    86                  s.applyTemplates(node);
    87              }
    88          };
    89
    90          Rule r = new Rule(pattern, action);
    91          s.addRule(r);
    92
    93          s.applyTemplates(document, new DefaultXPath("root/author/url"));
    94
    95          assertEquals("Check url is processed twice", "urlurl", b.toString());
    96      }
    97
    98      // Implementation methods
    99      // -------------------------------------------------------------------------
    100     protected void setUp() throws Exception {
    101         super.setUp();
    102
    103         stylesheet = new Stylesheet();
    104         stylesheet.setValueOfAction(new Action() {
    105             public void run(Node node) {
    106                 log("Default ValueOf action on node: " + node);
    107                 log("........................................");
    108             }
    109         });
    110     }
    111
    112     protected void addTemplate(final String match) {
    113         log("Adding template match: " + match);
    114
    115         Pattern pattern = DocumentHelper.createPattern(match);
    116
    117         log("Pattern: " + pattern);
    118         log("........................................");
    119
    120         Action action = new Action() {
    121             public void run(Node node) throws Exception {
    122                 log("Matched pattern: " + match);
    123                 log("Node: " + node.asXML());
    124                 log("........................................");
    125
    126                 // apply any child templates
    127                 stylesheet.applyTemplates(node);
    128             }
    129         };
    130
    131         Rule rule = new Rule(pattern, action);
    132         stylesheet.addRule(rule);
    133     }
    134 }
    135
    136 /*
    137  * Redistribution and use of this software and associated documentation
    138  * ("Software"), with or without modification, are permitted provided that the
    139  * following conditions are met:
    140  *
    141  * 1. Redistributions of source code must retain copyright statements and
    142  * notices. Redistributions must also contain a copy of this document.
    143  *
    144  * 2. Redistributions in binary form must reproduce the above copyright notice,
    145  * this list of conditions and the following disclaimer in the documentation
    146  * and/or other materials provided with the distribution.
    147  *
    148  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    149  * from this Software without prior written permission of MetaStuff, Ltd. For
    150  * written permission, please contact dom4j-info@metastuff.com.
    151  *
    152  * 4. Products derived from this Software may not be called "DOM4J" nor may
    153  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    154  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    155  *
    156  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    157  *
    158  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    159  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    160  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    161  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    162  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    163  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    164  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    165  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    166  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    167  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    168  * POSSIBILITY OF SUCH DAMAGE.
    169  *
    170  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    171  */