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
;
38 protected LinkedList
<Point2f
> corners
;
39 protected Route route
;
40 protected boolean inStrings
;
41 protected String strId
;
42 protected String strLang
;
46 public MemlParser (MuseumEnvironment me
)
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
));
72 BnacLog
.trace("MemlParser.startElement %s %s %s >><<",
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")),
84 if (attrs
.getValue("name") != null) {
85 hall
.setName(attrs
.getValue("name"));
87 corners
= new LinkedList
<Point2f
>();
88 } else if (qName
.equals("link")) {
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")) {
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"));
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]));
120 PlacedCapture pc
= new PlacedCapture(
121 attrs
.getValue("id"),
122 me
.getCapture(attrs
.getValue("ref")),
124 Float
.parseFloat(attrs
.getValue("x")),
125 Float
.parseFloat(attrs
.getValue("y"))),
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]));
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();
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
);
170 } else if (qName
.equals("startingPoint")) {
171 if (corners
!= null) {
172 hall
.setCorners(corners
.toArray(new Point2f
[0]));
176 hall
.addStartingPoint(
177 attrs
.getValue("id"),
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
)
190 if (qName
.equals("painting")) {
193 } else if (qName
.equals("capture2D")) {
196 } else if (qName
.equals("hall")) {
199 } else if (qName
.equals("route")) {
202 } else if (qName
.equals("strings")) {
204 } else if (txt
!= null) {
205 String key
= Util
.capitalize(qName
);
208 } else if (cap
!= null) {
210 } else if (hall
!= null) {
212 } else if (qName
.equals("title")) {
213 me
.set("Title", txt
);
214 } else if (route
!= null && qName
.equals("capture")) {
215 route
.addCapture(txt
);
221 public void characters (char[] ch
, int idx
, int len
)
224 txt
= new String(ch
, idx
, len
);