tcurdt 02/04/04 06:12:47 Modified: src/scratchpad/src/org/apache/cocoon/precept Instance.java src/scratchpad/src/org/apache/cocoon/precept/preceptors/easyrelax PreceptorImpl.java src/scratchpad/src/org/apache/cocoon/precept/stores/bean InstanceImpl.java src/scratchpad/src/org/apache/cocoon/precept/stores/dom/simple InstanceImpl.java Log: introduce node exposing in the instance interface, fixed compilation, cleanup Revision Changes Path 1.4 +5 -2 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/Instance.java Index: Instance.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/Instance.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- Instance.java 4 Apr 2002 07:02:09 -0000 1.3 +++ Instance.java 4 Apr 2002 14:12:47 -0000 1.4 @@ -53,7 +53,9 @@ import org.apache.avalon.framework.component.Component; import org.xml.sax.ContentHandler; -import org.xml.sax.SAXException; +import org.xml.sax.SAXException; + +import java.util.Collection; /* * @version: Mar 15, 2002 @@ -64,7 +66,8 @@ public void setValue(String xpath, Object value) throws PreceptorViolationException, InvalidXPathSyntaxException; public void setValue(String xpath, Object value, Context context) throws PreceptorViolationException, InvalidXPathSyntaxException; - public Object getValue(String xpath) throws InvalidXPathSyntaxException, NoSuchNodeException; + public Object getValue(String xpath) throws InvalidXPathSyntaxException, NoSuchNodeException; + public Collection getNodePaths(); public void setPreceptor( Preceptor preceptor ); public Preceptor getPreceptor(); 1.4 +24 -7 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/preceptors/easyrelax/PreceptorImpl.java Index: PreceptorImpl.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/preceptors/easyrelax/PreceptorImpl.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- PreceptorImpl.java 4 Apr 2002 07:02:09 -0000 1.3 +++ PreceptorImpl.java 4 Apr 2002 14:12:47 -0000 1.4 @@ -60,6 +60,7 @@ * @version: Mar 14, 2002 * @author: Torsten Curdt <[EMAIL PROTECTED]> */ + public class PreceptorImpl extends AbstractPreceptor { HashMap index = new HashMap(); @@ -70,23 +71,39 @@ Object value = instance.getValue(xpath); for (Iterator it = constraints.iterator(); it.hasNext();) { Constraint constraint = (Constraint) it.next(); - if (!constraint.isSatisfiedBy(value,context)) { + if (!constraint.isSatisfiedBy(value, context)) { if (violations == null) { violations = new HashSet(); } violations.add(constraint); } } - return(violations); + return (violations); } else { - return(null); + return (null); } } public Collection validate(Instance instance, Context context) throws InvalidXPathSyntaxException { - /* we need obtaint a list of all nodes in the instance here */ - return(null); + Collection allViolations = null; + Collection xpaths = instance.getNodePaths(); + for (Iterator it = xpaths.iterator(); it.hasNext();) { + String xpath = (String) it.next(); + try { + Collection violations = validate(instance, xpath, context); + if (violations != null) { + if (allViolations == null) { + allViolations = new HashSet(); + } + allViolations.addAll(violations); + } + } + catch (NoSuchNodeException e) { + getLogger().error("hm.. the instance just told us about the nodes!"); + } + } + return (allViolations); } public Collection getConstraintsFor(String xpath) throws NoSuchNodeException { @@ -192,8 +209,8 @@ getLogger().debug("found node [" + String.valueOf(currentPath) + "] in index"); if (node instanceof ElementPreceptorNode) { - if (((ElementPreceptorNode)node).getMaxOcc() != ElementPreceptorNode.UNBOUND && levelInt > ((ElementPreceptorNode)node).getMaxOcc()) { - getLogger().debug(String.valueOf(levelName) + "[" + levelInt + "] exceeds maximal occurrences [" + ((ElementPreceptorNode)node).getMaxOcc() + "]"); + if (((ElementPreceptorNode) node).getMaxOcc() != ElementPreceptorNode.UNBOUND && levelInt > ((ElementPreceptorNode) node).getMaxOcc()) { + getLogger().debug(String.valueOf(levelName) + "[" + levelInt + "] exceeds maximal occurrences [" + ((ElementPreceptorNode) node).getMaxOcc() + "]"); return (false); } } 1.3 +5 -12 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/stores/bean/InstanceImpl.java Index: InstanceImpl.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/stores/bean/InstanceImpl.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- InstanceImpl.java 25 Mar 2002 23:23:55 -0000 1.2 +++ InstanceImpl.java 4 Apr 2002 14:12:47 -0000 1.3 @@ -69,6 +69,7 @@ import org.apache.cocoon.precept.stores.AbstractInstance; import java.util.List; +import java.util.Collection; /* * @version: Mar 15, 2002 @@ -152,18 +153,6 @@ preceptor.buildInstance(this); } - public List validate(String xpath, Context context) throws InvalidXPathSyntaxException, NoSuchNodeException { - - //NYI - return null; - } - - public List validate(Context context) throws InvalidXPathSyntaxException { - - //NYI - return null; - } - public Preceptor getPreceptor() { return (preceptor); } @@ -190,5 +179,9 @@ catch (MarshalException e) { throw new SAXException(e); } + } + + public Collection getNodePaths() { + return null; } } 1.4 +14 -55 xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/stores/dom/simple/InstanceImpl.java Index: InstanceImpl.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/scratchpad/src/org/apache/cocoon/precept/stores/dom/simple/InstanceImpl.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- InstanceImpl.java 4 Apr 2002 07:02:09 -0000 1.3 +++ InstanceImpl.java 4 Apr 2002 14:12:47 -0000 1.4 @@ -129,7 +129,6 @@ getLogger().debug("creating node [" + String.valueOf(currentPath) + "]"); ((ElementNode) currentParent).addChild(node); index.put(currentPath.toString(), node); - index.put(currentPath.toString() + "[1]", node); } } else { @@ -141,7 +140,6 @@ node = root = new ElementNode(level, null); } index.put(currentPath.toString(), node); - index.put(currentPath.toString() + "[1]", node); } } currentParent = node; @@ -154,12 +152,21 @@ } } + private Node lookupNode( String xpath ) { + Node node = (Node) index.get(xpath); + if (node == null) { + node = (Node) index.get(xpath + "[1]"); + } + return(node); + } + public void setValue(String xpath, Object value) throws PreceptorViolationException, InvalidXPathSyntaxException { setValue(xpath, value, null); } public void setValue(String xpath, Object value, Context context) throws PreceptorViolationException, InvalidXPathSyntaxException { - Node node = (Node) index.get(xpath); + Node node = lookupNode(xpath); + if (node != null) { node.setValue((String) value); } @@ -183,7 +190,7 @@ } public Object getValue(String xpath) throws InvalidXPathSyntaxException, NoSuchNodeException { - Node node = (Node) index.get(xpath); + Node node = lookupNode(xpath); if (node != null) { return (node.getValue()); } @@ -192,57 +199,6 @@ } } -/* - public List validate(Context context) throws InvalidXPathSyntaxException { - ArrayList all = null; - for (Iterator it = index.keySet().iterator(); it.hasNext();) { - String xpath = (String) it.next(); - if (!xpath.endsWith("[1]")) { - try { - List result = validate(xpath, context); - if (result != null) { - getLogger().debug("constraint violations for [" + String.valueOf(xpath) + "]"); - if (all == null) all = new ArrayList(); - all.addAll(result); - } - else { - getLogger().debug("[" + String.valueOf(xpath) + "] is valid"); - } - } - catch (NoSuchNodeException e) { - getLogger().error("hm... this should not happen!"); - } - } - } - return (all); - } - - public List validate(String xpath, Context context) throws InvalidXPathSyntaxException, NoSuchNodeException { - Node node = (Node) index.get(xpath); - if (node != null) { - ArrayList result = null; - List constraints = node.getConstraints(); - if (constraints != null) { - for (Iterator it = constraints.iterator(); it.hasNext();) { - Constraint constraint = (Constraint) it.next(); - if (constraint.isSatisfiedBy(getValue(xpath), context )) { - getLogger().debug("[" + String.valueOf(xpath) + "] constraint [" + String.valueOf(constraint) + "] is satisfied"); - } - else { - getLogger().debug("[" + String.valueOf(xpath) + "] constraint [" + String.valueOf(constraint) + "] FAILED!"); - if (result == null) result = new ArrayList(); - result.add(constraint); - } - } - } - return (result); - } - else { - getLogger().error("could not find node [" + String.valueOf(xpath) + "]"); - throw new NoSuchNodeException(xpath); - } - } -*/ public Preceptor getPreceptor() { return (preceptor); } @@ -269,4 +225,7 @@ } } + public Collection getNodePaths() { + return( Collections.unmodifiableCollection( index.keySet() )); + } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]