Skip to content

Commit

Permalink
[SHRINKDESC-84] Introduced relative and absolute "get" queries; added
Browse files Browse the repository at this point in the history
test cases to cover.
  • Loading branch information
bartoszmajsak authored and ALRubinger committed Sep 6, 2011
1 parent 69652ff commit 77be81a
Show file tree
Hide file tree
Showing 20 changed files with 956 additions and 351 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,25 +224,25 @@ public Node getRootNode()
@Override
public String getVersion()
{
return model.getAttributes().get("version");
return model.getAttribute("version");
}

@Override
public String getDisplayName()
{
return model.getAttributes().get("display-name");
return model.getAttribute("display-name");
}

@Override
public String getDescription()
{
return model.getAttributes().get("description");
return model.getAttribute("description");
}

@Override
public String getLibraryDirectory()
{
return model.getAttributes().get("library-directory");
return model.getAttribute("library-directory");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ else if (SchemaGenerationModeType.NONE.equals(schemaGenerationMode))
@Override
public String getName()
{
return persistenceUnit.getAttributes().get("name");
return persistenceUnit.getAttribute("name");
}

@Override
Expand All @@ -367,7 +367,7 @@ public String getNonJtaDataSource()
@Override
public String getTransactionType()
{
return persistenceUnit.getAttributes().get("transaction-type");
return persistenceUnit.getAttribute("transaction-type");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,37 +507,37 @@ private String getSimpleName(String fqcn)
@Override
public String getVersion()
{
return model.getAttributes().get("version");
return model.getAttribute("version");
}

@Override
public String getModuleName()
{
return model.getAttributes().get("module-name");
return model.getAttribute("module-name");
}

@Override
public String getDescription()
{
return model.getAttributes().get("description");
return model.getAttribute("description");
}

@Override
public String getDisplayName()
{
return model.getAttributes().get("display-name");
return model.getAttribute("display-name");
}

@Override
public boolean isDistributable()
{
return model.getAttributes().get("distributable") != null;
return model.getAttribute("distributable") != null;
}

@Override
public boolean isMetadataComplete()
{
String complete = model.getAttributes().get("metadata-complete");
String complete = model.getAttribute("metadata-complete");
if (complete == null)
{
complete = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public Node(final String name) throws IllegalArgumentException
public Node(final String name, final Node parent) throws IllegalArgumentException
{
// Precondition checks
if (name == null)
if (name == null || name.trim().length() == 0)
{
throw new IllegalArgumentException("name must be specified");
}
Expand Down Expand Up @@ -280,7 +280,7 @@ public boolean isRoot()
public Node createChild(final String name) throws IllegalArgumentException
{
// Precondition checks
if (name == null || name.length() == 0)
if (name == null || name.trim().length() == 0)
{
throw new IllegalArgumentException("name must be specified");
}
Expand All @@ -291,7 +291,7 @@ public Node createChild(final String name) throws IllegalArgumentException

public Node createChild(final Pattern... patterns)
{
return CreateQuery.INSTANCE.execute(this, patterns);
return CreateQuery.create().execute(this, patterns);
}

/**
Expand All @@ -312,7 +312,7 @@ public Node getOrCreate(String name)

public Node getOrCreate(final Pattern... patterns)
{
return GetOrCreateQuery.INSTANCE.execute(this, patterns);
return GetOrCreateQuery.create().execute(this, includeRootPatternFirst(patterns));
}

/**
Expand All @@ -331,7 +331,7 @@ public Node getSingle(String name)

public Node getSingle(final Pattern... patterns)
{
return GetSingleQuery.INSTANCE.execute(this, patterns);
return GetSingleQuery.absolute().execute(this, includeRootPatternFirst(patterns));
}

/**
Expand All @@ -353,7 +353,7 @@ public List<Node> get(String name)
*/
public List<Node> get(Pattern... patterns)
{
return GetQuery.INSTANCE.execute(this, patterns);
return GetQuery.absolute().execute(this, includeRootPatternFirst(patterns));
}

/**
Expand All @@ -365,7 +365,7 @@ public List<Node> get(Pattern... patterns)
*/
public List<Node> removeChildren(final String name) throws IllegalArgumentException
{
if (name == null || name.trim().isEmpty())
if (name == null || name.trim().length() == 0)
{
throw new IllegalArgumentException("Path must not be null or empty");
}
Expand Down Expand Up @@ -619,4 +619,9 @@ private Pattern[] validateAndMergePatternInput(final Pattern pattern, final Patt
}
return merged.toArray(PATTERN_CAST);
}

private Pattern[] includeRootPatternFirst(final Pattern... patterns)
{
return validateAndMergePatternInput(new Pattern(name), patterns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* A pattern that may be executed as part of a {@link Query} upon
* a {@link Node} in a search, or used to define a target {@link Node}
* to be created. Value object analogous to XPath patterns; it describes
* to be created. Value object analogous to XPath patterns; it describes
* something in the tree structure of {@link Node}.
*
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
Expand Down Expand Up @@ -58,7 +58,7 @@ public final class Pattern
public Pattern(final String name) throws IllegalArgumentException
{
// Precondition check
if (name == null || name.length() == 0)
if (name == null || name.trim().length() == 0)
{
throw new IllegalArgumentException("name must be specified");
}
Expand Down Expand Up @@ -161,20 +161,26 @@ public boolean matches(final Node node) throws IllegalArgumentException
{
return false;
}

if ((text != null && node.getText() == null) || (text != null && !text.trim().equals(node.getText().trim())))
{
return false;
}

if (attributes != null)
{
for (Map.Entry<String, String> attribute : attributes.entrySet())
for (final Map.Entry<String, String> attribute : attributes.entrySet())
{
if (!attribute.getValue().equals(node.getAttribute(attribute.getKey())))
final String attrValue = attribute.getValue();
final String attrName = attribute.getKey();

if (!attrValue.equals(node.getAttribute(attrName)))
{
return false;
}
}
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static Pattern[] from(final String queryExpression) throws IllegalArgumen
{
if (queryExpression == null)
{
throw new IllegalArgumentException("query expression must be specified");
throw new IllegalArgumentException("Query expression must be specified");
}

boolean isAbsolute = queryExpression.startsWith(PATH_SEPARATOR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ public interface Query<T>
* specified or no {@link Pattern}s are specified
*/
T execute(Node node, Pattern... patterns) throws IllegalArgumentException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
*/
public enum CreateQuery implements Query<Node> {
INSTANCE;
public class CreateQuery implements Query<Node> {


public static CreateQuery create()
{
return new CreateQuery();
}

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,21 @@
* @author <a href="mailto:aslak@redhat.com">Aslak Knutsen</a>
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
*/
public enum GetOrCreateQuery implements Query<Node> {
public class GetOrCreateQuery implements Query<Node> {

/**
* Instance
*/
INSTANCE;

/**
* Logger
*/
private static final Logger log = Logger.getLogger(GetOrCreateQuery.class.getName());

/**
* Used in casting
*/
private static final Pattern[] PATTERN_CAST = new Pattern[]
{};


public static GetOrCreateQuery create()
{
return new GetOrCreateQuery();
}

/**
* {@inheritDoc}
Expand Down Expand Up @@ -81,7 +79,7 @@ private Node findOrCreate(final Node root, final List<Pattern> patternsToSearch,
final Pattern... allPatterns)
{

final Node found = GetSingleQuery.INSTANCE.execute(root, patternsToSearch.toArray(PATTERN_CAST));
final Node found = GetSingleQuery.absolute().execute(root, patternsToSearch.toArray(PATTERN_CAST));

// Not found; we'll have to make it
if (found == null)
Expand All @@ -102,7 +100,7 @@ private Node findOrCreate(final Node root, final List<Pattern> patternsToSearch,
{
log.finest("Still not found, root: " + root);
}
return CreateQuery.INSTANCE.execute(root, allPatterns);
return CreateQuery.create().execute(root, allPatterns);
}
else
{
Expand Down Expand Up @@ -131,8 +129,7 @@ private Node findOrCreate(final Node root, final List<Pattern> patternsToSearch,
}

// Create the new Node and return it
final Node newNode = CreateQuery.INSTANCE.execute(found, patternsToCreate);
return newNode;
return CreateQuery.create().execute(found, patternsToCreate);
}
// Otherwise just return the Node we found (like a "get" operation)
else
Expand Down

0 comments on commit 77be81a

Please sign in to comment.