|
| 1 | +package parser; |
| 2 | + |
| 3 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 4 | +import javax.xml.parsers.DocumentBuilder; |
| 5 | +import javax.xml.parsers.ParserConfigurationException; |
| 6 | + |
| 7 | +import org.w3c.dom.*; |
| 8 | +import org.xml.sax.SAXException; |
| 9 | + |
| 10 | +import java.io.File; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * This class parses XML data to data structures relevant to a Scene object |
| 16 | + * |
| 17 | + * @author Benjamin Mamistvalov, Eyal Nathan |
| 18 | + */ |
| 19 | +public class SceneDescriptor { |
| 20 | + private final Map<String, String> sceneAttributes; |
| 21 | + private final Map<String, String> ambientLightAttributes; |
| 22 | + private List<Map<String, String>> planes; |
| 23 | + private List<Map<String, String>> spheres; |
| 24 | + private List<Map<String, String>> triangles; |
| 25 | + private final Element root; |
| 26 | + private Element geometries; |
| 27 | + |
| 28 | + /** |
| 29 | + * Constructor to initialize this class and start parsing the XML file |
| 30 | + * |
| 31 | + * @param filePath XML file path |
| 32 | + * @throws ParserConfigurationException . |
| 33 | + * @throws IOException . |
| 34 | + * @throws SAXException . |
| 35 | + */ |
| 36 | + public SceneDescriptor(String filePath) |
| 37 | + throws ParserConfigurationException, IOException, SAXException { |
| 38 | + this.sceneAttributes = new HashMap<>(); |
| 39 | + this.ambientLightAttributes = new HashMap<>(); |
| 40 | + |
| 41 | + File file = new File(filePath); |
| 42 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 43 | + DocumentBuilder db = dbf.newDocumentBuilder(); |
| 44 | + Document doc = db.parse(file); |
| 45 | + this.root = doc.getDocumentElement(); |
| 46 | + this.planes = null; |
| 47 | + this.spheres = null; |
| 48 | + this.triangles = null; |
| 49 | + this.geometries = null; |
| 50 | + |
| 51 | + this.parseSceneAttributes(); |
| 52 | + this.parseAmbientLightAttributes(); |
| 53 | + |
| 54 | + if (hasChild(this.root, "geometries")) { |
| 55 | + this.geometries = (Element) this.root.getElementsByTagName("geometries").item(0); |
| 56 | + this.planes = this.parseGeometry("plane"); |
| 57 | + this.spheres = this.parseGeometry("sphere"); |
| 58 | + this.triangles = this.parseGeometry("triangle"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Checks if an element has a child with the provided name |
| 64 | + * |
| 65 | + * @param parent parent element |
| 66 | + * @param nodeName child expected name |
| 67 | + * @return true if child is present |
| 68 | + */ |
| 69 | + private static boolean hasChild(Element parent, String nodeName) { |
| 70 | + NodeList childNodes = parent.getChildNodes(); |
| 71 | + int length = childNodes.getLength(); |
| 72 | + for (int i = 0; i < length; i++) |
| 73 | + if (childNodes.item(i).getNodeName().equals(nodeName)) |
| 74 | + return true; |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Parses the Scene attributes |
| 80 | + */ |
| 81 | + private void parseSceneAttributes() { |
| 82 | + NamedNodeMap attributes = this.root.getAttributes(); |
| 83 | + int length = attributes.getLength(); |
| 84 | + |
| 85 | + Node attribute; |
| 86 | + for (int i = 0; i < length; i++) { |
| 87 | + attribute = attributes.item(i); |
| 88 | + this.sceneAttributes.put(attribute.getNodeName(), attribute.getNodeValue()); |
| 89 | + } |
| 90 | + |
| 91 | + this.sceneAttributes.putIfAbsent("background-color", "0 0 0"); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Parses the Ambient Light attributes |
| 96 | + */ |
| 97 | + private void parseAmbientLightAttributes() { |
| 98 | + if (hasChild(this.root, "ambient-light")) { |
| 99 | + NamedNodeMap attributes = this.root.getElementsByTagName("ambient-light").item(0).getAttributes(); |
| 100 | + int length = attributes.getLength(); |
| 101 | + |
| 102 | + Node attribute; |
| 103 | + for (int i = 0; i < length; i++) { |
| 104 | + attribute = attributes.item(i); |
| 105 | + this.ambientLightAttributes.put(attribute.getNodeName(), attribute.getNodeValue()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + this.ambientLightAttributes.putIfAbsent("color", "0 0 0"); |
| 110 | + this.ambientLightAttributes.putIfAbsent("k", "0"); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Parses a specified geometry |
| 115 | + * |
| 116 | + * @param geometryName specified geometry name |
| 117 | + * @return parsed data structure |
| 118 | + */ |
| 119 | + private List<Map<String, String>> parseGeometry(String geometryName) { |
| 120 | + if (!hasChild(this.geometries, geometryName)) |
| 121 | + return null; |
| 122 | + |
| 123 | + List<Map<String, String>> result = new ArrayList<>(); |
| 124 | + NodeList requestedGeos = this.geometries.getElementsByTagName(geometryName); |
| 125 | + int length = requestedGeos.getLength(); |
| 126 | + |
| 127 | + NamedNodeMap attributes; |
| 128 | + for (int i = 0; i < length; i++) { |
| 129 | + attributes = requestedGeos.item(i).getAttributes(); |
| 130 | + int attrLength = attributes.getLength(); |
| 131 | + |
| 132 | + result.add(new HashMap<>()); |
| 133 | + |
| 134 | + Node attribute; |
| 135 | + for (int j = 0; j < attrLength; j++) { |
| 136 | + attribute = attributes.item(j); |
| 137 | + result.get(i).put(attribute.getNodeName(), attribute.getNodeValue()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return result; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Gets the Scene attributes data structure |
| 146 | + * |
| 147 | + * @return Scene attributes data structure |
| 148 | + */ |
| 149 | + public Map<String, String> getSceneAttributes() { |
| 150 | + return this.sceneAttributes; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Gets the Ambient Light data structure |
| 155 | + * |
| 156 | + * @return Ambient Light data structure |
| 157 | + */ |
| 158 | + public Map<String, String> getAmbientLightAttributes() { |
| 159 | + return this.ambientLightAttributes; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Gets the Planes data structure |
| 164 | + * |
| 165 | + * @return Planes data structure |
| 166 | + */ |
| 167 | + public List<Map<String, String>> getPlanes() { |
| 168 | + return this.planes; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Gets the Spheres data structure |
| 173 | + * |
| 174 | + * @return Spheres data structure |
| 175 | + */ |
| 176 | + public List<Map<String, String>> getSpheres() { |
| 177 | + return this.spheres; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Gets the Triangles data structure |
| 182 | + * |
| 183 | + * @return Triangles data structure |
| 184 | + */ |
| 185 | + public List<Map<String, String>> getTriangles() { |
| 186 | + return this.triangles; |
| 187 | + } |
| 188 | +} |
0 commit comments