/* * 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 Apr 15, 2005 */ package ml.backend.document; import java.io.IOException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import ml.backend.og.Slot; import ml.backend.renderer.Manager; import ml.backend.renderer.RenderEngine; import ml.core.State; import ml.core.exceptions.Exception; import ml.core.helper.PropertyContainer; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; class FileReader { private Document document; private String filename; public FileReader(Document document, String filename) { this.document=document; this.filename=filename; document.setFilename(filename); } private void importCache(Element cacheRoot) throws Exception { ml.backend.cache.Manager cacheManager=((Document)document).getCacheManager(); NodeList cacheList=cacheRoot.getElementsByTagName("cachenode"); for(int i=0;i tag"); } String propertyName=propertyNode.getAttributes().getNamedItem("name").getNodeValue(); ml.core.helper.Property property=propertyContainer.getProperty(propertyName); if(property==null) { throw new Exception("no such property in operator graph node: " + propertyName); } property.readFromFile(propertyElement,true); } private void importOGNodes(Element ogRoot) throws Exception { // find node "nodes" NodeList nodesList=ogRoot.getElementsByTagName("nodes"); if(nodesList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: more than one tag within tag"); } // iterate over children of nodes element: Element nodesRoot=(Element)nodesList.item(0); NodeList nodes=nodesRoot.getChildNodes(); for(int i=0; i tag"); } // create node and set name: Node attributeNode=docOGNode.getAttributes().getNamedItem("type"); String typeName=attributeNode.getNodeValue(); attributeNode=docOGNode.getAttributes().getNamedItem("name"); String nodeName=attributeNode.getNodeValue(); ml.core.State.getInstance().logger().info("creating OG node \"" + nodeName + "\"of type \"" + typeName + "\""); ml.backend.og.Node ogNode=document.getOGManager().createNodeByName(typeName, nodeName); // parse and set properties: NodeList propertyList=docOGNode.getChildNodes(); for(int j=0;j tag within tag"); } Element linksRoot=(Element)linksList.item(0); NodeList links=linksRoot.getElementsByTagName("link"); for(int i=0;i tag"); } NodeList inputList=linkElement.getElementsByTagName("input"); if(inputList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag within tag"); } Element inputElement=(Element)inputList.item(0); String inputNodeName=inputElement.getAttribute("node"); String inputSlotName=inputElement.getAttribute("slot"); Slot inputSlot=document.getOGManager().getNodeByName(inputNodeName).getSlot(inputSlotName); NodeList outputList=linkElement.getElementsByTagName("output"); if(outputList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: expects exactly one tag within tag"); } Element outputElement=(Element)outputList.item(0); String outputNodeName=outputElement.getAttribute("node"); String outputSlotName=outputElement.getAttribute("slot"); ml.core.State.getInstance().logger().info("linking " + inputNodeName + "/" + inputSlotName + " to " + outputNodeName + "/" + outputSlotName); Slot outputSlot=document.getOGManager().getNodeByName(outputNodeName).getSlot(outputSlotName); document.getOGManager().addLink(inputSlot,outputSlot); } } private void importOG(Element ogRoot) throws Exception { importOGNodes(ogRoot); importOGLinks(ogRoot); } private void importRenderSettings(Element renderEngines) throws Exception { NodeList engineNodes=renderEngines.getElementsByTagName("engine"); for(int i=0; i0) { Element cache=(Element) cacheList.item(0); importCache(cache); } // find og element NodeList ogList=root.getElementsByTagName("og"); if(ogList.getLength()!=1) { throw new ml.core.exceptions.Exception("invalid file format: more than one tag in file"); } Element og=(Element) ogList.item(0); // read and convert DOM tree here importOG(og); // older files do not yet contain the renderengines section, so do nothing if not found NodeList renderSettingsList=root.getElementsByTagName("renderengines"); if(renderSettingsList.getLength()>1) { throw new ml.core.exceptions.Exception("invalid file format: more than one tag in file"); } else if(renderSettingsList.getLength()==1) { Element renderEngines=(Element) renderSettingsList.item(0); importRenderSettings(renderEngines); } } catch (ParserConfigurationException e) { throw new Exception("Error while reading file",e); } catch (SAXException e) { throw new Exception("Error while reading file",e); } catch (IOException e) { throw new Exception("Error while reading file",e); } } }