View Javadoc

1   package de.matthias_burbach.mosaique.core.parser;
2   
3   import java.io.InputStreamReader;
4   import java.io.OutputStreamWriter;
5   import java.net.URL;
6   import java.util.ArrayList;
7   import java.util.HashMap;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.Map;
11  
12  import au.id.jericho.lib.html.Attributes;
13  import au.id.jericho.lib.html.Element;
14  import au.id.jericho.lib.html.Source;
15  import au.id.jericho.lib.html.Util;
16  import de.matthias_burbach.mosaique.core.model.Context;
17  import de.matthias_burbach.mosaique.core.model.Definition;
18  import de.matthias_burbach.mosaique.core.model.Put;
19  import de.matthias_burbach.mosaique.core.model.TilesDefinitions;
20  import de.matthias_burbach.mosaique.core.util.FileUtils;
21  
22  /***
23   * @author Matthias Burbach
24   */
25  public class TilesDefinitionsParser {
26      /***
27       * Parses a Tiles definitions configuration from a file into the internal
28       * representation.
29       *
30       * @param path The full path of the Tiles definitions to parse.
31       * @param context The context to inject into parsed objects.
32       * @return The internal representation of the successfully parsed Tiles
33       *         definitions.
34       * @throws Exception if anything goes wrong
35       */
36      public TilesDefinitions parse(
37              final String path, final Context context) throws Exception {
38          TilesDefinitions tilesDefinitions = new TilesDefinitions();
39          try {
40              URL sourceUrl = new URL("file:" + path);
41              String htmlText = Util.getString(
42                      new InputStreamReader(sourceUrl.openStream()));
43              Source source = new Source(htmlText);
44              source.setLogWriter(new OutputStreamWriter(System.err));
45  
46              String name = FileUtils.getNameWithoutPath(path);
47              tilesDefinitions.setName(name);
48              Map definitions = parseDefinitions(source, path, context);
49              tilesDefinitions.setDefinitionsByName(definitions);
50          } catch (Exception e) {
51              e.printStackTrace();
52          }
53          return tilesDefinitions;
54      }
55  
56      /***
57       * Parses the definition elements in the
58       * Tiles definitions file currently being parsed.
59       *
60       * @param source The Tiles definitions XML document currenly being parsed.
61       * @param filePath The full path of the file being parsed.
62       * @param context The context to inject into parsed objects.
63       * @return The list of definitions of type {@link Definition}.
64       * @throws Exception if anything goes wrong
65       */
66      private Map parseDefinitions(
67              final Source source,
68              final String filePath,
69              final Context context)
70              throws Exception {
71          Map definitions = new HashMap();
72          List definitionElements = source.findAllElements("definition");
73          for (Iterator iter = definitionElements.iterator(); iter.hasNext();) {
74              Element definitionElement = (Element) iter.next();
75              Attributes attributes = definitionElement.getAttributes();
76              Definition definition = new Definition(context);
77              String name = attributes.get("name").getValue();
78              definition.setName(name);
79              definition.setFilePath(filePath);
80              definition.setBegin(definitionElement.getBegin());
81              definition.setEnd(definitionElement.getEnd());
82              if (attributes.get("extends") != null) {
83                  String extendz = attributes.get("extends").getValue();
84                  definition.setExtendz(extendz);
85              }
86              if (attributes.get("page") != null) {
87                  String page = attributes.get("page").getValue();
88                  definition.setPage(page);
89              }
90              try {
91                  List puts =
92                      parsePuts(definitionElement, filePath, definition, context);
93                  definition.setPuts(puts);
94              } catch (Exception e) {
95                  e.printStackTrace();
96                  definition.setErroneous(true);
97                  definition.setMessage(
98                      "Could not successfully parse "
99                      + "the puts of this definition.");
100             }
101             definitions.put(name, definition);
102         }
103         return definitions;
104     }
105 
106     /***
107      * Parses the put elements in the
108      * Tiles definition element currently being parsed.
109      *
110      * @param definitionElement The Tiles definition XML element currenly being
111      *                          parsed.
112      * @param filePath The full path of the file being parsed.
113      * @param owner The definition that defines the puts to parse.
114      * @param context The context to inject into parsed objects.
115      * @return The list of puts of type {@link Put}.
116      * @throws Exception if anything goes wrong
117      */
118     private List parsePuts(
119             final Element definitionElement,
120             final String filePath,
121             final Definition owner,
122             final Context context)
123             throws Exception {
124         List puts = new ArrayList();
125         List putElements = definitionElement.findAllElements("put");
126         for (Iterator iter = putElements.iterator(); iter.hasNext();) {
127             Element putElement = (Element) iter.next();
128             Attributes attributes = putElement.getAttributes();
129             Put put = new Put(context);
130             String name = attributes.get("name").getValue();
131             try {
132                 put.setName(name);
133                 String value = attributes.get("value").getValue();
134                 put.setValue(value);
135                 put.setFilePath(filePath);
136                 put.setBegin(putElement.getBegin());
137                 put.setEnd(putElement.getEnd());
138                 put.setOwner(owner);
139             } catch (Exception e) {
140                 e.printStackTrace();
141                 System.err.println("file:" + filePath);
142                 System.err.println("definition:" + owner.getName());
143                 put.setErroneous(true);
144                 put.setMessage("Could not successfully parse this put.");
145             }
146             puts.add(put);
147         }
148         return puts;
149     }
150 }