    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;
    9
    10  import junit.textui.TestRunner;
    11
    12  import java.io.File;
    13
    14  import org.dom4j.io.SAXReader;
    15
    16  /***
    17   * TestEmbeddedHandler
    18   *
    19   * @author <a href="mailto:franz.beil@temis-group.com">FB </a>
    20   */
    21  public class EmbeddedHandlerTest extends AbstractTestCase {
    22      private static final int MAIN_READER = 0;
    23
    24      private static final int ON_END_READER = 1;
    25
    26      protected String[] testDocuments = {"xml/test/FranzBeilMain.xml"};
    27
    28      private StringBuffer[] results = {new StringBuffer(), new StringBuffer()};
    29
    30      protected int test;
    31
    32      public static void main(String[] args) {
    33          TestRunner.run(EmbeddedHandlerTest.class);
    34      }
    35
    36      // ---------------------------------------------
    37      // Test case(s)
    38      // ---------------------------------------------
    39      public void testMainReader() throws Exception {
    40          test = MAIN_READER;
    41          readDocuments();
    42      }
    43
    44      public void testOnEndReader() throws Exception {
    45          test = ON_END_READER;
    46          readDocuments();
    47      }
    48
    49      public void testBothReaders() throws Exception {
    50          testMainReader();
    51          testOnEndReader();
    52
    53          if (!results[MAIN_READER].toString().equals(
    54                  results[ON_END_READER].toString())) {
    55              StringBuffer msg = new StringBuffer();
    56              msg.append("Results of tests should be equal!\n");
    57              msg.append("Results testMainReader():\n"
    58                      + results[MAIN_READER].toString());
    59              msg.append("Results testOnEndReader():\n"
    60                      + results[ON_END_READER].toString());
    61              throw new Exception(msg.toString());
    62          }
    63      }
    64
    65      // ---------------------------------------------
    66      // Implementation methods
    67      // ---------------------------------------------
    68      private void readDocuments() throws Exception {
    69          for (int i = 0; i < testDocuments.length; i++) {
    70              File testDoc = getFile(testDocuments[i]);
    71              String mainDir = testDoc.getParent();
    72              SAXReader reader = new SAXReader();
    73              ElementHandler mainHandler = new MainHandler(mainDir);
    74              reader.addHandler("/main/import", mainHandler);
    75              getDocument(testDocuments[i], reader);
    76          }
    77      }
    78
    79      // Handler classes
    80      // ---------------------------------------------
    81      class MainHandler implements ElementHandler {
    82          private SAXReader mainReader;
    83
    84          private String mainDir;
    85
    86          public MainHandler(String dir) {
    87              mainReader = new SAXReader();
    88              mainDir = dir;
    89              mainReader.addHandler("/import/stuff", new EmbeddedHandler());
    90          }
    91
    92          public void onStart(ElementPath path) {
    93          }
    94
    95          public void onEnd(ElementPath path) {
    96              String href = path.getCurrent().attribute("href").getValue();
    97              Element importRef = path.getCurrent();
    98              Element parentElement = importRef.getParent();
    99              SAXReader onEndReader = new SAXReader();
    100             onEndReader.addHandler("/import/stuff", new EmbeddedHandler());
    101
    102             File file = new File(mainDir + File.separator + href);
    103             Element importElement = null;
    104
    105             try {
    106                 if (test == MAIN_READER) {
    107                     importElement = mainReader.read(file).getRootElement();
    108                 } else if (test == ON_END_READER) {
    109                     importElement = onEndReader.read(file).getRootElement();
    110                 }
    111             } catch (Exception e) {
    112                 // too bad that it's not possible to throw the exception at the
    113                 // caller
    114                 e.printStackTrace();
    115             }
    116
    117             // prune and replace
    118             importRef.detach();
    119             parentElement.add(importElement);
    120         }
    121     }
    122
    123     public class EmbeddedHandler implements ElementHandler {
    124         public void onStart(ElementPath path) {
    125             results[test].append(path.getCurrent().attribute("name").getValue()
    126                     + "\n");
    127         }
    128
    129         public void onEnd(ElementPath path) {
    130         }
    131     }
    132 }
    133
    134 /*
    135  * Redistribution and use of this software and associated documentation
    136  * ("Software"), with or without modification, are permitted provided that the
    137  * following conditions are met:
    138  *
    139  * 1. Redistributions of source code must retain copyright statements and
    140  * notices. Redistributions must also contain a copy of this document.
    141  *
    142  * 2. Redistributions in binary form must reproduce the above copyright notice,
    143  * this list of conditions and the following disclaimer in the documentation
    144  * and/or other materials provided with the distribution.
    145  *
    146  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    147  * from this Software without prior written permission of MetaStuff, Ltd. For
    148  * written permission, please contact dom4j-info@metastuff.com.
    149  *
    150  * 4. Products derived from this Software may not be called "DOM4J" nor may
    151  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    152  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    153  *
    154  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    155  *
    156  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    157  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    158  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    159  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    160  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    161  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    162  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    163  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    164  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    165  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    166  * POSSIBILITY OF SUCH DAMAGE.
    167  *
    168  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    169  */