Class XMLFunctions

java.lang.Object
mgui.interfaces.Utility
mgui.interfaces.xml.XMLFunctions

public class XMLFunctions
extends Utility
Utility class for XML-related functions.
Since:
1.0
Version:
1.0
Author:
Andrew Reid
  • Constructor Summary

    Constructors
    Constructor Description
    XMLFunctions()  
  • Method Summary

    Modifier and Type Method Description
    static <V> Attribute<V> createAttribute​(java.lang.Class<V> c, java.lang.String name, java.lang.Object value)
    Creates a new Attribute with a new instance of the given class.
    static java.awt.Color getColourForXML​(org.xml.sax.Attributes attributes)  
    static XMLObject.XMLEncoding getEncodingForStr​(java.lang.String encoding)
    Returns the XMLFormat corresponding to format.
    static java.lang.String getEncodingStr​(XMLObject.XMLEncoding encoding)
    Returns the string corresponding to encoding.
    static java.lang.Object getObjectForXML​(java.lang.String localName, org.xml.sax.Attributes attributes, java.lang.String class_name)
    Returns a new instantiated Object corresponding to the "object_class" field of attributes.
    static java.lang.Object getObjectForXMLString​(java.lang.Class<?> obj_class, java.lang.String string)
    Attempts to detect the data type of s and return a newly instantiated Object of the correct Class.
    static org.jogamp.vecmath.Point2f getPoint2f​(java.lang.String s)  
    static java.util.ArrayList<org.jogamp.vecmath.Point2f> getPoint2fList​(java.lang.String s)
    Parses s for '\t' and '\n' characters into a list of 2D points
    static org.jogamp.vecmath.Point3d getPoint3d​(java.lang.String s)  
    static org.jogamp.vecmath.Point3f getPoint3f​(java.lang.String s)  
    static java.lang.String getTab​(int tab)
    Returns a string with the specified number of tabs
    static java.lang.String getTupleStr​(org.jogamp.vecmath.Tuple3d t, int precision)  
    static java.lang.String getTupleStr​(org.jogamp.vecmath.Tuple3f t, int precision)  
    static org.jogamp.vecmath.Vector2d getVector2d​(java.lang.String s)  
    static org.jogamp.vecmath.Vector3d getVector3d​(java.lang.String s)  
    static org.jogamp.vecmath.Vector3f getVector3f​(java.lang.String s)  
    static java.lang.String getVectorStr​(org.jogamp.vecmath.Tuple2d t, int precision)  
    static java.lang.String getVectorStr​(org.jogamp.vecmath.Tuple2f t, int precision)  
    static java.lang.String getXMLCodedString​(java.lang.String string)
    Encodes a string into an XML-friendly line by replacing angle brackets with their escape characters: < --> < > --> >
    static java.lang.String getXMLDecodedString​(java.lang.String string)
    Decodes a string from an XML-friendly line by replacing angle brackets escape characters with their characters.
    static javax.swing.filechooser.FileFilter getXMLFileFilter()  
    static java.lang.String getXMLForAttributeObject​(java.lang.Object obj, int tab)
    Returns a string representing obj in an Attribute.
    static java.lang.String getXMLForObject​(java.lang.Object obj, int tab)  
    static java.lang.String getXMLForPoint2f​(org.jogamp.vecmath.Point2f p, int precision)  
    static java.lang.String getXMLFriendlyString​(java.lang.String string)
    Returns a string which is an XML-friendly version of string, for insertion into a tag value.
    static java.lang.String getXMLHeader()
    Returns the XML header from the current environment
    static java.lang.String getXMLStrForType​(XMLObject.XMLType type)
    Returns a String corresponding to the XMLObject.XMLType.
    static java.lang.String getXMLTabbedString​(java.lang.String string, int tab)
    Prepends all newline characters with the appropriate tab character, for friendlier XML output
    static XMLObject.XMLType getXMLTypeForStr​(java.lang.String string)
    Returns the XMLObject.XMLType corresponding to string.
    static java.lang.String getXMLUntabbedString​(java.lang.String string, int tab)
    Removes all new-line tabs from an XML input string
    static java.awt.Color parseColor​(java.lang.String fstr)
    Parses a color string (three space-delimited values) and returns the corresponding font
    static java.awt.Font parseFont​(java.lang.String fstr)
    Parses a font string (returned by Font.toString()) and returns the corresponding font
    static java.awt.BasicStroke parseStroke​(java.lang.String fstr)
    Parses the string for a BasicStroke.
    static void writeToXML​(java.io.File file, XMLObject xml)
    Writes an XML object to file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • XMLFunctions

      public XMLFunctions()
  • Method Details

    • getXMLFriendlyString

      public static java.lang.String getXMLFriendlyString​(java.lang.String string)
      Returns a string which is an XML-friendly version of string, for insertion into a tag value. That is, replaces special characters in tags such as '&', '<', or '>' with their XML equivalents. See this page for a list of codes.

      TODO: Either (1) complete this list; or (2) set policy such that tags cannot contain special characters

      Parameters:
      string -
      Returns:
    • getXMLHeader

      public static java.lang.String getXMLHeader()
      Returns the XML header from the current environment
      Returns:
    • getXMLTypeForStr

      public static XMLObject.XMLType getXMLTypeForStr​(java.lang.String string)
      Returns the XMLObject.XMLType corresponding to string.
      Parameters:
      string -
      Returns:
    • getXMLStrForType

      public static java.lang.String getXMLStrForType​(XMLObject.XMLType type)
      Returns a String corresponding to the XMLObject.XMLType.
      Parameters:
      string -
      Returns:
    • getEncodingStr

      public static java.lang.String getEncodingStr​(XMLObject.XMLEncoding encoding)
      Returns the string corresponding to encoding.
      Parameters:
      format -
      Returns:
    • getEncodingForStr

      public static XMLObject.XMLEncoding getEncodingForStr​(java.lang.String encoding)
      Returns the XMLFormat corresponding to format.
      Parameters:
      format -
      Returns:
    • getTab

      public static java.lang.String getTab​(int tab)
      Returns a string with the specified number of tabs
      Parameters:
      tab -
      Returns:
    • getTupleStr

      public static java.lang.String getTupleStr​(org.jogamp.vecmath.Tuple3d t, int precision)
    • getTupleStr

      public static java.lang.String getTupleStr​(org.jogamp.vecmath.Tuple3f t, int precision)
    • getVectorStr

      public static java.lang.String getVectorStr​(org.jogamp.vecmath.Tuple2d t, int precision)
    • getVectorStr

      public static java.lang.String getVectorStr​(org.jogamp.vecmath.Tuple2f t, int precision)
    • getVector3d

      public static org.jogamp.vecmath.Vector3d getVector3d​(java.lang.String s)
    • getVector2d

      public static org.jogamp.vecmath.Vector2d getVector2d​(java.lang.String s)
    • getVector3f

      public static org.jogamp.vecmath.Vector3f getVector3f​(java.lang.String s)
    • createAttribute

      public static <V> Attribute<V> createAttribute​(java.lang.Class<V> c, java.lang.String name, java.lang.Object value) throws org.xml.sax.SAXException
      Creates a new Attribute with a new instance of the given class. Class must define an empty constructor.
      Parameters:
      c -
      name -
      Returns:
      Throws:
      org.xml.sax.SAXException
    • getXMLCodedString

      public static java.lang.String getXMLCodedString​(java.lang.String string)
      Encodes a string into an XML-friendly line by replacing angle brackets with their escape characters:
      • < --> <
      • > --> >
      Parameters:
      string -
      Returns:
    • getXMLDecodedString

      public static java.lang.String getXMLDecodedString​(java.lang.String string)
      Decodes a string from an XML-friendly line by replacing angle brackets escape characters with their characters.
      • < --> <
      • > --> >
      Parameters:
      string -
      Returns:
    • getXMLTabbedString

      public static java.lang.String getXMLTabbedString​(java.lang.String string, int tab)
      Prepends all newline characters with the appropriate tab character, for friendlier XML output
      Parameters:
      string -
      Returns:
    • getXMLUntabbedString

      public static java.lang.String getXMLUntabbedString​(java.lang.String string, int tab)
      Removes all new-line tabs from an XML input string
      Parameters:
      string -
      tab -
    • getPoint2fList

      public static java.util.ArrayList<org.jogamp.vecmath.Point2f> getPoint2fList​(java.lang.String s)
      Parses s for '\t' and '\n' characters into a list of 2D points
      Parameters:
      s -
      Returns:
    • getPoint2f

      public static org.jogamp.vecmath.Point2f getPoint2f​(java.lang.String s)
    • getPoint3f

      public static org.jogamp.vecmath.Point3f getPoint3f​(java.lang.String s)
    • getPoint3d

      public static org.jogamp.vecmath.Point3d getPoint3d​(java.lang.String s)
    • getXMLFileFilter

      public static javax.swing.filechooser.FileFilter getXMLFileFilter()
    • getXMLForPoint2f

      public static java.lang.String getXMLForPoint2f​(org.jogamp.vecmath.Point2f p, int precision)
    • getXMLForObject

      public static java.lang.String getXMLForObject​(java.lang.Object obj, int tab)
    • getXMLForAttributeObject

      public static java.lang.String getXMLForAttributeObject​(java.lang.Object obj, int tab)
      Returns a string representing obj in an Attribute.
      Parameters:
      obj -
      tab -
      Returns:
    • getColourForXML

      public static java.awt.Color getColourForXML​(org.xml.sax.Attributes attributes)
    • getObjectForXML

      public static java.lang.Object getObjectForXML​(java.lang.String localName, org.xml.sax.Attributes attributes, java.lang.String class_name)
      Returns a new instantiated Object corresponding to the "object_class" field of attributes.
      Parameters:
      localName -
      attributes -
      Returns:
      New object instance, or null if one could not be created TODO: Allow user-defined handlers in the InterfaceEnvironment
    • getObjectForXMLString

      public static java.lang.Object getObjectForXMLString​(java.lang.Class<?> obj_class, java.lang.String string) throws XMLException
      Attempts to detect the data type of s and return a newly instantiated Object of the correct Class.
      Parameters:
      string - String with which to instantiate object
      Returns:
      The instantiated object
      Throws:
      XMLException - If object could not be instantiated
    • parseFont

      public static java.awt.Font parseFont​(java.lang.String fstr) throws java.lang.Exception
      Parses a font string (returned by Font.toString()) and returns the corresponding font
      Parameters:
      fstr -
      Returns:
      Throws:
      java.lang.Exception
    • parseColor

      public static java.awt.Color parseColor​(java.lang.String fstr) throws java.lang.Exception
      Parses a color string (three space-delimited values) and returns the corresponding font
      Parameters:
      fstr -
      Returns:
      Throws:
      java.lang.Exception
    • parseStroke

      public static java.awt.BasicStroke parseStroke​(java.lang.String fstr) throws java.lang.Exception
      Parses the string for a BasicStroke.
      Parameters:
      fstr -
      Returns:
      Throws:
      java.lang.Exception
    • writeToXML

      public static void writeToXML​(java.io.File file, XMLObject xml) throws XMLException
      Writes an XML object to file. Overwrites an existing file. To append to an existing file, use appendToXML.
      Parameters:
      file -
      xml -
      Throws:
      XMLException