public class XmlFilePlugin extends BasePlugin
AstManagerPlugin
implementation which is backed by an
XML file.BasePlugin.Pair
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
AST_POSTFIX
Convention for an XML AST filename extension.
|
private static int |
AST_POSTFIX_LEN
Calculated postfix length.
|
private static java.lang.String |
ATTR_CLASS
The class name of the AnnotatedAst subclass used for all instances.
|
private static java.lang.String |
ATTR_COL
The input file column number at which the AST's text starts.
|
private static java.lang.String |
ATTR_DTYPE
The data type of an annotation, as a Java class name.
|
private static java.lang.String |
ATTR_FLAGS
The optional 'original' flag that filename.
|
private static java.lang.String |
ATTR_HIDDEN
The hidden member of the AST.
|
static java.lang.String |
ATTR_ID
The project-unique 64-bit identifier of each AST.
|
private static java.lang.String |
ATTR_KEY
The key with which a unique annotation can be stored or retrieved.
|
private static java.lang.String |
ATTR_LEFT_ID
Left-side AST identifier for a shadow node.
|
private static java.lang.String |
ATTR_LINE
The input file line number on which the AST's text starts.
|
private static java.lang.String |
ATTR_NEXT
The next available AST node ID for that filename.
|
private static java.lang.String |
ATTR_RIGHT_ID
Right-side AST identifier for a shadow node.
|
private static java.lang.String |
ATTR_TERSE
Indicator that this XML document uses terse AST format
|
private static java.lang.String |
ATTR_TEXT
The source text as parsed from the original input file.
|
private static java.lang.String |
ATTR_TREE
The filename that is associated with a unique ID.
|
private static java.lang.String |
ATTR_TTYPE
The token type of the AST.
|
private static java.lang.String |
ATTR_VAL
The value of an annotation.
|
private static java.util.Map<java.lang.String,java.lang.Class<?>> |
classCache
Cache of classes for faster retrieval than
Class.forName alone |
private static java.lang.String |
ELEM_ANN
A single annotation as a child element of an AST element.
|
static java.lang.String |
ELEM_AST
AST-specific element, containing all data as attributes, all
annotations as child elements and any number of child AST elements.
|
private static java.lang.String |
ELEM_LISTITEM
One element of an array annotation.
|
private static java.lang.String |
ELEM_LOCAL
Element which contains all tree-local data.
|
private static java.lang.String |
ELEM_MAP
Represents a single name = ID pair.
|
private static java.lang.String |
ELEM_REGISTRY_ROOT
Root element containing the entire mapping database.
|
static java.lang.String |
ELEM_ROOT
Root element containing the entire AST.
|
private static java.lang.String |
ELEM_SHADOW
Element containing a special shadow node AST.
|
private static java.lang.String |
ELEM_SHADOW_LIST
Element containing a list of shadow nodes.
|
private java.io.File |
file
XML file for persisting the registry.
|
private boolean |
useResources
If
true then plugin reads data from resources rather than files. |
dirty, fids, fnames, HIGH_BITS, locklist, MAX_POSSIBLE, TREE_MASK
Constructor and Description |
---|
XmlFilePlugin(java.lang.String name,
boolean useResources)
Constructor which looks up the registry file or resource name and initializes its data
structures based on reading that file or resource, if it exists.
|
Modifier and Type | Method and Description |
---|---|
private static Aast |
createAst(org.w3c.dom.Element current,
java.lang.Class astCls,
boolean terse)
Reads an AST associated with the passed-in XML element and creates a
new AST based on the associated attributes and annotations.
|
long |
getTreeId(java.lang.String name)
Reverse lookup of the ID that corresponds to a given AST name.
|
boolean |
isExistingAst(java.lang.String name)
Check if the AST specified by the given name exists already in the plugin's storage.
|
private void |
load(java.io.InputStream inputStream)
Read the source XML document and build the mapping database from the stored elements.
|
static Aast |
loadTree(org.w3c.dom.Element root,
boolean inlineShadow)
Load a complete AST by parsing the source XML tree and to obtain the
stored AST and annotation elements.
|
Aast |
loadTree(java.lang.String name)
Load a named AST from persistent storage.
|
private void |
persist(org.w3c.dom.Document dom,
java.io.File target)
Complete the processing by serializing the XML document to the output
file.
|
private static void |
readAnnotations(org.w3c.dom.Element current,
Aast ast)
Reads any annotations associated with the passed-in XML element and
stores these in the AST.
|
private static Aast |
readAst(org.w3c.dom.Element current,
java.lang.Class astCls,
boolean terse,
java.util.List<java.lang.Object> inline)
Processes an AST element in an XML document to create an associated AST
instance and then recursively calls itself for each AST element that
is a child.
|
private static void |
readListElements(org.w3c.dom.Element list,
Aast ast,
java.lang.String key)
Reads the elements of an array annotation associated with the passed-in
XML element and stores these in the AST.
|
private static TreeLocal |
readTreeLocal(org.w3c.dom.Element data,
java.lang.Class astCls,
Aast root,
boolean terse)
Processes a tree-local element in an XML document to create an associated
shadow node instances from the contained list.
|
void |
save()
Create the target XML document, add comments and a timestamp, create a map element for every file and
persist the XML file to the file system.
|
void |
saveTree(Aast ast,
java.lang.String name,
boolean terse)
Save the given AST to persistent storage under the given name.
|
private void |
writeAnnotations(Aast ast,
org.w3c.dom.Document dom,
org.w3c.dom.Node parent)
Process the list of annotations from an AST and write these into the
current XML document as child nodes.
|
private org.w3c.dom.Element |
writeAst(Aast ast,
org.w3c.dom.Document dom,
org.w3c.dom.Node parent,
java.lang.String elemName,
boolean terse)
Write an AST node to XML, recursively calling itself to write all
children.
|
private void |
writeListElements(java.util.List list,
org.w3c.dom.Document dom,
org.w3c.dom.Node parent)
Write a the list elements of an arry annotation (as identified by a
single key) into the current XML document as child nodes of the passed
parent node.
|
private void |
writeSingleAnnotation(Aast ast,
org.w3c.dom.Document dom,
org.w3c.dom.Node parent,
java.lang.String key)
Write a single annotation (as identified by a single key) into the
current XML document as child nodes of the passed parent node.
|
private void |
writeTreeLocal(Aast ast,
org.w3c.dom.Document dom,
org.w3c.dom.Node parent,
boolean terse)
Write the tree-local data to XML.
|
addTree, getFiles, getNextNodeId, getTreeId, getTreeName, isInvalid, lockTree, removeTree, resetNextNodeId, setFlags, setNextNodeId, unlockTree, usesMemoryStorage
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
dom2string
public static final java.lang.String AST_POSTFIX
public static final java.lang.String ELEM_ROOT
public static final java.lang.String ELEM_AST
public static final java.lang.String ATTR_ID
private static final int AST_POSTFIX_LEN
private static final java.lang.String ELEM_ANN
private static final java.lang.String ELEM_LISTITEM
private static final java.lang.String ELEM_LOCAL
private static final java.lang.String ELEM_SHADOW_LIST
private static final java.lang.String ELEM_SHADOW
private static final java.lang.String ELEM_REGISTRY_ROOT
private static final java.lang.String ELEM_MAP
private static final java.lang.String ATTR_CLASS
private static final java.lang.String ATTR_TERSE
private static final java.lang.String ATTR_TEXT
private static final java.lang.String ATTR_TTYPE
private static final java.lang.String ATTR_HIDDEN
private static final java.lang.String ATTR_LINE
private static final java.lang.String ATTR_COL
private static final java.lang.String ATTR_DTYPE
private static final java.lang.String ATTR_KEY
private static final java.lang.String ATTR_VAL
private static final java.lang.String ATTR_LEFT_ID
private static final java.lang.String ATTR_RIGHT_ID
private static final java.lang.String ATTR_TREE
private static final java.lang.String ATTR_NEXT
private static final java.lang.String ATTR_FLAGS
private static final java.util.Map<java.lang.String,java.lang.Class<?>> classCache
Class.forName
aloneprivate java.io.File file
private boolean useResources
true
then plugin reads data from resources rather than files.public XmlFilePlugin(java.lang.String name, boolean useResources) throws AstException
name
- The registry will be loaded from this file or resource. Can be null
.useResources
- If true
then plugin reads data from resources rather than files.AstException
public long getTreeId(java.lang.String name)
getTreeId
in interface AstManagerPlugin
getTreeId
in class BasePlugin
name
- Tree name whose unique ID should be returned.AstManager.INVALID_ID
if the tree name doesn't exist or
is null
.public static Aast loadTree(org.w3c.dom.Element root, boolean inlineShadow) throws AstException
This method will synchronize on the object monitor of the given
root
node.
root
- The root element of an AST document to be read and converted
into an AST.inlineShadow
- Flag indicating that the shadow nodes are inlined with the AST nodes, as their
IDs are not set yet.AstException
- if the XML parser has a failure during parsing.public Aast loadTree(java.lang.String name) throws AstException, java.lang.IllegalArgumentException
name
- The name of the tree. Often this is a file or resource name.AstException
- On any error during the loading process.java.lang.IllegalArgumentException
- If the name is invalid or the specified resource doesn't exist.public void saveTree(Aast ast, java.lang.String name, boolean terse) throws AstException, java.lang.IllegalArgumentException
ast
- The tree to save.name
- The name of the tree. Often this is a file name.terse
- Do not store line and column data when the node data is saved.AstException
- On any error during the saving process.java.lang.IllegalArgumentException
- If the name is invalid or the specified resource cannot be
used as a target to store the AST.public boolean isExistingAst(java.lang.String name)
name
- The name (including path) of the AST file to be checked.true
if the specified AST is in the plugin's storage.public void save() throws AstException
AstException
- If it is not possible to create an empty XML document or in the case that the XML document
cannot be stored in the file system or if the contents of the maps to persist are
null
.private void load(java.io.InputStream inputStream) throws AstException
AstException
- If the XML parser has a failure during parsing or in its file system operations.java.lang.IllegalArgumentException
- If the input file is invalid or doesn't exist.private static Aast readAst(org.w3c.dom.Element current, java.lang.Class astCls, boolean terse, java.util.List<java.lang.Object> inline) throws AstException
current
- Represents an element in the XML document that is an AST
element and may contain 0 or more AST elements.astCls
- The class of the AnnotatedAst subclass used for the instance.terse
- true
if line and column data should not be read
from XML elements (because it doesn't exist).inline
- Optional list for gathering shadow nodes inlined with the normal AST nodes.AstException
private static Aast createAst(org.w3c.dom.Element current, java.lang.Class astCls, boolean terse) throws AstException
current
- The XML AST element.astCls
- The class of the AnnotatedAst subclass used for the instance.terse
- true
if line and column data should not be read
from XML elements (because it doesn't exist).AstException
- if an unknown data type is encountered.private static void readAnnotations(org.w3c.dom.Element current, Aast ast) throws AstException
current
- The XML element which may have annotations to read and
store.ast
- The AST in which to store the annotations.AstException
- if an unknown data type is encountered.private static void readListElements(org.w3c.dom.Element list, Aast ast, java.lang.String key) throws AstException
null
values.list
- The XML element which may have annotations to read and
store.ast
- The AST in which to store the annotations.key
- The annotation's name.AstException
- if an unknown data type is encountered.private static TreeLocal readTreeLocal(org.w3c.dom.Element data, java.lang.Class astCls, Aast root, boolean terse) throws AstException
data
- Represents an element in the XML document that is the
tree-local element.astCls
- The class of the AnnotatedAst subclass used for the instance.root
- The root node for the tree, which will be set as the parent
of each shadow node.terse
- true
if line and column data should not be read
from XML elements (because it doesn't exist).AstException
private org.w3c.dom.Element writeAst(Aast ast, org.w3c.dom.Document dom, org.w3c.dom.Node parent, java.lang.String elemName, boolean terse)
ast
- AST node to persist.dom
- The document being written.parent
- Parent node in the XML document, to which the associated
element should be appended as a child.elemName
- Element name.terse
- Do not store line and column data when the node data is saved.private void writeAnnotations(Aast ast, org.w3c.dom.Document dom, org.w3c.dom.Node parent)
ast
- AST containing the annotations to write.dom
- The document being written.parent
- Parent node in the XML document, to which the associated
element should be appended as a child.private void writeSingleAnnotation(Aast ast, org.w3c.dom.Document dom, org.w3c.dom.Node parent, java.lang.String key)
ast
- AST containing the annotation to write.dom
- The document being written.parent
- Parent node in the XML document, to which the associated
element should be appended as a child.key
- Specifies the annotation to write.private void writeListElements(java.util.List list, org.w3c.dom.Document dom, org.w3c.dom.Node parent)
null
values.list
- Array annotation to write.dom
- The document being written.parent
- Parent node in the XML document, to which the associated
element should be appended as a child.private void writeTreeLocal(Aast ast, org.w3c.dom.Document dom, org.w3c.dom.Node parent, boolean terse)
ast
- AST root node from which the tree-local data can be obtained.dom
- The document being written.parent
- Parent node in the XML document, to which the associated
element should be appended as a child.terse
- Do not store line and column data when the node data is saved.private void persist(org.w3c.dom.Document dom, java.io.File target) throws AstException
dom
- The XML document to persist.target
- The file in which to persist the XML document.AstException
- If any error occurs during XML serialization.