* BnacLog.java: changed logger class from org.apache.log4j.Logger to
[bnac-common.git] / src / cl.uchile.dcc.bnac / MemlParser.java
blob2315f15b9a50f9c27f9fc7819d4db9f7d531b7d8
1 /*
2 * bnac : virtual museum environment creation/manipulation project
3 * Copyright (C) 2010 Felipe CaƱas Sabat
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 package cl.uchile.dcc.bnac;
21 import java.io.IOException;
23 import java.util.LinkedList;
25 import javax.vecmath.Point2f;
27 import org.xml.sax.SAXException;
28 import org.xml.sax.Attributes;
29 import org.xml.sax.ext.DefaultHandler2;
31 public class MemlParser extends DefaultHandler2
33 public MuseumEnvironment me;
35 protected CObject obj;
36 protected Capture cap;
37 protected Hall hall;
38 protected LinkedList<Point2f> corners;
39 protected Route route;
40 protected boolean inStrings;
41 protected String strId;
42 protected String strLang;
44 protected String txt;
46 public MemlParser (MuseumEnvironment me)
48 this.me = me;
49 obj = null;
50 cap = null;
51 hall = null;
52 corners = null;
53 route = null;
54 inStrings = false;
55 strId = null;
56 strLang = null;
58 txt = null;
61 public void startElement (String uri, String lName, String qName,
62 Attributes attrs) throws SAXException
64 if (attrs.getLength() > 0) {
65 BnacLog.trace("MemlParser.startElement %s >>", qName);
66 for (int i=0; i<attrs.getLength(); ++i) {
67 BnacLog.trace("%s=%s",
68 attrs.getQName(i), attrs.getValue(i));
70 BnacLog.trace("<<");
71 } else {
72 BnacLog.trace("MemlParser.startElement %s %s %s >><<",
73 uri, lName, qName);
75 if (qName.equals("painting")) {
76 obj = new CObject(attrs.getValue("id"));
77 } else if (qName.equals("capture2D")) {
78 cap = new Capture2D(attrs.getValue("id"),
79 me.getObject(attrs.getValue("ref")));
80 } else if (qName.equals("hall")) {
81 hall = new Hall(attrs.getValue("id"),
82 Float.parseFloat(attrs.getValue("height")),
83 new Point2f[0]);
84 if (attrs.getValue("name") != null) {
85 hall.setName(attrs.getValue("name"));
87 corners = new LinkedList<Point2f>();
88 } else if (qName.equals("link")) {
89 me.addLink(new Link(
90 attrs.getValue("id"),
91 attrs.getValue("door1"), attrs.getValue("door2")));
92 } else if (qName.equals("route")) {
93 route = new Route(attrs.getValue("id"));
94 } else if (qName.equals("strings")) {
95 inStrings = true;
96 } else if (qName.equals("elem")) {
97 strId = attrs.getValue("id");
98 } else if (qName.equals("entry")) {
99 strLang = attrs.getValue("lang");
100 } else if (qName.equals("pair") && inStrings) {
101 me.addString(strId, strLang, attrs.getValue("name"),
102 attrs.getValue("value"));
103 } else {
104 if (obj != null) {
105 } else if (cap != null) {
106 if (qName.equals("resource")) {
107 cap.set("Type", attrs.getValue("type"));
109 } else if (hall != null) {
110 if (qName.equals("corner")) {
111 corners.add(new Point2f(
112 Float.parseFloat(attrs.getValue("x")),
113 Float.parseFloat(attrs.getValue("y"))));
114 } else if (qName.equals("placedCapture")) {
115 if (corners != null) {
116 hall.setCorners(corners.toArray(new Point2f[0]));
117 corners = null;
120 PlacedCapture pc = new PlacedCapture(
121 attrs.getValue("id"),
122 me.getCapture(attrs.getValue("ref")),
123 new Point2f(
124 Float.parseFloat(attrs.getValue("x")),
125 Float.parseFloat(attrs.getValue("y"))),
126 0.0f, 1.0f);
127 if (attrs.getValue("angle") != null) {
128 pc.angle = (float) -Math.toRadians(
129 Float.parseFloat(attrs.getValue("angle")));
131 if (attrs.getValue("scale") != null) {
132 pc.scale = Float.parseFloat(attrs.getValue("scale"));
134 hall.getWall(Integer.parseInt(attrs.getValue("wall"))).
135 addPlacedCapture(pc);
136 } else if (qName.equals("door")) {
137 if (corners != null) {
138 hall.setCorners(corners.toArray(new Point2f[0]));
139 corners = null;
142 String id = attrs.getValue("id");
143 Wall wall = hall.getWall(
144 Integer.parseInt(attrs.getValue("wall")));
145 Door d = new Door(id);
146 Point2f dims = d.getDimensions();
147 Point2f pos = d.pos;
148 if (attrs.getValue("w") != null) {
149 dims.x = Float.parseFloat(attrs.getValue("w"));
151 if (attrs.getValue("h") != null) {
152 dims.y = Float.parseFloat(attrs.getValue("h"));
154 if (attrs.getValue("x") != null) {
155 pos.x = Float.parseFloat(attrs.getValue("x"));
157 if (attrs.getValue("y") != null) {
158 pos.y = Float.parseFloat(attrs.getValue("y"));
160 if (attrs.getValue("angle") != null) {
161 d.setAngle((float) -Math.toRadians(
162 Float.parseFloat(attrs.getValue("angle"))));
164 if (attrs.getValue("scale") != null) {
165 d.setScale(Float.parseFloat(attrs.getValue("scale")));
167 d.setDimensions(dims.x, dims.y);
168 d.setPosition(pos.x, pos.y);
169 wall.addDoor(d);
170 } else if (qName.equals("startingPoint")) {
171 if (corners != null) {
172 hall.setCorners(corners.toArray(new Point2f[0]));
173 corners = null;
176 hall.addStartingPoint(
177 attrs.getValue("id"),
178 new Point2f(
179 Float.parseFloat(attrs.getValue("x")),
180 Float.parseFloat(attrs.getValue("y"))),
181 Float.parseFloat(attrs.getValue("angle")));
187 public void endElement (String uri, String lName, String qName)
188 throws SAXException
190 if (qName.equals("painting")) {
191 me.addObject(obj);
192 obj = null;
193 } else if (qName.equals("capture2D")) {
194 me.addCapture(cap);
195 cap = null;
196 } else if (qName.equals("hall")) {
197 me.addHall(hall);
198 hall = null;
199 } else if (qName.equals("route")) {
200 me.addRoute(route);
201 route = null;
202 } else if (qName.equals("strings")) {
203 inStrings = false;
204 } else if (txt != null) {
205 String key = Util.capitalize(qName);
206 if (obj != null) {
207 obj.set(key, txt);
208 } else if (cap != null) {
209 cap.set(key, txt);
210 } else if (hall != null) {
211 hall.set(key, txt);
212 } else if (qName.equals("title")) {
213 me.set("Title", txt);
214 } else if (route != null && qName.equals("capture")) {
215 route.addCapture(txt);
218 txt = null;
221 public void characters (char[] ch, int idx, int len)
222 throws SAXException
224 txt = new String(ch, idx, len);