public class Mel
extends java.lang.Object
| Constructor and Description |
|---|
Mel(org.w3c.dom.Document doc,
org.w3c.dom.Element ele)
Standard constructor for a Mel on an existing XML tree.
|
| Modifier and Type | Method and Description |
|---|---|
Mel |
addChild(java.lang.String elementName)
Creates a child element of the Mel class.
|
<T extends Mel> |
addChild(java.lang.String elementName,
java.lang.Class<T> childClass)
Creates a child element of your own class that extends the Mel class
|
void |
addVectorValue(java.lang.String memberName,
java.lang.String value)
If a containing tag has multiple child value tags you append a value to
that set of values.
|
static java.lang.String |
assureValidXMLChars(java.lang.String input) |
boolean |
attributeEquals(java.lang.String attrName,
java.lang.String testValue)
Retrieves an attribute value from a tag, and compares it to a given
value, returning true if they are equal.
|
static <T extends Mel> |
construct(java.lang.Class<T> childClass,
org.w3c.dom.Document doc,
org.w3c.dom.Element ele)
Constructs an instance of an extended class.
|
<T extends Mel> |
convertClass(java.lang.Class<T> desiredClass)
Constructs an instance of an extended class.
|
protected static org.w3c.dom.Document |
convertInputStreamToDocument(java.io.InputStream is) |
protected static org.w3c.dom.Document |
createDocument(java.lang.String rootNodeName)
This method creates a new Document Object.
|
static <T extends Mel> |
createEmpty(java.lang.String rootElement,
java.lang.Class<T> rootClass)
Use this to create a brand new base of the tree of the file.
|
void |
eliminateCData() |
<T extends Mel> |
findChild(java.lang.String elementName,
java.lang.String attributeName,
java.lang.String keyValue,
java.lang.Class<T> childClass)
Returns a child object of specific class with a specified name, attribute, and value
|
java.util.Vector<java.lang.String> |
getAllAttributeNames() |
java.util.Vector<Mel> |
getAllChildren()
getAllChildren will return a vector of Mels, one for each existing child
element, as well as one for each existing data value.
|
java.lang.String |
getAttribute(java.lang.String attrName)
If a containing tag has an attribute, getAttribute will return the string
value of that attribute.
|
long |
getAttributeLong(java.lang.String attrName)
If a containing tag has an attribute, and that attribute contains an
integer string value, then getAttribute will return the long value of
that attribute.
|
Mel |
getChild(java.lang.String elementName,
int index)
Returns a child Mel object with a specified name.
|
<T extends Mel> |
getChild(java.lang.String elementName,
int index,
java.lang.Class<T> childClass)
Returns a child object of the specified class with a specified name.
|
java.util.Vector<Mel> |
getChildren(java.lang.String elementName) |
<T extends Mel> |
getChildren(java.lang.String elementName,
java.lang.Class<T> childClass) |
java.lang.String |
getDataValue()
If you call "getAllChildren" you will get Mels that represent containers,
as well as ones that represent data.
|
org.w3c.dom.Document |
getDocument() |
org.w3c.dom.Element |
getElement()
Avoid using this, but provided for interfacing to external libraries
|
java.lang.String |
getFormattedString()
You really don't want to ever have XML in a string.
|
java.lang.String |
getName()
The name of the data element that this object represents.
|
java.lang.String |
getPrefix()
This returns the namespace prefix, if there is one.
|
static void |
getRawChildren(org.w3c.dom.Element parent,
java.lang.StringBuffer sb,
java.lang.String place) |
java.lang.String |
getRawDOM() |
java.lang.String |
getScalar(java.lang.String memberName)
If a containing tag has a child value tag getScalar will find that tag,
and return the string value.
|
java.util.Vector<java.lang.String> |
getVector(java.lang.String memberName)
If a containing tag has multiple child value tags you can access all the
values at once, retrieving a vector of string values.
|
boolean |
isContainer()
Generally a Mel is a container, meaning it can have children which are
either more Mel or data values.
|
static <T extends Mel> |
parseString(java.lang.String input,
java.lang.Class<T> rootClass)
You really should never use this.
|
static <T extends Mel> |
readFile(java.io.File inFile,
java.lang.Class<T> rootClass)
Given a File object (that points to a real existing MDS file) This global
static method will read the file and return a Mel for the base of the
tree of the file.
|
static <T extends Mel> |
readInputStream(java.io.InputStream is,
java.lang.Class<T> rootClass)
Given a byte stream (that points to a real existing MDS file) this global
static method will read the stream and return a Mel for the base of the
tree of the file.
|
void |
reformatXML() |
void |
removeAllNamedChild(java.lang.String elementName) |
void |
removeChild(Mel mele) |
static int |
safeConvertInt(java.lang.String val)
designed primarily for returning date long values works only for positive
integer (long) values considers all numeral, ignores all letter and
punctuation never throws an exception if you give this something that is
not a number, you get surprising result.
|
static long |
safeConvertLong(java.lang.String val)
designed primarily for returning date long values works only for positive
integer (long) values considers all numeral, ignores all letter and
punctuation never throws an exception if you give this something that is
not a number, you get surprising result.
|
void |
setAttribute(java.lang.String attrName,
java.lang.String value)
Sets an attribute and value on a tag.
|
void |
setAttributeInt(java.lang.String attrName,
int value) |
void |
setAttributeLong(java.lang.String attrName,
long value)
Sets an attribute and an integer (long) value on a tag.
|
void |
setScalar(java.lang.String memberName,
java.lang.String value)
setScalar will create a child value tag of a specified name and value for
the containing element.
|
void |
setVector(java.lang.String memberName,
java.util.Vector<java.lang.String> values)
If a containing tag has multiple child value tags you can access and set
all the values at once using a vector of string values.
|
static void |
writeShortLiteralValue(java.lang.StringBuffer sb,
java.lang.String value) |
void |
writeToFile(java.io.File outFile)
As you might have guessed from the name, the writeToFile method will
write the entire tree out to a file.
|
void |
writeToOutputStream(java.io.OutputStream out)
writeToOutputStream streams the entire XML output that reflects the
entire tree to an output stream.
|
public Mel(org.w3c.dom.Document doc,
org.w3c.dom.Element ele)
public static <T extends Mel> T construct(java.lang.Class<T> childClass, org.w3c.dom.Document doc, org.w3c.dom.Element ele) throws java.lang.Exception
java.lang.Exceptionpublic <T extends Mel> T convertClass(java.lang.Class<T> desiredClass) throws java.lang.Exception
java.lang.Exceptionpublic static <T extends Mel> T readFile(java.io.File inFile, java.lang.Class<T> rootClass) throws java.lang.Exception
java.lang.Exceptionpublic static <T extends Mel> T readInputStream(java.io.InputStream is, java.lang.Class<T> rootClass) throws java.lang.Exception
java.lang.Exceptionpublic static <T extends Mel> T parseString(java.lang.String input, java.lang.Class<T> rootClass) throws java.lang.Exception
java.lang.Exceptionpublic static <T extends Mel> T createEmpty(java.lang.String rootElement, java.lang.Class<T> rootClass) throws java.lang.Exception
java.lang.Exceptionpublic void writeToOutputStream(java.io.OutputStream out)
throws java.lang.Exception
java.lang.Exceptionpublic java.lang.String getFormattedString()
throws java.lang.Exception
java.lang.Exceptionpublic void writeToFile(java.io.File outFile)
throws java.lang.Exception
As you might have guessed from the name, the writeToFile method will write the entire tree out to a file. You must pass in a valid full path to a file name.
To assure atomic writing, the file is first output to a temporary file that is the same name, but with a "tmp-###" appended to the end of the name. Only when the entire tree is successfully output to the file, then the file is renamed to the desired name.
If there was a file already there with that name, then that file will be deleted, just before the temporary file is renamed to the desired name.
If your program crashes for any reason before the entire file is output, the result will be that the previous version of the file will still be there, and there will be a partially written file with a "tmp-###" appended to the end. Since these are most likely fragments of the entire file, they are not valid XML files, and can not be parsed. They should be discarded, and the cause of the crash researched. There are no normal situation that these files should be seen left in the file system.
Here is a specific example. If you want to write to a file with the path "c:/data/MyConfig.cfg" the folder "c:/data" must exist. The output routine will choose a random number, for example '137' and will write the contents of the tree to "c:/data/MyConfig.cfg-tmp-137". Once the file is completely output to disk, the old file "MyConfig.cfg" is deleted, and the temp file is renamed to "MyConfig.cfg".
With this approach you are most likely to always have a valid file with the desired name. There is an extremely small possibility that the program might crash between the deletion of the old file, and the renaming of the temp file, leaving you with no file at all with the desired name. There is a very small window of time between these two operations where another program or another thread might find the file missing from the file system. This seems to be a limitation that we have to live with until Java offer an atomic rename-and-delete-previous-file operation.
java.lang.Exceptionpublic java.lang.String getName()
public java.lang.String getPrefix()
public boolean isContainer()
public java.lang.String getDataValue()
public java.lang.String getAttribute(java.lang.String attrName)
public long getAttributeLong(java.lang.String attrName)
public void setAttribute(java.lang.String attrName,
java.lang.String value)
public void setAttributeLong(java.lang.String attrName,
long value)
public void setAttributeInt(java.lang.String attrName,
int value)
public boolean attributeEquals(java.lang.String attrName,
java.lang.String testValue)
throws java.lang.Exception
java.lang.Exceptionpublic java.util.Vector<java.lang.String> getAllAttributeNames()
public java.lang.String getScalar(java.lang.String memberName)
public void setScalar(java.lang.String memberName,
java.lang.String value)
public java.util.Vector<java.lang.String> getVector(java.lang.String memberName)
public void setVector(java.lang.String memberName,
java.util.Vector<java.lang.String> values)
public void addVectorValue(java.lang.String memberName,
java.lang.String value)
public <T extends Mel> T addChild(java.lang.String elementName, java.lang.Class<T> childClass) throws java.lang.Exception
java.lang.Exceptionpublic Mel addChild(java.lang.String elementName) throws java.lang.Exception
java.lang.Exceptionpublic void removeChild(Mel mele) throws java.lang.Exception
java.lang.Exceptionpublic void removeAllNamedChild(java.lang.String elementName)
throws java.lang.Exception
java.lang.Exceptionpublic Mel getChild(java.lang.String elementName, int index) throws java.lang.Exception
java.lang.Exceptionpublic <T extends Mel> T getChild(java.lang.String elementName, int index, java.lang.Class<T> childClass) throws java.lang.Exception
java.lang.Exceptionpublic <T extends Mel> T findChild(java.lang.String elementName, java.lang.String attributeName, java.lang.String keyValue, java.lang.Class<T> childClass) throws java.lang.Exception
java.lang.Exceptionpublic java.util.Vector<Mel> getChildren(java.lang.String elementName) throws java.lang.Exception
java.lang.Exceptionpublic <T extends Mel> java.util.Vector<T> getChildren(java.lang.String elementName, java.lang.Class<T> childClass) throws java.lang.Exception
java.lang.Exceptionpublic java.util.Vector<Mel> getAllChildren() throws java.lang.Exception
java.lang.Exceptionpublic org.w3c.dom.Element getElement()
public org.w3c.dom.Document getDocument()
public static long safeConvertLong(java.lang.String val)
public static int safeConvertInt(java.lang.String val)
public void reformatXML()
throws java.lang.Exception
java.lang.Exceptionpublic void eliminateCData()
throws java.lang.Exception
java.lang.Exceptionprotected static org.w3c.dom.Document createDocument(java.lang.String rootNodeName)
throws java.lang.Exception
java.lang.Exceptionprotected static org.w3c.dom.Document convertInputStreamToDocument(java.io.InputStream is)
throws java.lang.Exception
java.lang.Exceptionpublic static java.lang.String assureValidXMLChars(java.lang.String input)
public java.lang.String getRawDOM()
public static void getRawChildren(org.w3c.dom.Element parent,
java.lang.StringBuffer sb,
java.lang.String place)
public static void writeShortLiteralValue(java.lang.StringBuffer sb,
java.lang.String value)