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.Iterator;
8 import java.util.List;
9
10 import au.id.jericho.lib.html.Attribute;
11 import au.id.jericho.lib.html.Attributes;
12 import au.id.jericho.lib.html.Element;
13 import au.id.jericho.lib.html.Source;
14 import au.id.jericho.lib.html.Util;
15 import de.matthias_burbach.mosaique.core.model.Context;
16 import de.matthias_burbach.mosaique.core.model.Insert;
17 import de.matthias_burbach.mosaique.core.model.Jsp;
18 import de.matthias_burbach.mosaique.core.model.Put;
19 import de.matthias_burbach.mosaique.core.util.FileUtils;
20
21 /***
22 * @author Matthias Burbach
23 */
24 public class JspParser {
25 /***
26 * Parses a JSP from a file into the internal representation.
27 *
28 * @param path The full path of the JSP to parse.
29 * @param context The context to inject into parsed objects.
30 * @return The internal representation of the successfully parsed JSP.
31 */
32 public Jsp parse(final String path, final Context context) {
33 Jsp jsp = new Jsp();
34 try {
35 URL sourceUrl = new URL("file:" + path);
36 String htmlText = Util.getString(
37 new InputStreamReader(sourceUrl.openStream()));
38 Source source = new Source(htmlText);
39 source.setLogWriter(new OutputStreamWriter(System.err));
40
41 String name = FileUtils.getNameWithoutPath(path);
42 jsp.setName(name);
43 jsp.setFilePath(path);
44 jsp.setBegin(1);
45 jsp.setEnd(1);
46 List inserts = parseInserts(source, path, context);
47 jsp.setInserts(inserts);
48 } catch (Exception e) {
49 e.printStackTrace();
50 jsp.setErroneous(true);
51 jsp.setMessage("Could not successfully parse this JSP.");
52 }
53 return jsp;
54 }
55
56 /***
57 * Parses the tiles:insert elements in the JSP currently being parsed.
58 *
59 * @param source The source segment of the JSP to parse.
60 * @param filePath The full path of the JSP being parsed.
61 * @param context The context to inject into parsed objects.
62 * @return The list of insert objects of type {@link Insert}.
63 */
64 private List parseInserts(
65 final Source source, final String filePath, final Context context) {
66 List inserts = new ArrayList();
67 List insertElements = source.findAllElements("tiles:insert");
68 for (Iterator iter = insertElements.iterator(); iter.hasNext();) {
69 Element insertElement = (Element) iter.next();
70 Attributes attributes = insertElement.getAttributes();
71 Attribute attribute = attributes.get("attribute");
72 Attribute definition = attributes.get("definition");
73 Insert insert = new Insert(context);
74 try {
75 insert.setFilePath(filePath);
76 insert.setBegin(insertElement.getBegin());
77 insert.setEnd(insertElement.getEnd());
78 if (attribute != null) {
79 insert.setAttribute(attribute.getValue());
80 } else if (definition != null) {
81 insert.setAttribute(definition.getValue());
82 }
83 List puts = parsePuts(insertElement, filePath, context);
84 insert.setPuts(puts);
85 } catch (Exception e) {
86 e.printStackTrace();
87 insert.setErroneous(true);
88 insert.setMessage("Could not successfully parse this insert.");
89 }
90 inserts.add(insert);
91 }
92 return inserts;
93 }
94
95 /***
96 * Parses the tiles:put elements in the JSP currently being parsed.
97 *
98 * @param sourceSegment The source segment of the JSP to parse.
99 * @param filePath The full path of the JSP being parsed.
100 * @param context The context to inject into parsed objects.
101 * @return The list of put objects of type {@link Put}.
102 */
103 private List parsePuts(
104 final Element sourceSegment,
105 final String filePath,
106 final Context context) {
107 List puts = new ArrayList();
108 List putElements = sourceSegment.findAllElements("tiles:put");
109 for (Iterator iter = putElements.iterator(); iter.hasNext();) {
110 Element putElement = (Element) iter.next();
111 Attributes attributes = putElement.getAttributes();
112 Attribute attribute = attributes.get("name");
113 Put put = new Put(context);
114 put.setFilePath(filePath);
115 put.setBegin(putElement.getBegin());
116 put.setEnd(putElement.getEnd());
117 put.setName(attribute.getValue());
118 put.setDynamicPut(true);
119 puts.add(put);
120 }
121 return puts;
122 }
123 }