/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Jan 5, 2005 */ package ml.core.helper; import ml.colour.Colour; import ml.core.exceptions.Exception; import ml.math.Vector3D; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * Property implements a typed name/value pair. * * @author gregor */ public class Property extends Value { private String name; private PropertyContainer container; public Property(PropertyContainer container) { super(); container.registerProperty(this); this.container = container; } private Property() { super(); } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public void setType(Type type) { super.setType(type); container.setHasUpdatedProperties(); } @Override public void setMaxSize(int maxSize) throws Exception { super.setMaxSize(maxSize); container.setHasUpdatedProperties(); } public void setPropertyContainer(PropertyContainer container) { container.unregisterProperty(this); this.container=container; container.registerProperty(this); } public PropertyContainer getPropertyContainer() { return container; } public void copyFrom(Property property) { super.copyFrom(property); name=property.name; //container=property.container; } @Override public Property clone() { Property property=new Property(); property.copyFrom(this); return property; } @Override public void remove(int index) throws Exception { super.remove(index); container.setHasUpdatedProperties(); } @Override public void setValue(String stringValue) throws Exception { super.setValue(stringValue); container.setHasUpdatedProperties(); } @Override public void setValue(Vector3D vectorValue) throws Exception { super.setValue(vectorValue); container.setHasUpdatedProperties(); } @Override public void setValue(Colour colourValue) throws Exception { super.setValue(colourValue); container.setHasUpdatedProperties(); } @Override public void setValue(boolean booleanValue) throws Exception { super.setValue(booleanValue); container.setHasUpdatedProperties(); } @Override public void setValue(int integerValue) throws Exception { super.setValue(integerValue); container.setHasUpdatedProperties(); } @Override public void setValue(double doubleValue) throws Exception { super.setValue(doubleValue); container.setHasUpdatedProperties(); } @Override public void setArrayValue(int index, String stringValue) throws Exception { super.setArrayValue(index,stringValue); container.setHasUpdatedProperties(); } @Override public void setArrayValue(int index, Vector3D vectorValue) throws Exception { super.setArrayValue(index,vectorValue); container.setHasUpdatedProperties(); } @Override public void setArrayValue(int index, boolean booleanValue) throws Exception { super.setArrayValue(index,booleanValue); container.setHasUpdatedProperties(); } @Override public void setArrayValue(int index, int integerValue) throws Exception { super.setArrayValue(index,integerValue); container.setHasUpdatedProperties(); } @Override public void setArrayValue(int index, double doubleValue) throws Exception { super.setArrayValue(index,doubleValue); container.setHasUpdatedProperties(); } @Override public void setValue(Value value) { super.setValue(value); container.setHasUpdatedProperties(); } public void readFromFile(Element propertyElement, boolean preserveType) throws Exception { if(!propertyElement.getNodeName().equals("property")) { throw new ml.core.exceptions.Exception("invalid file format: unknown tag found within tag"); } name=propertyElement.getAttributes().getNamedItem("name").getNodeValue(); // ml.core.State.getInstance().logger().info("property name: \"" + name + "\""); String propertyType=propertyElement.getAttributes().getNamedItem("type").getNodeValue(); if(propertyType.equals("none")) { // do nothing } else if(propertyType.equals("boolean")) { if(!preserveType) { setType(Value.Type.Boolean); } NodeList valueList=propertyElement.getElementsByTagName("value"); if(valueList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag inside tag of type boolean"); } Element valueElement=(Element)valueList.item(0); String value=valueElement.getAttribute("booleanvalue"); setValue(Boolean.parseBoolean(value)); } else if(propertyType.equals("integer")) { if(!preserveType) { setType(Value.Type.Integer); } NodeList valueList=propertyElement.getElementsByTagName("value"); if(valueList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag inside tag of type integer"); } Element valueElement=(Element)valueList.item(0); String value=valueElement.getAttribute("integervalue"); setValue(Integer.parseInt(value)); } else if(propertyType.equals("float")) { if(!preserveType) { setType(Value.Type.Float); } NodeList valueList=propertyElement.getElementsByTagName("value"); if(valueList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag inside tag of type float"); } Element valueElement=(Element)valueList.item(0); String value=valueElement.getAttribute("floatvalue"); setValue(Double.parseDouble(value)); } else if(propertyType.equals("string")) { if(!preserveType) { setType(Value.Type.String); } NodeList valueList=propertyElement.getElementsByTagName("value"); if(valueList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag inside tag of type string"); } Element valueElement=(Element)valueList.item(0); String value=valueElement.getAttribute("stringvalue"); setValue(value); } else if(propertyType.equals("vector3d")) { if(!preserveType) { setType(Value.Type.Vector3D); } NodeList valueList=propertyElement.getElementsByTagName("value"); if(valueList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag inside tag of type vector3d"); } Element valueElement=(Element)valueList.item(0); String xvalue=valueElement.getAttribute("xvalue"); String yvalue=valueElement.getAttribute("yvalue"); String zvalue=valueElement.getAttribute("zvalue"); ml.math.Vector3D value=new ml.math.Vector3D(); value.X1=Double.parseDouble(xvalue); value.X2=Double.parseDouble(yvalue); value.X3=Double.parseDouble(zvalue); setValue(value); } else if(propertyType.equals("colour")) { if(!preserveType) { setType(Value.Type.Colour); } NodeList valueList=propertyElement.getElementsByTagName("value"); if(valueList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag inside tag of type colour"); } Element valueElement=(Element)valueList.item(0); String red=valueElement.getAttribute("red"); String green=valueElement.getAttribute("green"); String blue=valueElement.getAttribute("blue"); String alpha=valueElement.getAttribute("alpha"); Colour value=new Colour(); value.red=Double.parseDouble(red); value.green=Double.parseDouble(green); value.blue=Double.parseDouble(blue); value.alpha=Double.parseDouble(alpha); setValue(value); } else if(propertyType.equals("booleanarray")) { if(!preserveType) { setType(Value.Type.BooleanArray); } int length=Integer.parseInt(propertyElement.getAttribute("length")); NodeList valueList=propertyElement.getElementsByTagName("value"); if(valueList.getLength()!=length) { throw new ml.core.exceptions.Exception("invalid file format: number of tags inside doesn't match given length of booleanarray property"); } for(int k=0;k tags inside doesn't match given length of integerarray property"); } for(int k=0;k tags inside doesn't match given length of floatrarray property"); } for(int k=0;k tags inside doesn't match given length of stringarray property"); } for(int k=0;k tags inside doesn't match given length of vector3darray property"); } for(int k=0;k