You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2025/11/02 07:10:43 UTC
(directory-ldap-api) branch master updated: o Modified the subtree specification parser to add a check feature o Using the SubtreeSpecification parser instead of the checker for the subtree validation
This is an automated email from the ASF dual-hosted git repository.
elecharny pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git
The following commit(s) were added to refs/heads/master by this push:
new e611d4de5 o Modified the subtree specification parser to add a check feature o Using the SubtreeSpecification parser instead of the checker for the subtree validation
e611d4de5 is described below
commit e611d4de5ee8caccfd184946d4041a9008474bdb
Author: Emmanuel Lécharny <em...@worteks.com>
AuthorDate: Sun Nov 2 08:10:20 2025 +0100
o Modified the subtree specification parser to add a check feature
o Using the SubtreeSpecification parser instead of the checker for the
subtree validation
---
.../apache/directory/api/i18n/errors.properties | 30 +--
.../SubtreeSpecificationSyntaxCheckerTest.java | 7 +-
.../SubtreeSpecificationSyntaxChecker.java | 21 +-
.../model/subtree/SubtreeSpecificationParser.java | 254 ++++++++++++++++-----
4 files changed, 222 insertions(+), 90 deletions(-)
diff --git a/i18n/src/main/resources/org/apache/directory/api/i18n/errors.properties b/i18n/src/main/resources/org/apache/directory/api/i18n/errors.properties
index cd9db06c4..418d7cabe 100644
--- a/i18n/src/main/resources/org/apache/directory/api/i18n/errors.properties
+++ b/i18n/src/main/resources/org/apache/directory/api/i18n/errors.properties
@@ -884,22 +884,22 @@ ERR_15004_CANNOT_GENERATE_SOURCES=Failed while generating sources for {0}: {1}
ERR_15005_NO_PROPERTY=the schema property must be set
ERR_15006_SPACE_REQUIRED=The schema description requires a space at {0}
ERR_15007_UNEXPECTED_ESCAPED_SECOND_CHAR=The second escaped char is not one of ''c'', ''C'' or ''7''.
-ERR_15008_OID_REQUIRED=The schema description requires and OID at {0}, read {1}
-ERR_15009_MISSING_ESCAPED_CHAR=The schema description descr has an escaped sequence that is not for '' or '\\' at {0}
-ERR_15010_MISSING_QDESCR_LQUOTE=The schema description descr requires an opening simple quote at {0}
-ERR_15011_MISSING_ESCAPED_QUOTE=The schema description descr requires '\\'27 at {0}, got '\\2'{1}
-ERR_15012_MISSING_ESCAPED_ESCAPE=The schema description descr requires '\\'5c at {0}, got '\\5'{1}
-ERR_15013_QDESCR_INCOMPLET=The schema description qdescr requires closing simple quote for the qdescr part at {0}
-ERR_15014_AT_WRONG_USAGE= The schema description AttributeType usage is incorrect at {0}, expected on of DirectoryOperation, DistributedOperation or DSAOperation, got {1}
-ERR_15015_MISSING_DESC_LQUOTE=The schema description DESC requires an opening simple quote at {0}
-ERR_15016_MISSING_QDSTRING_RPAREN=The schema description qdstring requires a closing parenthesis at {0}
-ERR_15017_MISSING_OID_LEN_RIGHT_CURLY=The schema description syntax len is incorrect, there is a missing '}' at {0}
-ERR_15018_MANDATORY_SPACE_MISSING=The schema description is requiring a space at position {0}
-ERR_15019_MISSING_OIDS_RPAREN=The schema description oids requires a closing parenthesis at {0}
-ERR_15020_MISSING_SCHEMA_ELEMENT_RPAREN=The schema description element requires a closing parenthesis at {0}
-ERR_15021_NO_SYTNAX_LEN=The schema description syntax expects a length which is absent at {0}
+ERR_15008_OID_REQUIRED=The schema description requires and OID, read {0}
+ERR_15009_MISSING_ESCAPED_CHAR=The schema description descr has an escaped sequence that is not for '' or '\\'
+ERR_15010_MISSING_QDESCR_LQUOTE=The schema description descr requires an opening simple quote
+ERR_15011_MISSING_ESCAPED_QUOTE=The schema description descr requires '\\'27, got '\\2'{0}
+ERR_15012_MISSING_ESCAPED_ESCAPE=The schema description descr requires '\\'5c, got '\\5'{0}
+ERR_15013_QDESCR_INCOMPLET=The schema description qdescr requires closing simple quote for the qdescr part
+ERR_15014_AT_WRONG_USAGE= The schema description AttributeType usage is incorrect at, expected on of DirectoryOperation, DistributedOperation or DSAOperation, got {0}
+ERR_15015_MISSING_DESC_LQUOTE=The schema description DESC requires an opening simple quote
+ERR_15016_MISSING_QDSTRING_RPAREN=The schema description qdstring requires a closing parenthesis
+ERR_15017_MISSING_OID_LEN_RIGHT_CURLY=The schema description syntax len is incorrect, there is a missing '}'
+ERR_15018_MANDATORY_SPACE_MISSING=The schema description is requiring a space
+ERR_15019_MISSING_OIDS_RPAREN=The schema description oids requires a closing parenthesis
+ERR_15020_MISSING_SCHEMA_ELEMENT_RPAREN=The schema description element requires a closing parenthesis
+ERR_15021_NO_SYTNAX_LEN=The schema description syntax expects a length which is absent
ERR_15022_SCHEMA_ELEMENT_MISSING_RPAREN=The schema description is missing a final right parenthesis
-ERR_15023_IMPROPER_EXTENSION_NAME=The schema description extension must start with 'X-' followed by [a-zA-Z-_]+ characters, got {0} at {1}
+ERR_15023_IMPROPER_EXTENSION_NAME=The schema description extension must start with 'X-' followed by [a-zA-Z-_]+ characters, got {0}
# api-ldap-schema-data 16000 - 16999
ERR_16000_CANNOT_OVEWRITE_SCHEMA=Cannot overwrite yet schema output directory already exists: {0}
diff --git a/integ/src/test/java/org/apache/directory/api/ldap/schema/syntaxCheckers/SubtreeSpecificationSyntaxCheckerTest.java b/integ/src/test/java/org/apache/directory/api/ldap/schema/syntaxCheckers/SubtreeSpecificationSyntaxCheckerTest.java
index 59f6cf70a..4b0233362 100644
--- a/integ/src/test/java/org/apache/directory/api/ldap/schema/syntaxCheckers/SubtreeSpecificationSyntaxCheckerTest.java
+++ b/integ/src/test/java/org/apache/directory/api/ldap/schema/syntaxCheckers/SubtreeSpecificationSyntaxCheckerTest.java
@@ -29,6 +29,7 @@ import org.apache.directory.api.ldap.model.schema.syntaxCheckers.SubtreeSpecific
import org.apache.directory.api.ldap.schema.loader.JarLdifSchemaLoader;
import org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager;
import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@@ -118,7 +119,7 @@ public class SubtreeSpecificationSyntaxCheckerTest
/** A valid specification with refinement set */
private static final String SPEC_WITH_REFINEMENT = "{ base \"ou=system\", specificationFilter and:{ and:{ item:1.2.3"
- + ", or:{ item:4.5.6, item:person-7 } }, not: item:10.11.12 } }";
+ + ", or:{ item:2.5.6, item:person-7 } }, not: item:2.11.12 } }";
/** A valid specification with base and an empty refinement set */
private static final String SPEC_WITH_BASE_AND_EMPTY_REFINEMENT = "{ base \"ou=system\", specificationFilter and:{ } }";
@@ -127,7 +128,7 @@ public class SubtreeSpecificationSyntaxCheckerTest
private static final String SPEC_WITH_ALL_IN_ONE = "{ base \"ou=departments\""
+ ", specificExclusions { chopBefore:\"x=y\", chopAfter:\"k=l\", chopBefore:\"y=z\", chopAfter:\"l=m\" }"
+ ", minimum 7, maximum 77"
- + ", specificationFilter and:{ and:{ item:1.2.3, or:{ item:4.5.6, item:7.8.9 } }, not: item:10.11.12 } }";
+ + ", specificationFilter and:{ and:{ item:1.2.3, or:{ item:2.5.6, item:2.8.9 } }, not: item:2.11.12 } }";
/** An valid specification with unordinary component order */
private static final String SPEC_ORDER_OF_COMPONENTS_DOES_NOT_MATTER = "{ base \"ou=system\", minimum 3, specificExclusions { chopBefore:\"x=y\" } }";
@@ -280,7 +281,6 @@ public class SubtreeSpecificationSyntaxCheckerTest
public void testSpecWithAllInOne() throws Exception
{
assertTrue( checker.isValidSyntax( SPEC_WITH_ALL_IN_ONE ) );
-
}
@@ -316,6 +316,7 @@ public class SubtreeSpecificationSyntaxCheckerTest
* @throws Exception If the test failed
*/
@Test
+ @Disabled( "Filters are not part of RFC 3672" )
public void testSpecWithFilter() throws Exception
{
assertTrue( checker.isValidSyntax( SPEC_WITH_FILTER ) );
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/SubtreeSpecificationSyntaxChecker.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/SubtreeSpecificationSyntaxChecker.java
index 697029aaa..c823b5fd2 100644
--- a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/SubtreeSpecificationSyntaxChecker.java
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/schema/syntaxCheckers/SubtreeSpecificationSyntaxChecker.java
@@ -20,13 +20,11 @@
package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
-import java.text.ParseException;
-
import org.apache.directory.api.i18n.I18n;
import org.apache.directory.api.ldap.model.constants.SchemaConstants;
import org.apache.directory.api.ldap.model.schema.SchemaManager;
import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
-import org.apache.directory.api.ldap.model.subtree.SubtreeSpecificationChecker;
+import org.apache.directory.api.ldap.model.subtree.SubtreeSpecificationParser;
import org.apache.directory.api.util.Strings;
@@ -41,7 +39,7 @@ import org.apache.directory.api.util.Strings;
public final class SubtreeSpecificationSyntaxChecker extends SyntaxChecker
{
/** The associated checker */
- private transient SubtreeSpecificationChecker subtreeSpecificationChecker;
+ private transient SubtreeSpecificationParser subtreeSpecificationChecker;
/**
* A static instance of SubtreeSpecificationSyntaxChecker
@@ -94,7 +92,7 @@ public final class SubtreeSpecificationSyntaxChecker extends SyntaxChecker
private SubtreeSpecificationSyntaxChecker( String oid, SchemaManager schemaManager )
{
super( oid );
- subtreeSpecificationChecker = new SubtreeSpecificationChecker( schemaManager );
+ subtreeSpecificationChecker = new SubtreeSpecificationParser( schemaManager );
}
@@ -148,21 +146,18 @@ public final class SubtreeSpecificationSyntaxChecker extends SyntaxChecker
return false;
}
- try
- {
- synchronized ( subtreeSpecificationChecker )
- {
- subtreeSpecificationChecker.parse( strValue );
- }
+ boolean isValid = subtreeSpecificationChecker.check( strValue );
+ if ( isValid )
+ {
if ( LOG.isDebugEnabled() )
{
LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
}
-
+
return true;
}
- catch ( ParseException pe )
+ else
{
if ( LOG.isDebugEnabled() )
{
diff --git a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/subtree/SubtreeSpecificationParser.java b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/subtree/SubtreeSpecificationParser.java
index 33b873b65..e9ed11d20 100644
--- a/ldap/model/src/main/java/org/apache/directory/api/ldap/model/subtree/SubtreeSpecificationParser.java
+++ b/ldap/model/src/main/java/org/apache/directory/api/ldap/model/subtree/SubtreeSpecificationParser.java
@@ -125,6 +125,10 @@ public class SubtreeSpecificationParser
private static final String ID_AND = "and";
private static final String ID_OR = "or";
private static final String ID_NOT = "not";
+
+ /** Flags to use t differentiate a parsing from a checking */
+ private static final boolean PARSE = true;
+ private static final boolean VALIDATE = false;
/** The ObjectClass AT */
private static AttributeType objectClassAt;
@@ -209,11 +213,12 @@ public class SubtreeSpecificationParser
* chopAfter = ID_chopAfter ( SP )* COLON ( SP )* distinguishedName
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @ParseException If the input was incorrect
*/
- private void parseSpecificExclusion( String spec, Position pos,
+ private void parseSpecificExclusion( boolean action, String spec, Position pos,
SubtreeSpecificationModifier ssModifier ) throws ParseException
{
String token = getToken( spec, pos );
@@ -244,20 +249,23 @@ public class SubtreeSpecificationParser
throw new ParseException( I18n.err( I18n.ERR_13908_SS_INVALID_DN, dnStr ), pos.start );
}
- switch ( token )
+ if ( action == PARSE )
{
- case ID_CHOP_BEFORE:
- ssModifier.addChopBeforeExclusions( dn );
- return;
-
- case ID_CHOP_AFTER:
-
- ssModifier.addChopAfterExclusions( dn );
- return;
-
- default:
- // We are done
- return;
+ switch ( token )
+ {
+ case ID_CHOP_BEFORE:
+ ssModifier.addChopBeforeExclusions( dn );
+ return;
+
+ case ID_CHOP_AFTER:
+
+ ssModifier.addChopAfterExclusions( dn );
+ return;
+
+ default:
+ // We are done
+ return;
+ }
}
}
@@ -272,11 +280,12 @@ public class SubtreeSpecificationParser
* CLOSE_CURLY
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @ParseException If the input was incorrect
*/
- private void parseSpecificExclusions( String spec, Position pos,
+ private void parseSpecificExclusions( boolean action, String spec, Position pos,
SubtreeSpecificationModifier ssModifier ) throws ParseException
{
// Skip mandatory spaces
@@ -312,7 +321,7 @@ public class SubtreeSpecificationParser
skipSpaces( spec, pos, ZERO_N );
}
- parseSpecificExclusion( spec, pos, ssModifier );
+ parseSpecificExclusion( action, spec, pos, ssModifier );
}
// Skip ending spaces
@@ -329,11 +338,12 @@ public class SubtreeSpecificationParser
* item = ID_item ( SP )* COLON ( SP )* oid
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @ParseException If the input was incorrect
*/
- private ExprNode parseItem( String spec, Position pos ) throws ParseException
+ private ExprNode parseItem( boolean action, String spec, Position pos ) throws ParseException
{
// item = ID_item ( SP )* COLON ( SP )* oid
skipSpaces( spec, pos, ZERO_N );
@@ -357,7 +367,12 @@ public class SubtreeSpecificationParser
throw new ParseException( I18n.err( I18n.ERR_13906_SS_INVALID_ITEM, oid ), pos.start );
}
- ExprNode item = new EqualityNode( objectClassAt, new Value( oid ) );
+ ExprNode item = null;
+
+ if ( action == PARSE )
+ {
+ item = new EqualityNode( objectClassAt, new Value( oid ) );
+ }
return item;
}
@@ -369,19 +384,20 @@ public class SubtreeSpecificationParser
* (ID_and | ID_or) ( SP )* COLON ( SP )* refinements
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @return The parsed node
* @ParseException If the input was incorrect
*/
- private void parseAndOr( String spec, Position pos, BranchNode node ) throws ParseException
+ private void parseAndOr( boolean action, String spec, Position pos, BranchNode node ) throws ParseException
{
// The 'and'/'or' token has already been parsed
skipSpaces( spec, pos, ZERO_N );
matchChar( spec, COLON, pos );
- parseRefinements( spec, pos, node );
+ parseRefinements( action, spec, pos, node );
}
@@ -391,19 +407,25 @@ public class SubtreeSpecificationParser
* ID_not ( SP )* COLON ( SP )* refinement
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @return The parsed node
* @ParseException If the input was incorrect
*/
- private void parseNot( String spec, Position pos, BranchNode node ) throws ParseException
+ private void parseNot( boolean action, String spec, Position pos, BranchNode node ) throws ParseException
{
// The 'and'/'or' token has already been parsed
skipSpaces( spec, pos, ZERO_N );
matchChar( spec, COLON, pos );
- node.addNode( parseRefinement( spec, pos ) );
+ ExprNode refinement = parseRefinement( action, spec, pos );
+
+ if ( action == PARSE )
+ {
+ node.addNode( refinement );
+ }
}
@@ -422,7 +444,7 @@ public class SubtreeSpecificationParser
* @return the parsed nodes
* @ParseException If the input was incorrect
*/
- private void parseRefinements( String spec, Position pos, BranchNode node ) throws ParseException
+ private void parseRefinements( boolean action, String spec, Position pos, BranchNode node ) throws ParseException
{
matchChar( spec, LCURLY, pos );
@@ -451,9 +473,12 @@ public class SubtreeSpecificationParser
skipSpaces( spec, pos, ZERO_N );
- ExprNode refinement = parseRefinement( spec, pos );
+ ExprNode refinement = parseRefinement( action, spec, pos );
- node.addNode( refinement );
+ if ( action == PARSE )
+ {
+ node.addNode( refinement );
+ }
}
}
@@ -468,12 +493,13 @@ public class SubtreeSpecificationParser
* not = ID_not ( SP )* COLON ( SP )* refinement
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @return the parsed refinement
* @ParseException If the input was incorrect
*/
- private ExprNode parseRefinement( String spec, Position pos ) throws ParseException
+ private ExprNode parseRefinement( boolean action, String spec, Position pos ) throws ParseException
{
// Skip optional spaces
skipSpaces( spec, pos, ONE_N );
@@ -484,23 +510,41 @@ public class SubtreeSpecificationParser
switch ( token )
{
case ID_ITEM:
- return parseItem( spec, pos );
+ return parseItem( action, spec, pos );
case ID_AND:
- AndNode andNode = new AndNode();
- parseAndOr( spec, pos, andNode );
+ AndNode andNode = null;
+
+ if ( action == PARSE )
+ {
+ andNode = new AndNode();
+ }
+
+ parseAndOr( action, spec, pos, andNode );
return andNode;
case ID_OR:
- OrNode orNode = new OrNode();
- parseAndOr( spec, pos, orNode );
+ OrNode orNode = null;
+
+ if ( action == PARSE )
+ {
+ orNode = new OrNode();
+ }
+
+ parseAndOr( action, spec, pos, orNode );
return orNode;
case ID_NOT:
- NotNode notNode = new NotNode();
- parseNot( spec, pos, notNode );
+ NotNode notNode = null;
+
+ if ( action == PARSE )
+ {
+ notNode = new NotNode();
+ }
+
+ parseNot( action, spec, pos, notNode );
return notNode;
@@ -518,12 +562,13 @@ public class SubtreeSpecificationParser
* FILTER_VALUE = every char but '(', ')', '&', '|', '!'
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @return the parsed filter
* @ParseException If the input was incorrect
*/
- private String parseFilter( String spec, Position pos ) throws ParseException
+ private String parseFilter( boolean action, String spec, Position pos ) throws ParseException
{
int start = pos.start;
@@ -534,7 +579,14 @@ public class SubtreeSpecificationParser
}
*/
- return spec.substring( start, pos.start );
+ if ( action == PARSE )
+ {
+ return spec.substring( start, pos.start );
+ }
+ else
+ {
+ return null;
+ }
}
/**
@@ -557,12 +609,13 @@ public class SubtreeSpecificationParser
* FILTER_VALUE : ( ')' | '(' | '&' | '|' | '!' ) ( ~(')') )*
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @param ssModifier The SubtreeSpecification modifier instance
* @ParseException If the input was incorrect
*/
- private void parseSpecificationFilter( String spec, Position pos,
+ private void parseSpecificationFilter( boolean action, String spec, Position pos,
SubtreeSpecificationModifier ssModifier ) throws ParseException
{
// Skip mandatory spaces
@@ -600,33 +653,63 @@ public class SubtreeSpecificationParser
throw new ParseException( I18n.err( I18n.ERR_13906_SS_INVALID_ITEM, oid ), pos.start );
}
- ExprNode node = new EqualityNode( objectClassAt, new Value( oid ) );
+ if ( action == PARSE )
+ {
+ ExprNode node = new EqualityNode( objectClassAt, new Value( oid ) );
- ssModifier.setRefinement( node );
+ ssModifier.setRefinement( node );
+ }
return;
case ID_AND:
- AndNode andNode = new AndNode();
- parseAndOr( spec, pos, andNode );
+ AndNode andNode = null;
+
+ if ( action == PARSE )
+ {
+ andNode = new AndNode();
+ }
- ssModifier.setRefinement( andNode );
+ parseAndOr( action, spec, pos, andNode );
+
+ if ( action == PARSE )
+ {
+ ssModifier.setRefinement( andNode );
+ }
return;
case ID_OR:
- OrNode orNode = new OrNode();
- parseAndOr( spec, pos, orNode );
+ OrNode orNode = null;
+
+ if ( action == PARSE )
+ {
+ orNode = new OrNode();
+ }
+
+ parseAndOr( action, spec, pos, orNode );
- ssModifier.setRefinement( orNode );
+ if ( action == PARSE )
+ {
+ ssModifier.setRefinement( orNode );
+ }
return;
case ID_NOT:
- NotNode notNode = new NotNode();
- parseNot( spec, pos, notNode );
+ NotNode notNode = null;
+
+ if ( action == PARSE )
+ {
+ new NotNode();
+ }
+
+ parseNot( action, spec, pos, notNode );
- ssModifier.setRefinement( notNode );
+ if ( action == PARSE )
+ {
+ ssModifier.setRefinement( notNode );
+ }
return;
@@ -668,12 +751,13 @@ public class SubtreeSpecificationParser
* ss_base | ss_specificExclusions | ss_minimum | ss_maximum | ss_specificationFilter
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @param ssModifier The SubtreeSpecification modifier instance
* @ParseException If the input was incorrect
*/
- private void parseSubtreeSpecificationComponent( String spec, Position pos,
+ private void parseSubtreeSpecificationComponent( boolean action, String spec, Position pos,
SubtreeSpecificationModifier ssModifier ) throws ParseException
{
String token = getToken( spec, pos );
@@ -683,13 +767,16 @@ public class SubtreeSpecificationParser
case ID_BASE:
Dn base = parseBase( spec, pos );
- ssModifier.setBase( base );
+ if ( action == PARSE )
+ {
+ ssModifier.setBase( base );
+ }
return;
case ID_SPECIFIC_EXCLUSIONS:
// Parse the exclusions
- parseSpecificExclusions( spec, pos, ssModifier );
+ parseSpecificExclusions( action, spec, pos, ssModifier );
return;
@@ -702,7 +789,10 @@ public class SubtreeSpecificationParser
// Now parse an integer
int minimum = parseInteger( spec, pos );
- ssModifier.setMinBaseDistance( minimum );
+ if ( action == PARSE )
+ {
+ ssModifier.setMinBaseDistance( minimum );
+ }
return;
@@ -715,12 +805,15 @@ public class SubtreeSpecificationParser
// Now parse an integer
int maximum = parseInteger( spec, pos );
- ssModifier.setMaxBaseDistance( maximum );
+ if ( action == PARSE )
+ {
+ ssModifier.setMaxBaseDistance( maximum );
+ }
return;
case ID_SPECIFICATION_FILTER:
- parseSpecificationFilter( spec, pos, ssModifier );
+ parseSpecificationFilter( action, spec, pos, ssModifier );
return;
@@ -743,12 +836,13 @@ public class SubtreeSpecificationParser
* ( SP )*
* </pre>
*
+ * @action Tells if we parse or validate the spec
* @param spec The subtreeSpecification string to parse
* @param pos The position in the string
* @return A SubtreeSpecification instance
* @ParseException If the input was incorrect
*/
- private SubtreeSpecification parse( String spec, Position pos ) throws ParseException
+ private SubtreeSpecification parse( boolean action, String spec, Position pos ) throws ParseException
{
// Get rid of spaces
spec = Strings.trim( spec );
@@ -758,7 +852,13 @@ public class SubtreeSpecificationParser
return null;
}
- SubtreeSpecificationModifier ssModifier = new SubtreeSpecificationModifier();
+
+ SubtreeSpecificationModifier ssModifier = null;
+
+ if ( action == PARSE )
+ {
+ ssModifier = new SubtreeSpecificationModifier();
+ }
// Must have an opening {
matchChar( spec, LCURLY, pos );
@@ -787,18 +887,25 @@ public class SubtreeSpecificationParser
skipSpaces( spec, pos, ZERO_N );
}
- parseSubtreeSpecificationComponent( spec, pos, ssModifier );
+ parseSubtreeSpecificationComponent( action, spec, pos, ssModifier );
}
// Must have an closing }
matchChar( spec, RCURLY, pos );
- return ssModifier.getSubtreeSpecification();
+ if ( action == PARSE )
+ {
+ return ssModifier.getSubtreeSpecification();
+ }
+ else
+ {
+ return null;
+ }
}
/**
- * Parses a subtree specification without exhausting the parser.
+ * Parses a subtree specification
*
* @param spec the specification to be parsed
* @return the specification bean
@@ -814,7 +921,36 @@ public class SubtreeSpecificationParser
Position pos = new Position();
pos.length = spec.length();
- return parse( spec, pos );
+ return parse( PARSE, spec, pos );
+ }
+
+
+ /**
+ * Check a subtree specification
+ *
+ * @param spec the specification to be checked
+ * @return <code>true</code> if the subtree specification is valid, <code>false</code> otherwise
+ **/
+ public boolean check( String spec )
+ {
+ if ( spec == null )
+ {
+ return true;
+ }
+
+ Position pos = new Position();
+ pos.length = spec.length();
+
+ try
+ {
+ parse( VALIDATE, spec, pos );
+
+ return true;
+ }
+ catch ( ParseException pe )
+ {
+ return false;
+ }
}