View Javadoc

1   package org.apache.commons.digester3.examples.api.documentmarkup;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */ 
19  
20  import java.io.StringReader;
21  
22  /** 
23   * A simple "test harness" which demonstrates how the MarkupDigester class
24   * (plus the supporting interface/rule classes) can process "document-markup"
25   * style xml data.
26   * <p>
27   * See the readme file included with this example for more information.
28   */
29  public class Main
30  {
31  
32      /** The input xml to be parsed by this example. */
33      String in = "<p>Hi, this is an <i>example</i> of some <b>bold</b> text.</p>";
34  
35      /** Invoked when a text segment is present in the parsed input. */
36      public void addSegment( String text )
37      {
38          System.out.println( "Text segment: [" + text + "]" );
39      }
40  
41      /** Invoked when an &lt;i&gt; node is found in the parsed input. */
42      public void addItalic( String text )
43      {
44          System.out.println( "Italic: [" + text + "]" );
45      }
46  
47      /** Invoked when an &lt;b&gt; node is found in the parsed input. */
48      public void addBold( String text )
49      {
50          System.out.println( "Bold: [" + text + "]" );
51      }
52  
53      /** 
54       * Invoked via a standard Digester CallMethodRule, passing the 
55       * "body text" of the top-level xml element. This demonstrates
56       * the default behaviour of Digester (which is not suitable for
57       * processing markup-style xml). 
58       */
59      public void addAllText( String text )
60      {
61          System.out.println( "And the merged text for the p element is [" + text + "]" );
62      }
63  
64      /**
65       * Main method of this test harness. Set up some digester rules,
66       * then parse the input xml contained in the "in" member variable.
67       * The rules cause methods on this object to be invoked, which just
68       * dump information to standard output, to show the callbacks that
69       * a real program could arrange to get when parsing markup input.
70       */
71      public void run()
72          throws Exception
73      {
74          System.out.println( "Started." );
75          MarkupDigester d = new MarkupDigester();
76  
77          d.push( this );
78  
79          SetTextSegmentRule r = new SetTextSegmentRule( "addSegment" );
80          d.addRule( "p", r );
81          d.addCallMethod( "p", "addAllText", 0 );
82  
83          d.addCallMethod( "p/i", "addItalic", 0 );
84          d.addCallMethod( "p/b", "addBold", 0 );
85  
86          d.parse( new StringReader( in ) );
87  
88          System.out.println( "Finished." );
89      }
90  
91      /** See the run method. */
92      public static void main( String[] args )
93          throws Exception
94      {
95          new Main().run();
96      }
97  
98  }