1 /* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 package com.jme3.util.xml; 34 35 import com.jme3.math.ColorRGBA; 36 import com.jme3.math.Vector3f; 37 import org.xml.sax.Attributes; 38 import org.xml.sax.SAXException; 39 40 /** 41 * Utility methods for parsing XML data using SAX. 42 */ 43 public final class SAXUtil { 44 45 /** 46 * Parses an integer from a string, if the string is null returns 47 * def. 48 * 49 * @param i 50 * @param def 51 * @return 52 * @throws SAXException 53 */ parseInt(String i, int def)54 public static int parseInt(String i, int def) throws SAXException{ 55 if (i == null) 56 return def; 57 else{ 58 try { 59 return Integer.parseInt(i); 60 } catch (NumberFormatException ex){ 61 throw new SAXException("Expected an integer, got '"+i+"'"); 62 } 63 } 64 } 65 parseInt(String i)66 public static int parseInt(String i) throws SAXException{ 67 if (i == null) 68 throw new SAXException("Expected an integer"); 69 else{ 70 try { 71 return Integer.parseInt(i); 72 } catch (NumberFormatException ex){ 73 throw new SAXException("Expected an integer, got '"+i+"'"); 74 } 75 } 76 } 77 parseFloat(String f, float def)78 public static float parseFloat(String f, float def) throws SAXException{ 79 if (f == null) 80 return def; 81 else{ 82 try { 83 return Float.parseFloat(f); 84 } catch (NumberFormatException ex){ 85 throw new SAXException("Expected a decimal, got '"+f+"'"); 86 } 87 } 88 } 89 parseFloat(String f)90 public static float parseFloat(String f) throws SAXException{ 91 if (f == null) 92 throw new SAXException("Expected a decimal"); 93 else{ 94 try { 95 return Float.parseFloat(f); 96 } catch (NumberFormatException ex){ 97 throw new SAXException("Expected a decimal, got '"+f+"'"); 98 } 99 } 100 } 101 parseBool(String bool, boolean def)102 public static boolean parseBool(String bool, boolean def) throws SAXException{ 103 if (bool == null || bool.equals("")) 104 return def; 105 else 106 return Boolean.valueOf(bool); 107 //else 108 //else 109 // throw new SAXException("Expected a boolean, got'"+bool+"'"); 110 } 111 parseString(String str, String def)112 public static String parseString(String str, String def){ 113 if (str == null) 114 return def; 115 else 116 return str; 117 } 118 parseString(String str)119 public static String parseString(String str) throws SAXException{ 120 if (str == null) 121 throw new SAXException("Expected a string"); 122 else 123 return str; 124 } 125 parseVector3(Attributes attribs)126 public static Vector3f parseVector3(Attributes attribs) throws SAXException{ 127 float x = parseFloat(attribs.getValue("x")); 128 float y = parseFloat(attribs.getValue("y")); 129 float z = parseFloat(attribs.getValue("z")); 130 return new Vector3f(x,y,z); 131 } 132 parseColor(Attributes attribs)133 public static ColorRGBA parseColor(Attributes attribs) throws SAXException{ 134 float r = parseFloat(attribs.getValue("r")); 135 float g = parseFloat(attribs.getValue("g")); 136 float b = parseFloat(attribs.getValue("b")); 137 return new ColorRGBA(r, g, b, 1f); 138 } 139 140 } 141