KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > template > BasePathResultsMap


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.template;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.service.ServiceRegistry;
24 import org.alfresco.service.cmr.repository.NodeRef;
25 import org.alfresco.service.cmr.repository.TemplateNode;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * A special Map that executes an XPath against the parent Node as part of the get()
31  * Map interface implementation.
32  *
33  * @author Kevin Roast
34  */

35 public abstract class BasePathResultsMap extends BaseTemplateMap
36 {
37     protected static Log logger = LogFactory.getLog(BasePathResultsMap.class);
38     
39     /**
40      * Constructor
41      *
42      * @param parent The parent TemplateNode to execute searches from
43      * @param services The ServiceRegistry to use
44      */

45     public BasePathResultsMap(TemplateNode parent, ServiceRegistry services)
46     {
47         super(parent, services);
48     }
49     
50     /**
51      * Return a list or a single Node from executing an xpath against the parent Node.
52      *
53      * @param xpath XPath to execute
54      * @param firstOnly True to return the first result only
55      *
56      * @return List<TemplateNode>
57      */

58     protected List JavaDoc<TemplateNode> getChildrenByXPath(String JavaDoc xpath, boolean firstOnly)
59     {
60         List JavaDoc<TemplateNode> result = null;
61         
62         if (xpath.length() != 0)
63         {
64             if (logger.isDebugEnabled())
65                 logger.debug("Executing xpath: " + xpath);
66             
67             List JavaDoc<NodeRef> nodes = this.services.getSearchService().selectNodes(
68                     this.parent.getNodeRef(),
69                     xpath,
70                     null,
71                     this.services.getNamespaceService(),
72                     false);
73             
74             // see if we only want the first result
75
if (firstOnly == true)
76             {
77                 if (nodes.size() != 0)
78                 {
79                     result = new ArrayList JavaDoc<TemplateNode>(1);
80                     result.add(new TemplateNode(nodes.get(0), this.services, this.parent.getImageResolver()));
81                 }
82             }
83             // or all the results
84
else
85             {
86                 result = new ArrayList JavaDoc<TemplateNode>(nodes.size());
87                 for (NodeRef ref : nodes)
88                 {
89                     result.add(new TemplateNode(ref, this.services, this.parent.getImageResolver()));
90                 }
91             }
92         }
93         
94         return result != null ? result : (List JavaDoc)Collections.emptyList();
95     }
96 }
97
Popular Tags