org.opensourcephysics.controls
Class XMLControlElement

java.lang.Object
  extended by org.opensourcephysics.controls.XMLControlElement
All Implemented Interfaces:
Control, XMLControl, XMLProperty

public class XMLControlElement
extends java.lang.Object
implements XMLControl

This is a basic xml control for storing data.

Version:
1.0
Author:
Douglas Brown

Field Summary
static int ALWAYS_DECRYPT
           
 boolean canWrite
           
protected  java.lang.String className
           
static int compactArraySize
           
protected  java.util.Map<java.lang.String,java.lang.Integer> counts
           
protected  java.lang.String doctype
           
protected static java.lang.String encoding
           
protected  java.io.BufferedReader input
           
protected  int level
           
protected  java.lang.String name
           
static int NEVER_DECRYPT
           
protected  java.lang.Object object
           
protected  java.io.BufferedWriter output
           
protected  XMLProperty parent
           
static int PASSWORD_DECRYPT
           
protected  java.util.ArrayList<java.lang.String> propNames
           
protected  java.util.ArrayList<XMLProperty> props
           
protected  boolean readFailed
           
protected  java.lang.Class<?> theClass
           
protected  boolean valid
           
protected  java.lang.String version
           
 
Fields inherited from interface org.opensourcephysics.controls.Control
NOT_EDITABLE_BACKGROUND
 
Constructor Summary
XMLControlElement()
          Constructs an empty control for the Object class.
XMLControlElement(java.lang.Class<?> type)
          Constructs an empty control for the specified class.
XMLControlElement(java.lang.Object obj)
          Constructs and loads a control with the specified object.
XMLControlElement(java.lang.String input)
          Constructs a control and reads the specified input.
XMLControlElement(XMLControl control)
          Constructs a copy of the specified XMLControl.
XMLControlElement(XMLProperty parent)
          Constructs a control with the specified parent.
 
Method Summary
 java.lang.String addNumbering(java.lang.String name)
          Appends numbering to a specified name.
 void calculationDone(java.lang.String s)
          Method required by the Control interface.
 void clearMessages()
          Method required by the Control interface.
 void clearValues()
          Clears all properties.
 java.lang.Object clone()
          Returns a copy of this control.
 boolean failedToRead()
          Returns true if the most recent read operation failed.
 boolean getBoolean(java.lang.String name)
          Gets the boolean value of the specified named property.
 XMLControl getChildControl(java.lang.String name)
          Gets the named XMLControl child of this property.
 XMLControl[] getChildControls()
          Gets the XMLControl children of this property.
 java.lang.String getDoctype()
          Gets the doctype.
 double getDouble(java.lang.String name)
          Gets the double value of the specified named property.
 int getInt(java.lang.String name)
          Gets the int value of the specified named property.
 int getLevel()
          Gets the level of this property relative to the root.
 java.lang.Object getObject(java.lang.String name)
          Gets the object value of the specified named property.
 java.lang.Class<?> getObjectClass()
          Gets the class of the object for which this element stores data.
 java.lang.String getObjectClassName()
          Gets the name of the object class for which this element stores data.
<T> java.util.List<T>
getObjects(java.lang.Class<T> type)
          Returns a list of objects of a specified class within this control.
<T> java.util.List<T>
getObjects(java.lang.Class<T> type, boolean useChooser)
          Returns a list of objects of a specified class within this control.
 XMLProperty getParentProperty()
          Gets the immediate parent property, if any.
 java.lang.String getPassword()
          Gets the password.
 java.lang.Class<?> getPropertyClass()
          Gets the property class.
 java.util.List<java.lang.Object> getPropertyContent()
          Gets the property content of this control.
 java.lang.String getPropertyName()
          Gets the property name.
 java.util.Collection<java.lang.String> getPropertyNames()
          Gets the set of property names.
 java.lang.String getPropertyType()
          Gets the property type.
 java.lang.String getPropertyType(java.lang.String name)
          Gets the type of the specified property.
 XMLControlElement getRootControl()
          Gets the root control.
 java.lang.String getString(java.lang.String name)
          Gets the string value of the specified named property.
 java.lang.String getVersion()
          Gets the version.
 boolean isValid()
          Gets the valid property.
 java.lang.Object loadObject(java.lang.Object obj)
          Loads an object with data from this element.
 java.lang.Object loadObject(java.lang.Object obj, boolean autoImport)
          Loads an object with data from this element.
 java.lang.Object loadObject(java.lang.Object obj, boolean autoImport, boolean importAll)
          Loads an object with data from this element.
 void print(java.lang.String s)
          Method required by the Control interface.
 void println()
          Method required by the Control interface.
 void println(java.lang.String s)
          Method required by the Control interface.
 void read(java.io.Reader in)
          Reads the control from a Reader.
 java.lang.String read(java.lang.String name)
          Reads data into this control from a named source.
 java.lang.String readForClass(java.lang.String name, java.lang.Class<?> type)
          Reads data into this control from a named source if the source specifies the same class as the current className.
 void readXML(java.lang.String xml)
          Reads the control from an xml string.
 boolean readXMLForClass(java.lang.String xml, java.lang.Class<?> type)
          Reads this control from an xml string if the xml specifies the same class as the current className.
 void saveObject(java.lang.Object obj)
          Saves an object's data in this element.
 void setDecryptPolicy(int policy)
          Sets the verify password flag.
 void setDoctype(java.lang.String name)
          Sets the doctype.
 void setLockValues(boolean lock)
          Locks the control's interface.
 void setObjectClass(java.lang.Class<?> type)
          Sets the class of the object for which this element stores data.
 void setPassword(java.lang.String pass)
          Sets the password.
 void setValid(boolean valid)
          Sets the valid property.
 void setValue(java.lang.String stringValue)
          This does nothing since the property type is "object".
 void setValue(java.lang.String name, boolean value)
          Sets a property with the specified name and boolean value.
 void setValue(java.lang.String name, double value)
          Sets a property with the specified name and double value.
 void setValue(java.lang.String name, int value)
          Sets a property with the specified name and int value.
 void setValue(java.lang.String name, java.lang.Object obj)
          Sets a property with the specified name and object value.
 void setVersion(java.lang.String vers)
          Sets the version.
 java.lang.String toString()
          Returns the string xml representation.
 java.lang.String toXML()
          Returns this control as an xml string.
 java.lang.String write(java.lang.String fileName)
          Writes this control as an xml file with the specified name.
 void write(java.io.Writer out)
          Writes this control to a Writer.
 void writeDocType(java.io.Writer out)
          Writes the DTD to a Writer.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALWAYS_DECRYPT

public static final int ALWAYS_DECRYPT
See Also:
Constant Field Values

PASSWORD_DECRYPT

public static final int PASSWORD_DECRYPT
See Also:
Constant Field Values

NEVER_DECRYPT

public static final int NEVER_DECRYPT
See Also:
Constant Field Values

compactArraySize

public static int compactArraySize

encoding

protected static java.lang.String encoding

className

protected java.lang.String className

theClass

protected java.lang.Class<?> theClass

name

protected java.lang.String name

counts

protected java.util.Map<java.lang.String,java.lang.Integer> counts

object

protected java.lang.Object object

parent

protected XMLProperty parent

level

protected int level

propNames

protected java.util.ArrayList<java.lang.String> propNames

props

protected java.util.ArrayList<XMLProperty> props

input

protected java.io.BufferedReader input

output

protected java.io.BufferedWriter output

canWrite

public boolean canWrite

valid

protected boolean valid

readFailed

protected boolean readFailed

version

protected java.lang.String version

doctype

protected java.lang.String doctype
Constructor Detail

XMLControlElement

public XMLControlElement()
Constructs an empty control for the Object class.


XMLControlElement

public XMLControlElement(java.lang.Class<?> type)
Constructs an empty control for the specified class.

Parameters:
type - the class.

XMLControlElement

public XMLControlElement(java.lang.Object obj)
Constructs and loads a control with the specified object.

Parameters:
obj - the object.

XMLControlElement

public XMLControlElement(XMLProperty parent)
Constructs a control with the specified parent.

Parameters:
parent - the parent.

XMLControlElement

public XMLControlElement(java.lang.String input)
Constructs a control and reads the specified input. Input may be a file name or an xml string

Parameters:
input - the input string

XMLControlElement

public XMLControlElement(XMLControl control)
Constructs a copy of the specified XMLControl.

Parameters:
control - the XMLControl to copy.
Method Detail

setLockValues

public void setLockValues(boolean lock)
Locks the control's interface. Values sent to the control will not update the display until the control is unlocked. Not implemented.

Specified by:
setLockValues in interface Control
Parameters:
lock - boolean

setValue

public void setValue(java.lang.String name,
                     boolean value)
Sets a property with the specified name and boolean value.

Specified by:
setValue in interface Control
Parameters:
name - the name
value - the boolean value

setValue

public void setValue(java.lang.String name,
                     double value)
Sets a property with the specified name and double value.

Specified by:
setValue in interface Control
Parameters:
name - the name
value - the double value

setValue

public void setValue(java.lang.String name,
                     int value)
Sets a property with the specified name and int value.

Specified by:
setValue in interface Control
Parameters:
name - the name
value - the int value

setValue

public void setValue(java.lang.String name,
                     java.lang.Object obj)
Sets a property with the specified name and object value.

Specified by:
setValue in interface Control
Parameters:
name - the name
obj - the object

getBoolean

public boolean getBoolean(java.lang.String name)
Gets the boolean value of the specified named property.

Specified by:
getBoolean in interface Control
Parameters:
name - the name
Returns:
the boolean value, or false if none found

getDouble

public double getDouble(java.lang.String name)
Gets the double value of the specified named property.

Specified by:
getDouble in interface Control
Parameters:
name - the name
Returns:
the double value, or Double.NaN if none found

getInt

public int getInt(java.lang.String name)
Gets the int value of the specified named property.

Specified by:
getInt in interface Control
Parameters:
name - the name
Returns:
the int value, or Integer.MIN_VALUE if none found

getString

public java.lang.String getString(java.lang.String name)
Gets the string value of the specified named property.

Specified by:
getString in interface Control
Parameters:
name - the name
Returns:
the string value, or null if none found

getObject

public java.lang.Object getObject(java.lang.String name)
Gets the object value of the specified named property.

Specified by:
getObject in interface Control
Parameters:
name - the name
Returns:
the object, or null if not found

getPropertyNames

public java.util.Collection<java.lang.String> getPropertyNames()
Gets the set of property names.

Specified by:
getPropertyNames in interface Control
Returns:
a set of names

getPropertyType

public java.lang.String getPropertyType(java.lang.String name)
Gets the type of the specified property. Returns null if the property is not found.

Specified by:
getPropertyType in interface XMLControl
Parameters:
name - the property name
Returns:
the type

setPassword

public void setPassword(java.lang.String pass)
Sets the password. Files are encrypted when the password is non-null.

Parameters:
pass - the password or phrase

getPassword

public java.lang.String getPassword()
Gets the password.

Returns:
the password

setDecryptPolicy

public void setDecryptPolicy(int policy)
Sets the verify password flag.

Parameters:
true - to require password verification for file decryption

read

public java.lang.String read(java.lang.String name)
Reads data into this control from a named source.

Specified by:
read in interface XMLControl
Parameters:
name - the name
Returns:
the path of the opened document or null if failed

readXML

public void readXML(java.lang.String xml)
Reads the control from an xml string.

Specified by:
readXML in interface XMLControl
Parameters:
xml - the xml string

read

public void read(java.io.Reader in)
Reads the control from a Reader.

Specified by:
read in interface XMLControl
Parameters:
in - the Reader

readForClass

public java.lang.String readForClass(java.lang.String name,
                                     java.lang.Class<?> type)
Reads data into this control from a named source if the source specifies the same class as the current className.

Parameters:
name - the name
type - the class
Returns:
the path of the opened document or null if failed

readXMLForClass

public boolean readXMLForClass(java.lang.String xml,
                               java.lang.Class<?> type)
Reads this control from an xml string if the xml specifies the same class as the current className.

Parameters:
xml - the xml string
type - the class
Returns:
true if successfully read

failedToRead

public boolean failedToRead()
Returns true if the most recent read operation failed.

Specified by:
failedToRead in interface XMLControl
Returns:
true if the most recent read operation failed

write

public java.lang.String write(java.lang.String fileName)
Writes this control as an xml file with the specified name.

Specified by:
write in interface XMLControl
Parameters:
fileName - the file name
Returns:
the path of the saved document or null if failed

write

public void write(java.io.Writer out)
Writes this control to a Writer.

Specified by:
write in interface XMLControl
Parameters:
out - the Writer

writeDocType

public void writeDocType(java.io.Writer out)
Writes the DTD to a Writer.

Parameters:
out - the Writer

toXML

public java.lang.String toXML()
Returns this control as an xml string.

Specified by:
toXML in interface XMLControl
Returns:
the xml string

setValid

public void setValid(boolean valid)
Sets the valid property.

Parameters:
valid - true to write the DTD and DocType

isValid

public boolean isValid()
Gets the valid property. When true, this writes the DTD and defines the DocType when writing an xml document. Note: the presence or absense of the DocType header and DTD has no effect on the read() methods--this will always read a well-formed osp document and ignore a non-osp document.

Returns:
true if this is valid

setVersion

public void setVersion(java.lang.String vers)
Sets the version.

Parameters:
vers - the version data

getVersion

public java.lang.String getVersion()
Gets the version. May return null.

Returns:
the version

setDoctype

public void setDoctype(java.lang.String name)
Sets the doctype. Not yet implemented since only one doctype is defined.

Parameters:
name - the doctype resource name

getDoctype

public java.lang.String getDoctype()
Gets the doctype. May return null.

Returns:
the doctype

setObjectClass

public void setObjectClass(java.lang.Class<?> type)
Sets the class of the object for which this element stores data.

Parameters:
type - the Class of the object

getObjectClass

public java.lang.Class<?> getObjectClass()
Gets the class of the object for which this element stores data.

Specified by:
getObjectClass in interface XMLControl
Returns:
the Class of the object

getObjectClassName

public java.lang.String getObjectClassName()
Gets the name of the object class for which this element stores data.

Specified by:
getObjectClassName in interface XMLControl
Returns:
the object class name

saveObject

public void saveObject(java.lang.Object obj)
Saves an object's data in this element.

Specified by:
saveObject in interface XMLControl
Parameters:
obj - the object to save.

loadObject

public java.lang.Object loadObject(java.lang.Object obj)
Loads an object with data from this element. This asks the user for approval and review before importing data from mismatched classes.

Specified by:
loadObject in interface XMLControl
Parameters:
obj - the object to load
Returns:
the loaded object

loadObject

public java.lang.Object loadObject(java.lang.Object obj,
                                   boolean autoImport)
Loads an object with data from this element. This asks the user to review data from mismatched classes before importing it.

Parameters:
obj - the object to load
autoImport - true to automatically import data from mismatched classes
Returns:
the loaded object

loadObject

public java.lang.Object loadObject(java.lang.Object obj,
                                   boolean autoImport,
                                   boolean importAll)
Loads an object with data from this element.

Parameters:
obj - the object to load
autoImport - true to automatically import data from mismatched classes
importAll - true to import all importable data
Returns:
the loaded object

clearValues

public void clearValues()
Clears all properties.

Specified by:
clearValues in interface Control

println

public void println(java.lang.String s)
Method required by the Control interface.

Specified by:
println in interface Control
Parameters:
s - the string

println

public void println()
Method required by the Control interface.

Specified by:
println in interface Control

print

public void print(java.lang.String s)
Method required by the Control interface.

Specified by:
print in interface Control
Parameters:
s - the string

clearMessages

public void clearMessages()
Method required by the Control interface.

Specified by:
clearMessages in interface Control

calculationDone

public void calculationDone(java.lang.String s)
Method required by the Control interface.

Specified by:
calculationDone in interface Control
Parameters:
s - the string

getPropertyName

public java.lang.String getPropertyName()
Gets the property name.

Specified by:
getPropertyName in interface XMLProperty
Returns:
a name

getPropertyType

public java.lang.String getPropertyType()
Gets the property type.

Specified by:
getPropertyType in interface XMLProperty
Returns:
the type

getPropertyClass

public java.lang.Class<?> getPropertyClass()
Gets the property class.

Specified by:
getPropertyClass in interface XMLProperty
Returns:
the class

getParentProperty

public XMLProperty getParentProperty()
Gets the immediate parent property, if any.

Specified by:
getParentProperty in interface XMLProperty
Returns:
the parent

getLevel

public int getLevel()
Gets the level of this property relative to the root.

Specified by:
getLevel in interface XMLProperty
Returns:
a non-negative integer

getPropertyContent

public java.util.List<java.lang.Object> getPropertyContent()
Gets the property content of this control.

Specified by:
getPropertyContent in interface XMLProperty
Returns:
a list of XMLProperties

getChildControl

public XMLControl getChildControl(java.lang.String name)
Gets the named XMLControl child of this property. May return null.

Specified by:
getChildControl in interface XMLProperty
Parameters:
name - the property name
Returns:
the XMLControl

getChildControls

public XMLControl[] getChildControls()
Gets the XMLControl children of this property. The returned array has length for type "object" = 1, "collection" and "array" = 0+, other types = 0.

Specified by:
getChildControls in interface XMLProperty
Returns:
an XMLControl array

getRootControl

public XMLControlElement getRootControl()
Gets the root control.

Returns:
the root control

addNumbering

public java.lang.String addNumbering(java.lang.String name)
Appends numbering to a specified name. Increments the number each time this is called for the same name.

Parameters:
name - the name
Returns:
the name with appended numbering

setValue

public void setValue(java.lang.String stringValue)
This does nothing since the property type is "object".

Specified by:
setValue in interface XMLProperty
Parameters:
stringValue - the string value of a primitive or string property

toString

public java.lang.String toString()
Returns the string xml representation.

Overrides:
toString in class java.lang.Object
Returns:
the string xml representation

getObjects

public <T> java.util.List<T> getObjects(java.lang.Class<T> type)
Returns a list of objects of a specified class within this control.

Parameters:
type - the Class
Returns:
the list of objects

getObjects

public <T> java.util.List<T> getObjects(java.lang.Class<T> type,
                                        boolean useChooser)
Returns a list of objects of a specified class within this control.

Parameters:
type - the Class
useChooser - true to allow user to choose
Returns:
the list of objects

clone

public java.lang.Object clone()
Returns a copy of this control.

Overrides:
clone in class java.lang.Object
Returns:
a clone