|
Author: adrianc
Date: Sun Jan 10 07:15:50 2010 New Revision: 897594 URL: http://svn.apache.org/viewvc?rev=897594&view=rev Log: Converted PathNode class to visitor pattern. Added: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java (with props) ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java (with props) ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java (with props) ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java (with props) Modified: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AccessControllerImpl.java ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/ArtifactPath.java ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AuthorizationManagerImpl.java ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNode.java Modified: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AccessControllerImpl.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AccessControllerImpl.java?rev=897594&r1=897593&r2=897594&view=diff ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AccessControllerImpl.java (original) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AccessControllerImpl.java Sun Jan 10 07:15:50 2010 @@ -41,18 +41,18 @@ public static final String module = AccessControllerImpl.class.getName(); protected final OFBizPermission permission; - protected final PathNode node; + protected final PermissionsGatherer permissionsGatherer; // Temporary - will be removed later protected boolean verbose = false; protected boolean disabled = false; protected AccessControllerImpl(PathNode node) { - this.node = node; + this.permissionsGatherer = new PermissionsGatherer(node); this.permission = new OFBizPermission(ThreadContext.getUserLogin().getString("userLoginId")); this.verbose = "true".equals(UtilProperties.getPropertyValue("api.properties", "authorizationManager.verbose")); this.disabled = "true".equals(UtilProperties.getPropertyValue("api.properties", "authorizationManager.disabled")); if (this.verbose) { - Debug.logInfo("Permissions for " + ThreadContext.getUserLogin().getString("userLoginId") + ": \n" + this.node, module); + Debug.logInfo("Permissions for " + ThreadContext.getUserLogin().getString("userLoginId") + ": \n" + node, module); } } @@ -61,7 +61,7 @@ Debug.logInfo("Checking permission: " + ThreadContext.getExecutionPath() + "[" + permission + "]", module); } this.permission.reset(); - this.node.getPermissions(new ArtifactPath(ThreadContext.getExecutionPath()), this.permission); + this.permissionsGatherer.gatherPermissions(new ArtifactPath(ThreadContext.getExecutionPath()), this.permission); if (this.verbose) { Debug.logInfo("Found permission(s): " + ThreadContext.getUserLogin().getString("userLoginId") + "@" + ThreadContext.getExecutionPath() + "[" + this.permission + "]", module); Modified: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/ArtifactPath.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/ArtifactPath.java?rev=897594&r1=897593&r2=897594&view=diff ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/ArtifactPath.java (original) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/ArtifactPath.java Sun Jan 10 07:15:50 2010 @@ -21,14 +21,19 @@ import java.util.Iterator; import java.util.NoSuchElementException; +import javolution.text.TextBuilder; +import javolution.util.FastList; + /** Artifact path class. */ -public class ArtifactPath implements Cloneable, Iterator<String> { +public class ArtifactPath implements Iterator<String> { public static final ArtifactPath PATH_ROOT = new ArtifactPath("ofbiz"); public static final String ELEMENT_SEPARATOR = "/"; protected int currentIndex = 0; protected final String[] pathElementArray; + protected FastList<Integer> stack = null; + protected final TextBuilder stringBuilder = TextBuilder.newInstance(); public ArtifactPath(String artifactPath) { this.pathElementArray = artifactPath.split(ELEMENT_SEPARATOR); @@ -38,22 +43,18 @@ this.pathElementArray = pathElementArray; } - @Override - public ArtifactPath clone() { - ArtifactPath newPath = new ArtifactPath(this.pathElementArray); - newPath.currentIndex = this.currentIndex; - return newPath; - } - public String getCurrentPath() { - StringBuilder sb = new StringBuilder(); + if (this.pathElementArray.length == 1 || !this.hasNext()) { + return this.pathElementArray[this.currentIndex]; + } + this.stringBuilder.clear(); for (int i = this.currentIndex; i < this.pathElementArray.length; i++) { if (i != this.currentIndex) { - sb.append(ELEMENT_SEPARATOR); + stringBuilder.append(ELEMENT_SEPARATOR); } - sb.append(this.pathElementArray[i]); + stringBuilder.append(this.pathElementArray[i]); } - return sb.toString(); + return stringBuilder.toString(); } public String getCurrentPathElement() { @@ -77,4 +78,17 @@ public void remove() { throw new UnsupportedOperationException(); } + + public void restoreState() { + if (this.stack == null) { + this.stack = FastList.newInstance(); + } + this.stack.addLast(this.currentIndex); + } + + public void saveState() { + if (this.stack != null && !this.stack.isEmpty()) { + this.currentIndex = this.stack.removeLast(); + } + } } Modified: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AuthorizationManagerImpl.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AuthorizationManagerImpl.java?rev=897594&r1=897593&r2=897594&view=diff ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AuthorizationManagerImpl.java (original) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/AuthorizationManagerImpl.java Sun Jan 10 07:15:50 2010 @@ -90,9 +90,10 @@ } protected static void setPermissions(String id, PathNode node, List<GenericValue> permissionValues) throws AuthorizationManagerException { + PermissionTreeBuilder builder = new PermissionTreeBuilder(); for (GenericValue value : permissionValues) { - String artifactPath = value.getString("artifactPath"); - OFBizPermission target = new OFBizPermission(id + "@" + artifactPath); + String artifactPathString = value.getString("artifactPath"); + OFBizPermission target = new OFBizPermission(id + "@" + artifactPathString); String[] pair = value.getString("permissionValue").split("="); if ("filter".equalsIgnoreCase(pair[0])) { target.addFilter(pair[1]); @@ -110,7 +111,7 @@ throw new AuthorizationManagerException("Invalid permission: " + pair[0]); } } - node.setPermissions(new ArtifactPath(artifactPath), target); + builder.buildPermissionTree(node, new ArtifactPath(artifactPathString), target); } } Modified: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNode.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNode.java?rev=897594&r1=897593&r2=897594&view=diff ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNode.java (original) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNode.java Sun Jan 10 07:15:50 2010 @@ -18,14 +18,9 @@ *******************************************************************************/ package org.ofbiz.context; -import java.util.Collection; import java.util.Map; -import javolution.util.FastList; -import javolution.util.FastMap; - -/** A node in a permissions tree. - */ +/** A node in a permissions tree. */ public abstract class PathNode { public static final String SUBSTITUTION_CHARACTER = "?"; @@ -49,50 +44,15 @@ this.nodeName = nodeName; } - protected void buildNodeString(FastList<PathNode> currentPath, StringBuilder result) { - if (this.childNodes != null) { - Collection<PathNode> childNodes = this.childNodes.values(); - for (PathNode childNode : childNodes) { - childNode.buildNodeString(currentPath, result); - } - } - } - - protected void getChildNodePermissions(String key, ArtifactPath artifactPath, OFBizPermission permission) { - if (this.childNodes != null) { - PathNode node = this.childNodes.get(key.toUpperCase()); - if (node != null) { - node.getPermissions(artifactPath, permission); - } - } - } - - public abstract void getPermissions(ArtifactPath artifactPath, OFBizPermission permission); - - protected void setChildNodePermissions(String key, ArtifactPath artifactPath, OFBizPermission permission) { - if (this.childNodes == null) { - this.childNodes = FastMap.newInstance(); - } - key = key.toUpperCase(); - PathNode node = this.childNodes.get(key); - if (node == null) { - node = PathNode.getInstance(artifactPath); - this.childNodes.put(key, node); - } - node.setPermissions(artifactPath, permission); - } - - public abstract void setPermissions(ArtifactPath artifactPath, OFBizPermission permission); + public abstract void accept(PathNodeVisitor visitor); @Override public String toString() { - FastList<PathNode> currentPath = FastList.newInstance(); - StringBuilder result = new StringBuilder(); - buildNodeString(currentPath, result); - return result.toString(); + TreeStringBuilder tsb = new TreeStringBuilder(this); + return tsb.toString(); } - protected static class BranchNode extends PathNode { + public static class BranchNode extends PathNode { protected OFBizPermission permission = null; protected SubstitutionNode substitutionNode = null; protected WildCardNode wildCardNode = null; @@ -102,132 +62,33 @@ } @Override - protected void buildNodeString(FastList<PathNode> currentPath, StringBuilder result) { - currentPath.add(this); - if (this.permission != null) { - for (PathNode pathNode: currentPath) { - result.append("/"); - result.append(pathNode.nodeName); - } - result.append("["); - result.append(this.permission); - result.append("]"); - result.append("\n"); - } - if (this.substitutionNode != null) { - this.substitutionNode.buildNodeString(currentPath, result); - } - if (this.wildCardNode != null) { - this.wildCardNode.buildNodeString(currentPath, result); - } - super.buildNodeString(currentPath, result); - currentPath.removeLast(); - } - - @Override - public void getPermissions(ArtifactPath artifactPath, OFBizPermission permission) { - permission.accumulatePermissions(this.permission); - if (artifactPath.hasNext()) { - String key = artifactPath.next(); - if (this.substitutionNode != null) { - this.substitutionNode.getPermissions(artifactPath.clone(), permission); - } - if (this.wildCardNode != null) { - this.wildCardNode.getPermissions(artifactPath.clone(), permission); - } - this.getChildNodePermissions(key, artifactPath, permission); - } - } - - @Override - public void setPermissions(ArtifactPath artifactPath, OFBizPermission permission) { - if (!artifactPath.hasNext()) { - if (this.permission == null) { - this.permission = permission; - } else { - this.permission.accumulatePermissions(permission); - } - return; - } - String key = artifactPath.next(); - if (SUBSTITUTION_CHARACTER.equals(key)) { - if (this.substitutionNode == null) { - this.substitutionNode = new SubstitutionNode(); - } - this.substitutionNode.setPermissions(artifactPath, permission); - return; - } - if (WILDCARD_CHARACTER.equals(key)) { - if (this.wildCardNode == null) { - this.wildCardNode = new WildCardNode(); - } - this.wildCardNode.setPermissions(artifactPath, permission); - return; - } - this.setChildNodePermissions(key, artifactPath, permission); + public void accept(PathNodeVisitor visitor) { + visitor.visit(this); } } - protected static class SubstitutionNode extends PathNode { + public static class SubstitutionNode extends PathNode { protected SubstitutionNode() { super(SUBSTITUTION_CHARACTER); } @Override - protected void buildNodeString(FastList<PathNode> currentPath, StringBuilder result) { - currentPath.add(this); - super.buildNodeString(currentPath, result); - currentPath.removeLast(); + public void accept(PathNodeVisitor visitor) { + visitor.visit(this); } - @Override - public void getPermissions(ArtifactPath artifactPath, OFBizPermission permission) { - if (artifactPath.hasNext()) { - this.getChildNodePermissions(artifactPath.next(), artifactPath, permission); - } - } - - @Override - public void setPermissions(ArtifactPath artifactPath, OFBizPermission permission) { - if (artifactPath.hasNext()) { - this.setChildNodePermissions(artifactPath.next(), artifactPath, permission); - } - } } - protected static class WildCardNode extends PathNode { + public static class WildCardNode extends PathNode { protected WildCardNode() { super(WILDCARD_CHARACTER); } @Override - protected void buildNodeString(FastList<PathNode> currentPath, StringBuilder result) { - currentPath.add(this); - super.buildNodeString(currentPath, result); - currentPath.removeLast(); - } - - public void getPermissions(ArtifactPath artifactPath, OFBizPermission permission) { - if (artifactPath.hasNext() && this.childNodes != null) { - artifactPath.next(); - String currentPath = artifactPath.getCurrentPath().toUpperCase(); - for (Map.Entry<String, PathNode> entry : this.childNodes.entrySet()) { - if (currentPath.endsWith(entry.getKey())) { - entry.getValue().getPermissions(artifactPath, permission); - return; - } - } - } - } - - @Override - public void setPermissions(ArtifactPath artifactPath, OFBizPermission permission) { - if (artifactPath.hasNext()) { - artifactPath.next(); - this.setChildNodePermissions(artifactPath.getCurrentPath(), artifactPath, permission); - } + public void accept(PathNodeVisitor visitor) { + visitor.visit(this); } } } Added: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java?rev=897594&view=auto ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java (added) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java Sun Jan 10 07:15:50 2010 @@ -0,0 +1,29 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.context; + +import org.ofbiz.context.PathNode.BranchNode; +import org.ofbiz.context.PathNode.SubstitutionNode; +import org.ofbiz.context.PathNode.WildCardNode; + +public interface PathNodeVisitor { + public void visit(BranchNode node); + public void visit(SubstitutionNode node); + public void visit(WildCardNode node); +} Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PathNodeVisitor.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java?rev=897594&view=auto ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java (added) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java Sun Jan 10 07:15:50 2010 @@ -0,0 +1,93 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.context; + +import javolution.util.FastMap; + +import org.ofbiz.context.PathNode.BranchNode; +import org.ofbiz.context.PathNode.SubstitutionNode; +import org.ofbiz.context.PathNode.WildCardNode; + +public class PermissionTreeBuilder implements PathNodeVisitor { + + protected ArtifactPath artifactPath; + protected OFBizPermission permission; + + public void buildPermissionTree(PathNode node, ArtifactPath artifactPath, OFBizPermission permission) { + this.artifactPath = artifactPath; + this.permission = permission; + node.accept(this); + } + + protected void setChildNodePermissions(PathNode node, String key) { + if (node.childNodes == null) { + node.childNodes = FastMap.newInstance(); + } + key = key.toUpperCase(); + PathNode childNode = node.childNodes.get(key); + if (childNode == null) { + childNode = PathNode.getInstance(this.artifactPath); + node.childNodes.put(key, childNode); + } + childNode.accept(this); + } + + @Override + public void visit(BranchNode node) { + if (!this.artifactPath.hasNext()) { + if (node.permission == null) { + node.permission = this.permission; + } else { + node.permission.accumulatePermissions(this.permission); + } + return; + } + String key = this.artifactPath.next(); + if (PathNode.SUBSTITUTION_CHARACTER.equals(key)) { + if (node.substitutionNode == null) { + node.substitutionNode = new SubstitutionNode(); + } + node.substitutionNode.accept(this); + return; + } + if (PathNode.WILDCARD_CHARACTER.equals(key)) { + if (node.wildCardNode == null) { + node.wildCardNode = new WildCardNode(); + } + node.wildCardNode.accept(this); + return; + } + this.setChildNodePermissions(node, key); + } + + @Override + public void visit(SubstitutionNode node) { + if (this.artifactPath.hasNext()) { + this.setChildNodePermissions(node, this.artifactPath.next()); + } + } + + @Override + public void visit(WildCardNode node) { + if (this.artifactPath.hasNext()) { + this.artifactPath.next(); + this.setChildNodePermissions(node, this.artifactPath.getCurrentPath()); + } + } +} Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionTreeBuilder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java?rev=897594&view=auto ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java (added) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java Sun Jan 10 07:15:50 2010 @@ -0,0 +1,91 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.context; + +import java.util.Map; + +import org.ofbiz.context.PathNode.BranchNode; +import org.ofbiz.context.PathNode.SubstitutionNode; +import org.ofbiz.context.PathNode.WildCardNode; + +public class PermissionsGatherer implements PathNodeVisitor { + protected ArtifactPath artifactPath; + protected final PathNode node; + protected OFBizPermission permission; + + public PermissionsGatherer(PathNode node) { + this.node = node; + } + + public void gatherPermissions(ArtifactPath artifactPath, OFBizPermission permission) { + this.artifactPath = artifactPath; + this.permission = permission; + this.node.accept(this); + } + + protected void getChildNodePermissions(PathNode node, String key) { + if (node.childNodes != null) { + PathNode childNode = node.childNodes.get(key.toUpperCase()); + if (childNode != null) { + childNode.accept(this); + } + } + } + + @Override + public void visit(BranchNode node) { + this.permission.accumulatePermissions(node.permission); + if (this.artifactPath.hasNext()) { + String key = this.artifactPath.next(); + if (node.substitutionNode != null) { + this.artifactPath.saveState(); + node.substitutionNode.accept(this); + this.artifactPath.restoreState(); + } + if (node.wildCardNode != null) { + this.artifactPath.saveState(); + node.wildCardNode.accept(this); + this.artifactPath.restoreState(); + } + this.getChildNodePermissions(node, key); + } + } + + @Override + public void visit(SubstitutionNode node) { + if (this.artifactPath.hasNext()) { + this.getChildNodePermissions(node, this.artifactPath.next()); + } + } + + @Override + public void visit(WildCardNode node) { + if (this.artifactPath.hasNext() && node.childNodes != null) { + this.artifactPath.next(); + String currentPath = this.artifactPath.getCurrentPath().toUpperCase(); + for (Map.Entry<String, PathNode> entry : node.childNodes.entrySet()) { + if (currentPath.endsWith(entry.getKey())) { + entry.getValue().accept(this); + return; + } + } + } + } + +} Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/PermissionsGatherer.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java URL: http://svn.apache.org/viewvc/ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java?rev=897594&view=auto ============================================================================== --- ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java (added) +++ ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java Sun Jan 10 07:15:50 2010 @@ -0,0 +1,91 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + *******************************************************************************/ +package org.ofbiz.context; + +import java.util.Collection; + +import javolution.text.TextBuilder; +import javolution.util.FastList; + +import org.ofbiz.context.PathNode.BranchNode; +import org.ofbiz.context.PathNode.SubstitutionNode; +import org.ofbiz.context.PathNode.WildCardNode; + +public class TreeStringBuilder implements PathNodeVisitor { + protected final FastList<PathNode> currentPath = FastList.newInstance(); + protected final PathNode node; + protected final TextBuilder stringBuilder = TextBuilder.newInstance(); + + public TreeStringBuilder(PathNode node) { + this.node = node; + } + + protected void buildNodeString(PathNode node) { + if (node.childNodes != null) { + Collection<PathNode> childNodes = node.childNodes.values(); + for (PathNode childNode : childNodes) { + childNode.accept(this); + } + } + } + + @Override + public String toString() { + this.node.accept(this); + return this.stringBuilder.toString(); + } + + @Override + public void visit(BranchNode node) { + this.currentPath.addLast(node); + if (node.permission != null) { + for (PathNode pathNode: this.currentPath) { + this.stringBuilder.append("/"); + this.stringBuilder.append(pathNode.nodeName); + } + this.stringBuilder.append("["); + this.stringBuilder.append(node.permission); + this.stringBuilder.append("]"); + this.stringBuilder.append("\n"); + } + if (node.substitutionNode != null) { + node.substitutionNode.accept(this); + } + if (node.wildCardNode != null) { + node.wildCardNode.accept(this); + } + this.buildNodeString(node); + this.currentPath.removeLast(); + } + + @Override + public void visit(SubstitutionNode node) { + this.currentPath.addLast(node); + this.buildNodeString(node); + this.currentPath.removeLast(); + } + + @Override + public void visit(WildCardNode node) { + this.currentPath.addLast(node); + this.buildNodeString(node); + this.currentPath.removeLast(); + } + +} Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java ------------------------------------------------------------------------------ svn:keywords = Date Rev Author URL Id Propchange: ofbiz/branches/executioncontext20091231/framework/context/src/org/ofbiz/context/TreeStringBuilder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
| Free forum by Nabble | Edit this page |
