org.specrunner.context.core
Class ContextImpl

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.AbstractSequentialList<E>
              extended by java.util.LinkedList<IBlock>
                  extended by org.specrunner.context.core.ContextImpl
All Implemented Interfaces:
Serializable, Cloneable, Iterable<IBlock>, Collection<IBlock>, Deque<IBlock>, List<IBlock>, Queue<IBlock>, IBlock, IBlockFactory, IContext

public class ContextImpl
extends LinkedList<IBlock>
implements IContext

Default context implementation.

Author:
Thiago Santos
See Also:
Serialized Form

Field Summary
protected  IChannel channel
          The context channel.
protected  IRunner runner
          The runner.
protected  Deque<ISource> sources
          Queue of sources.
 
Fields inherited from class java.util.AbstractList
modCount
 
Fields inherited from interface org.specrunner.context.IContext
UPACCESS
 
Constructor Summary
ContextImpl(ISource source, IRunner runner)
          Creates a context from a source and a runner.
 
Method Summary
 void addMetadata()
          Add metadata from block context.
 void clearGlobal(String global)
          Removes a variable name from global context.
 void clearLocal(String local)
          Removes a variable name from local context.
 void clearScoped(String scope, String local)
          Removes a scoped variable.
 void clearStrict(String name)
          Clear the context peek mapping from name.
protected  Object findName(int start, String name)
          Find an object by name.
protected  IBlock findNode(int start, Class<? extends nu.xom.Node> type)
          Find a node type.
protected  IBlock findPlugin(int start, Class<? extends IPlugin> type)
          Find a plugin by type.
 IBlock getByElement(Class<? extends nu.xom.Node> type)
          Search a block by element type.
 Object getByName(String name)
          Search a value by its name, from the most restricted block to the outermost (i.e. the root XML element).
 IBlock getByPlugin(Class<? extends IPlugin> type)
          Search a block by plugin type.
 ISource getCurrentSource()
          Get current source.
 Map<String,Object> getMap()
          Mapping of elements related to the block.
 nu.xom.Node getNode()
          Returns the specification node object related to the block.
 Map<String,Object> getObjects()
          A map of all objects in context.
 IBlock getParentByElement(Class<? extends nu.xom.Node> type)
          Strict parent search by element type.
 Object getParentByName(String name)
          A strict parent search a value by its name, from the most restricted block to the outermost (i.e. the root XML element).
 IBlock getParentByPlugin(Class<? extends IPlugin> type)
          Strict parent search by plugin type.
 IPlugin getPlugin()
          The plugin related to the block.
 IRunner getRunner()
          The runner related to the context.
 Deque<ISource> getSources()
          Queue of sources.
 boolean hasChildren()
          Indicates if the block node has children.
 boolean hasMap()
          Indicates if block has a mapping or not.
 boolean hasNode()
          Indicates if block has been created form a specification or not.
 boolean hasPlugin()
          Indicates if block has a plugin or not.
 boolean isChanged()
          Indicates if a given block has changed.
 boolean isValid()
          A valid block has node (hasNode) or plugin ( hasPlugin).
 IBlock newBlock(nu.xom.Node node, IPlugin plugin)
          Creates a new block.
 IBlock newBlock(nu.xom.Node node, IPlugin plugin, Map<String,Object> map)
          Create a new block.
 void saveGlobal(String global, Object obj)
          Saves a variable with the given name to the global context (outermost block).
 void saveLocal(String local, Object obj)
          Saves a variable with the given name to the local context.
 void saveScoped(String scope, String local, Object obj)
          Saves a variable to the given scope.
 void saveStrict(String local, Object obj)
          Save the value to the current context peek, no matter what type of node it is.
 void setChanged(boolean changed)
          Sets the changed status.
 void setMap(Map<String,Object> map)
          Sets the block mapping of object.
 void setNode(nu.xom.Node node)
          Change block node.
 void setPlugin(IPlugin plugin)
          Sets the block plugin.
 void setRunner(IRunner runner)
          Sets the runner.
 void setSources(Deque<ISource> sources)
          Sets the sources.
 
Methods inherited from class java.util.LinkedList
add, add, addAll, addAll, addFirst, addLast, clear, clone, contains, descendingIterator, element, get, getFirst, getLast, indexOf, lastIndexOf, listIterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, set, size, toArray, toArray
 
Methods inherited from class java.util.AbstractSequentialList
iterator
 
Methods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange, subList
 
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Deque
add, addFirst, addLast, contains, descendingIterator, element, getFirst, getLast, iterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, size
 
Methods inherited from interface java.util.Collection
addAll, clear, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 
Methods inherited from interface java.util.List
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, retainAll, subList
 

Field Detail

sources

protected Deque<ISource> sources
Queue of sources.


channel

protected IChannel channel
The context channel.


runner

protected IRunner runner
The runner.

Constructor Detail

ContextImpl

public ContextImpl(ISource source,
                   IRunner runner)
            throws ContextException
Creates a context from a source and a runner.

Parameters:
source - The source.
runner - The runner.
Throws:
ContextException - On creation errors.
Method Detail

getSources

public Deque<ISource> getSources()
Description copied from interface: IContext
Queue of sources. On file inclusion this queue is changed according and is used to avoid cyclic dependency.

Specified by:
getSources in interface IContext
Returns:
The queue.

getCurrentSource

public ISource getCurrentSource()
Description copied from interface: IContext
Get current source.

Specified by:
getCurrentSource in interface IContext
Returns:
The top source.

setSources

public void setSources(Deque<ISource> sources)
Sets the sources.

Parameters:
sources - The sources.

getRunner

public IRunner getRunner()
Description copied from interface: IContext
The runner related to the context.

Specified by:
getRunner in interface IContext
Returns:
The runner.

setRunner

public void setRunner(IRunner runner)
Sets the runner.

Parameters:
runner - The runner.

saveGlobal

public void saveGlobal(String global,
                       Object obj)
Description copied from interface: IContext
Saves a variable with the given name to the global context (outermost block).

Specified by:
saveGlobal in interface IContext
Parameters:
global - The variable name.
obj - The value.

clearGlobal

public void clearGlobal(String global)
Description copied from interface: IContext
Removes a variable name from global context.

Specified by:
clearGlobal in interface IContext
Parameters:
global - The variable name.

saveLocal

public void saveLocal(String local,
                      Object obj)
Description copied from interface: IContext
Saves a variable with the given name to the local context. The local context is defined as the immediate parent of the current node.

Specified by:
saveLocal in interface IContext
Parameters:
local - The variable name.
obj - The value.

clearLocal

public void clearLocal(String local)
Description copied from interface: IContext
Removes a variable name from local context.

Specified by:
clearLocal in interface IContext
Parameters:
local - The variable name.

saveStrict

public void saveStrict(String local,
                       Object obj)
Description copied from interface: IContext
Save the value to the current context peek, no matter what type of node it is.

Specified by:
saveStrict in interface IContext
Parameters:
local - The name.
obj - The value.

clearStrict

public void clearStrict(String name)
Description copied from interface: IContext
Clear the context peek mapping from name.

Specified by:
clearStrict in interface IContext
Parameters:
name - The name.

saveScoped

public void saveScoped(String scope,
                       String local,
                       Object obj)
Description copied from interface: IContext
Saves a variable to the given scope.

Specified by:
saveScoped in interface IContext
Parameters:
scope - Indicates where to bind the value, i.e. when saveScope("body","name","value") is used in a "span" tag, this method searches for the "body" backward tag to add variable.
local - The variable name.
obj - The value.

clearScoped

public void clearScoped(String scope,
                        String local)
Description copied from interface: IContext
Removes a scoped variable.

Specified by:
clearScoped in interface IContext
Parameters:
scope - The scope to unbound.
local - The variable name.

getByElement

public IBlock getByElement(Class<? extends nu.xom.Node> type)
Description copied from interface: IContext
Search a block by element type.

Specified by:
getByElement in interface IContext
Parameters:
type - The type.
Returns:
The nearest block that matches the type.

getParentByElement

public IBlock getParentByElement(Class<? extends nu.xom.Node> type)
Description copied from interface: IContext
Strict parent search by element type.

Specified by:
getParentByElement in interface IContext
Parameters:
type - The type.
Returns:
The nearest parent block that matches the type.

findNode

protected IBlock findNode(int start,
                          Class<? extends nu.xom.Node> type)
Find a node type.

Parameters:
start - The start index.
type - The type.
Returns:
The block found.

getByPlugin

public IBlock getByPlugin(Class<? extends IPlugin> type)
Description copied from interface: IContext
Search a block by plugin type.

Specified by:
getByPlugin in interface IContext
Parameters:
type - The type.
Returns:
The nearest block that matches the type.

getParentByPlugin

public IBlock getParentByPlugin(Class<? extends IPlugin> type)
Description copied from interface: IContext
Strict parent search by plugin type.

Specified by:
getParentByPlugin in interface IContext
Parameters:
type - The type.
Returns:
The nearest parent block that matches the type.

findPlugin

protected IBlock findPlugin(int start,
                            Class<? extends IPlugin> type)
Find a plugin by type.

Parameters:
start - The start index.
type - The type.
Returns:
The block.

getByName

public Object getByName(String name)
Description copied from interface: IContext
Search a value by its name, from the most restricted block to the outermost (i.e. the root XML element).

Specified by:
getByName in interface IContext
Parameters:
name - The name.
Returns:
The nearest block that matches the type.

getParentByName

public Object getParentByName(String name)
Description copied from interface: IContext
A strict parent search a value by its name, from the most restricted block to the outermost (i.e. the root XML element).

Specified by:
getParentByName in interface IContext
Parameters:
name - The name.
Returns:
The nearest block that matches the type.

findName

protected Object findName(int start,
                          String name)
Find an object by name.

Parameters:
start - The start index.
name - The object name.
Returns:
The object.

getObjects

public Map<String,Object> getObjects()
Description copied from interface: IContext
A map of all objects in context. Names are resolved from most specific to most general.

Specified by:
getObjects in interface IContext
Returns:
The naming mapping for this context.

isValid

public boolean isValid()
Description copied from interface: IBlock
A valid block has node (hasNode) or plugin ( hasPlugin). They cannot be both 'null'.

Specified by:
isValid in interface IBlock
Returns:
true, if valid, false otherwise.

hasChildren

public boolean hasChildren()
Description copied from interface: IBlock
Indicates if the block node has children.

Specified by:
hasChildren in interface IBlock
Returns:
true, if is not terminal in the specification, false, otherwise.

isChanged

public boolean isChanged()
Description copied from interface: IBlock
Indicates if a given block has changed.

Specified by:
isChanged in interface IBlock
Returns:
true, if the content of block has changed, false, otherwise.

setChanged

public void setChanged(boolean changed)
Description copied from interface: IBlock
Sets the changed status.

Specified by:
setChanged in interface IBlock
Parameters:
changed - The new status.

hasNode

public boolean hasNode()
Description copied from interface: IBlock
Indicates if block has been created form a specification or not.

Specified by:
hasNode in interface IBlock
Returns:
True, if it has node attached to it, false, otherwise.

getNode

public nu.xom.Node getNode()
Description copied from interface: IBlock
Returns the specification node object related to the block.

Specified by:
getNode in interface IBlock
Returns:
The block node.

setNode

public void setNode(nu.xom.Node node)
Description copied from interface: IBlock
Change block node.

Specified by:
setNode in interface IBlock
Parameters:
node - The new node.

hasPlugin

public boolean hasPlugin()
Description copied from interface: IBlock
Indicates if block has a plugin or not.

Specified by:
hasPlugin in interface IBlock
Returns:
True, if it has a plugin attached to it, false, otherwise.

getPlugin

public IPlugin getPlugin()
Description copied from interface: IBlock
The plugin related to the block.

Specified by:
getPlugin in interface IBlock
Returns:
The block plugin.

setPlugin

public void setPlugin(IPlugin plugin)
Description copied from interface: IBlock
Sets the block plugin.

Specified by:
setPlugin in interface IBlock
Parameters:
plugin - The new plugin.

hasMap

public boolean hasMap()
Description copied from interface: IBlock
Indicates if block has a mapping or not.

Specified by:
hasMap in interface IBlock
Returns:
True, if it has a map attached to it, false, otherwise.

getMap

public Map<String,Object> getMap()
Description copied from interface: IBlock
Mapping of elements related to the block. This mapping is responsible for the storage of variables and elements that are expected to be shared between plugins.

Specified by:
getMap in interface IBlock
Returns:
An object mapping.

setMap

public void setMap(Map<String,Object> map)
Description copied from interface: IBlock
Sets the block mapping of object.

Specified by:
setMap in interface IBlock
Parameters:
map - The new mapping.

newBlock

public IBlock newBlock(nu.xom.Node node,
                       IPlugin plugin)
Description copied from interface: IBlockFactory
Creates a new block.

Specified by:
newBlock in interface IBlockFactory
Parameters:
node - A node.
plugin - A plugin.
Returns:
A new block.

newBlock

public IBlock newBlock(nu.xom.Node node,
                       IPlugin plugin,
                       Map<String,Object> map)
Description copied from interface: IBlockFactory
Create a new block.

Specified by:
newBlock in interface IBlockFactory
Parameters:
node - A node.
plugin - A plugin.
map - An object mapping.
Returns:
A new block.

addMetadata

public void addMetadata()
Description copied from interface: IContext
Add metadata from block context.

Specified by:
addMetadata in interface IContext


Copyright © 2014. All rights reserved.