001package org.apache.commons.digester3.examples.api.documentmarkup; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020import java.io.StringReader; 021 022/** 023 * A simple "test harness" which demonstrates how the MarkupDigester class 024 * (plus the supporting interface/rule classes) can process "document-markup" 025 * style xml data. 026 * <p> 027 * See the readme file included with this example for more information. 028 */ 029public class Main 030{ 031 032 /** The input xml to be parsed by this example. */ 033 String in = "<p>Hi, this is an <i>example</i> of some <b>bold</b> text.</p>"; 034 035 /** Invoked when a text segment is present in the parsed input. */ 036 public void addSegment( String text ) 037 { 038 System.out.println( "Text segment: [" + text + "]" ); 039 } 040 041 /** Invoked when an <i> node is found in the parsed input. */ 042 public void addItalic( String text ) 043 { 044 System.out.println( "Italic: [" + text + "]" ); 045 } 046 047 /** Invoked when an <b> node is found in the parsed input. */ 048 public void addBold( String text ) 049 { 050 System.out.println( "Bold: [" + text + "]" ); 051 } 052 053 /** 054 * Invoked via a standard Digester CallMethodRule, passing the 055 * "body text" of the top-level xml element. This demonstrates 056 * the default behaviour of Digester (which is not suitable for 057 * processing markup-style xml). 058 */ 059 public void addAllText( String text ) 060 { 061 System.out.println( "And the merged text for the p element is [" + text + "]" ); 062 } 063 064 /** 065 * Main method of this test harness. Set up some digester rules, 066 * then parse the input xml contained in the "in" member variable. 067 * The rules cause methods on this object to be invoked, which just 068 * dump information to standard output, to show the callbacks that 069 * a real program could arrange to get when parsing markup input. 070 */ 071 public void run() 072 throws Exception 073 { 074 System.out.println( "Started." ); 075 MarkupDigester d = new MarkupDigester(); 076 077 d.push( this ); 078 079 SetTextSegmentRule r = new SetTextSegmentRule( "addSegment" ); 080 d.addRule( "p", r ); 081 d.addCallMethod( "p", "addAllText", 0 ); 082 083 d.addCallMethod( "p/i", "addItalic", 0 ); 084 d.addCallMethod( "p/b", "addBold", 0 ); 085 086 d.parse( new StringReader( in ) ); 087 088 System.out.println( "Finished." ); 089 } 090 091 /** See the run method. */ 092 public static void main( String[] args ) 093 throws Exception 094 { 095 new Main().run(); 096 } 097 098}