1 // $Id: Interpreter.java,v 1.4 2004/02/12 22:29:32 powerpete Exp $
2 // [JMP, 02.02.2004] Created this file.
3 package org.jface.stepmt.core;
4
5 import org.jface.stepmt.core.util.ContentHandlerFacade;
6 import org.jface.stepmt.core.util.TextRegion;
7 import org.xml.sax.ContentHandler;
8 import org.xml.sax.SAXException;
9
10 /***
11 * The {@link Interpreter} is the central class of the core component. It
12 * is the actual generator of the XML code. The generation of the XML code is
13 * based on the given root {@link Region} and the <tt>text</tt>.
14 * <p>
15 * The root {@link Region} contains the root node of the regions defined
16 * in the template.
17 *
18 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
19 * @version $Id: Interpreter.java,v 1.4 2004/02/12 22:29:32 powerpete Exp $
20 */
21 public class Interpreter
22 {
23 private ContentHandlerFacade facade = new ContentHandlerFacade();
24 private Region region;
25 private String text;
26 private ContentHandler consumer;
27
28 /***
29 * Generates the XML code using the given {@link ContentHandler}.
30 *
31 * @param consumer The {@link ContentHandler} used to recieve SAX events.
32 * @param region The root {@link Region} of the document's template.
33 * @param text The plain text that is parsed depending on the
34 * root {@link Region}. For the root and all sub-regions
35 * XML elements are created (if available in the plain
36 * text).
37 *
38 * @throws SAXException If something went wrong during the XML generation.
39 */
40 public void generate()
41 throws SAXException
42 {
43 facade.setContentHandler(consumer);
44 facade.start();
45 generate(region, text);
46 facade.end();
47 }
48
49 public void setRegion(Region region)
50 {
51 this.region = region;
52 }
53
54 public void setText(String text)
55 {
56 this.text = text;
57 }
58
59 public void setConsumer(ContentHandler consumer)
60 {
61 this.consumer = consumer;
62 }
63
64 private void generate(Region region, String text) throws SAXException
65 {
66 String regionName = region.getName();
67 facade.start(regionName);
68 TextRegion textRegion = null;
69 int position = 1;
70 while ((textRegion = region.findBestTextRegion(text, position++))
71 != null)
72 {
73 generate(textRegion.getRegion(), textRegion.getText());
74 text = textRegion.getTextAfter();
75 }
76 facade.text(text);
77 facade.end(regionName);
78 }
79 }
This page was automatically generated by Maven