📄 dominputcapsule.java
字号:
/* * Copyright (c) 2003-2009 jMonkeyEngine * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of 'jMonkeyEngine' nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package com.jme.util.export.xml;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.FloatBuffer;import java.nio.IntBuffer;import java.nio.ShortBuffer;import java.util.ArrayList;import java.util.BitSet;import java.util.HashMap;import java.util.Map;import java.util.logging.Level;import java.util.logging.Logger;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import com.jme.image.Texture;import com.jme.renderer.Renderer;import com.jme.scene.state.RenderState;import com.jme.scene.state.TextureState;import com.jme.system.DisplaySystem;import com.jme.util.TextureKey;import com.jme.util.TextureManager;import com.jme.util.export.InputCapsule;import com.jme.util.export.Savable;import com.jme.util.geom.BufferUtils;/** * Part of the jME XML IO system as introduced in the google code jmexml project. * * @author Kai Rabien (hevee) - original author of the code.google.com jmexml project * @author Doug Daniels (dougnukem) - adjustments for jME 2.0 and Java 1.5 */public class DOMInputCapsule implements InputCapsule { private Document doc; private Element currentElem; private XMLImporter importer; private boolean isAtRoot = true; private Map<String, Savable> referencedSavables = new HashMap<String, Savable>(); public DOMInputCapsule(Document doc, XMLImporter importer) { this.doc = doc; this.importer = importer; currentElem = doc.getDocumentElement(); } private static String decodeString(String s) { if (s == null) { return null; } s = s.replaceAll("\\"", "\"").replaceAll("\\<", "<").replaceAll("\\&", "&"); return s; } private Element findFirstChildElement(Element parent) { Node ret = parent.getFirstChild(); while (ret != null && (!(ret instanceof Element))) { ret = ret.getNextSibling(); } return (Element) ret; } private Element findChildElement(Element parent, String name) { if (parent == null) { return null; } Node ret = parent.getFirstChild(); while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name))) { ret = ret.getNextSibling(); } return (Element) ret; } private Element findNextSiblingElement(Element current) { Node ret = current.getNextSibling(); while (ret != null) { if (ret instanceof Element) { return (Element) ret; } ret = ret.getNextSibling(); } return null; } public byte readByte(String name, byte defVal) throws IOException { byte ret = defVal; try { ret = Byte.parseByte(currentElem.getAttribute(name)); } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public byte[] readByteArray(String name, byte[] defVal) throws IOException { byte[] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size = Integer.parseInt(tmpEl.getAttribute("size")); byte[] tmp = new byte[size]; String[] strings = tmpEl.getAttribute("data").split("\\s+"); for (int i = 0; i < size; i++) { tmp[i] = Byte.parseByte(strings[i]); } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public byte[][] readByteArray2D(String name, byte[][] defVal) throws IOException { byte[][] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size = Integer.parseInt(tmpEl.getAttribute("size")); byte[][] tmp = new byte[size][]; NodeList nodes = currentElem.getChildNodes(); int strIndex = 0; for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n instanceof Element && n.getNodeName().contains("array")) { if (strIndex < size) { tmp[strIndex++] = readByteArray(n.getNodeName(), null); } else { throw new IOException( "String array contains more elements than specified!"); } } } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } currentElem = (Element) currentElem.getParentNode(); return ret; } public int readInt(String name, int defVal) throws IOException { int ret = defVal; try { String s = currentElem.getAttribute(name); if (s.length() > 0) { ret = Integer.parseInt(s); } } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public int[] readIntArray(String name, int[] defVal) throws IOException { int[] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size = Integer.parseInt(tmpEl.getAttribute("size")); int[] tmp = new int[size]; String[] strings = tmpEl.getAttribute("data").split("\\s+"); for (int i = 0; i < size; i++) { tmp[i] = Integer.parseInt(strings[i]); } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public int[][] readIntArray2D(String name, int[][] defVal) throws IOException { int[][] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size = Integer.parseInt(tmpEl.getAttribute("size")); int[][] tmp = new int[size][]; NodeList nodes = currentElem.getChildNodes(); int strIndex = 0; for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n instanceof Element && n.getNodeName().contains("array")) { if (strIndex < size) { tmp[strIndex++] = readIntArray(n.getNodeName(), null); } else { throw new IOException( "String array contains more elements than specified!"); } } } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } currentElem = (Element) currentElem.getParentNode(); return ret; } public float readFloat(String name, float defVal) throws IOException { float ret = defVal; try { String s = currentElem.getAttribute(name); if (s.length() > 0) { ret = Float.parseFloat(s); } } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public float[] readFloatArray(String name, float[] defVal) throws IOException { float[] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size = Integer.parseInt(tmpEl.getAttribute("size")); float[] tmp = new float[size]; String[] strings = tmpEl.getAttribute("data").split("\\s+"); for (int i = 0; i < size; i++) { tmp[i] = Float.parseFloat(strings[i]); } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public float[][] readFloatArray2D(String name, float[][] defVal) throws IOException { float[][] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size_outer = Integer.parseInt(tmpEl.getAttribute("size_outer")); int size_inner = Integer.parseInt(tmpEl.getAttribute("size_outer")); float[][] tmp = new float[size_outer][size_inner]; String[] strings = tmpEl.getAttribute("data").split("\\s+"); for (int i = 0; i < size_outer; i++) { tmp[i] = new float[size_inner]; for (int k = 0; k < size_inner; k++) { tmp[i][k] = Float.parseFloat(strings[i]); } } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public double readDouble(String name, double defVal) throws IOException { double ret = defVal; try { ret = Double.parseDouble(currentElem.getAttribute(name)); } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public double[] readDoubleArray(String name, double[] defVal) throws IOException { double[] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size = Integer.parseInt(tmpEl.getAttribute("size")); double[] tmp = new double[size]; String[] strings = tmpEl.getAttribute("data").split("\\s+"); for (int i = 0; i < size; i++) { tmp[i] = Double.parseDouble(strings[i]); } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e); throw ex; } return ret; } public double[][] readDoubleArray2D(String name, double[][] defVal) throws IOException { double[][] ret = defVal; try { Element tmpEl; if (name != null) { tmpEl = findChildElement(currentElem, name); } else { tmpEl = currentElem; } if (tmpEl == null) { return defVal; } int size = Integer.parseInt(tmpEl.getAttribute("size")); double[][] tmp = new double[size][]; NodeList nodes = currentElem.getChildNodes(); int strIndex = 0; for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); if (n instanceof Element && n.getNodeName().contains("array")) { if (strIndex < size) { tmp[strIndex++] = readDoubleArray(n.getNodeName(), null); } else { throw new IOException("String array contains more elements than specified!"); } } } ret = tmp; } catch (Exception e) { IOException ex = new IOException(); ex.initCause(e);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -