📄 domoutputcapsule.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.Arrays;import java.util.BitSet;import java.util.IdentityHashMap;import java.util.Iterator;import java.util.Map;import org.w3c.dom.DOMException;import org.w3c.dom.Document;import org.w3c.dom.Element;import com.jme.util.export.JMEExporter;import com.jme.util.export.OutputCapsule;import com.jme.util.export.Savable;/** * 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 DOMOutputCapsule implements OutputCapsule { private static final String dataAttributeName = "data"; private Document doc; private Element currentElement; private JMEExporter exporter; private Map<Savable, Element> writtenSavables = new IdentityHashMap<Savable, Element>(); public DOMOutputCapsule(Document doc, JMEExporter exporter) { this.doc = doc; this.exporter = exporter; currentElement = null; } public Document getDoc() { return doc; } /** * appends a new Element with the given name to currentElement, sets * currentElement to be new Element, and returns the new Element as well */ private Element appendElement(String name) { Element ret = null; ret = doc.createElement(name); if (currentElement == null) { doc.appendChild(ret); } else { currentElement.appendChild(ret); } currentElement = ret; return ret; } private static String encodeString(String s) { if (s == null) { return null; } s = s.replaceAll("\\&", "&") .replaceAll("\\\"", """) .replaceAll("\\<", "<"); return s; } public void write(byte value, String name, byte defVal) throws IOException { if (value == defVal) { return; } currentElement.setAttribute(name, String.valueOf(value)); } public void write(byte[] value, String name, byte[] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { value = defVal; } for (byte b : value) { buf.append(b); buf.append(" "); } //remove last space buf.setLength(buf.length() - 1); Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(byte[][] value, String name, byte[][] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { value = defVal; } for (byte[] bs : value) { for (byte b : bs) { buf.append(b); buf.append(" "); } buf.append(" "); } //remove last spaces buf.setLength(buf.length() - 2); Element el = appendElement(name); el.setAttribute("size_outer", String.valueOf(value.length)); el.setAttribute("size_inner", String.valueOf(value[0].length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(int value, String name, int defVal) throws IOException { if (value == defVal) { return; } currentElement.setAttribute(name, String.valueOf(value)); } public void write(int[] value, String name, int[] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { return; } if (Arrays.equals(value, defVal)) { return; } for (int b : value) { buf.append(b); buf.append(" "); } //remove last space buf.setLength(Math.max(0, buf.length() - 1)); Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(int[][] value, String name, int[][] defVal) throws IOException { if (value == null) return; if(Arrays.deepEquals(value, defVal)) return; Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); for (int i=0; i<value.length; i++) { int[] array = value[i]; write(array, "array_"+i, defVal==null?null:defVal[i]); } currentElement = (Element) el.getParentNode(); } public void write(float value, String name, float defVal) throws IOException { if (value == defVal) { return; } currentElement.setAttribute(name, String.valueOf(value)); } public void write(float[] value, String name, float[] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { value = defVal; } for (float b : value) { buf.append(b); buf.append(" "); } //remove last space buf.setLength(buf.length() - 1); Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(float[][] value, String name, float[][] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) return; if(Arrays.deepEquals(value, defVal)) return; for (float[] bs : value) { for(float b : bs){ buf.append(b); buf.append(" "); } } //remove last space buf.setLength(buf.length() - 1); Element el = appendElement(name); el.setAttribute("size_outer", String.valueOf(value.length)); el.setAttribute("size_inner", String.valueOf(value[0].length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(double value, String name, double defVal) throws IOException { if (value == defVal) { return; } currentElement.setAttribute(name, String.valueOf(value)); } public void write(double[] value, String name, double[] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { value = defVal; } for (double b : value) { buf.append(b); buf.append(" "); } //remove last space buf.setLength(buf.length() - 1); Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(double[][] value, String name, double[][] defVal) throws IOException { if (value == null) return; if(Arrays.deepEquals(value, defVal)) return; Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); for (int i=0; i<value.length; i++) { double[] array = value[i]; write(array, "array_"+i, defVal==null?null:defVal[i]); } currentElement = (Element) el.getParentNode(); } public void write(long value, String name, long defVal) throws IOException { if (value == defVal) { return; } currentElement.setAttribute(name, String.valueOf(value)); } public void write(long[] value, String name, long[] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { value = defVal; } for (long b : value) { buf.append(b); buf.append(" "); } //remove last space buf.setLength(buf.length() - 1); Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(long[][] value, String name, long[][] defVal) throws IOException { if (value == null) return; if(Arrays.deepEquals(value, defVal)) return; Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); for (int i=0; i<value.length; i++) { long[] array = value[i]; write(array, "array_"+i, defVal==null?null:defVal[i]); } currentElement = (Element) el.getParentNode(); } public void write(short value, String name, short defVal) throws IOException { if (value == defVal) { return; } currentElement.setAttribute(name, String.valueOf(value)); } public void write(short[] value, String name, short[] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { value = defVal; } for (short b : value) { buf.append(b); buf.append(" "); } //remove last space buf.setLength(buf.length() - 1); Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(short[][] value, String name, short[][] defVal) throws IOException { if (value == null) return; if(Arrays.deepEquals(value, defVal)) return; Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); for (int i=0; i<value.length; i++) { short[] array = value[i]; write(array, "array_"+i, defVal==null?null:defVal[i]); } currentElement = (Element) el.getParentNode(); } public void write(boolean value, String name, boolean defVal) throws IOException { if (value == defVal) { return; } currentElement.setAttribute(name, String.valueOf(value)); } public void write(boolean[] value, String name, boolean[] defVal) throws IOException { StringBuilder buf = new StringBuilder(); if (value == null) { value = defVal; } for (boolean b : value) { buf.append(b); buf.append(" "); } //remove last space buf.setLength(Math.max(0, buf.length() - 1)); Element el = appendElement(name); el.setAttribute("size", String.valueOf(value.length)); el.setAttribute(dataAttributeName, buf.toString()); currentElement = (Element) currentElement.getParentNode(); } public void write(boolean[][] value, String name, boolean[][] defVal) throws IOException { if (value == null) return; if(Arrays.deepEquals(value, defVal)) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -