KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > navigation > impl > DefaultNavigationBuilder


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.navigation.impl;
17
18 import org.outerx.daisy.x10Navigationspec.*;
19 import org.outerj.daisy.repository.*;
20 import org.outerj.daisy.navigation.NavigationVersionMode;
21 import org.apache.xmlbeans.XmlObject;
22 import org.apache.xmlbeans.SchemaType;
23
24 import java.util.Stack JavaDoc;
25
26 public class DefaultNavigationBuilder implements NavigationFactory.NavigationBuilder {
27     private CommonNavigationManager.Context context;
28     private String JavaDoc[] collectionNames;
29     private long branchId;
30     private long languageId;
31     private NavigationVersionMode versionMode;
32
33     /**
34      * Builds a navigation tree from an XML navigation description provided as a string argument.
35      */

36     public Node build(XmlObject xmlObject, long branchId, long languageId, NavigationVersionMode versionMode,
37             CommonNavigationManager.Context context, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
38         this.context = context;
39         this.branchId = branchId;
40         this.languageId = languageId;
41         this.versionMode = versionMode;
42         return build((NavigationTreeDocument)xmlObject, counter, importStack);
43     }
44
45     public SchemaType getSchemaType() {
46         return NavigationTreeDocument.type;
47     }
48
49     private Node build(NavigationTreeDocument navTreeXml, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
50         if (navTreeXml.getNavigationTree().isSetCollections()) {
51             NavigationTreeDocument.NavigationTree.Collections.Collection[] collections = navTreeXml.getNavigationTree().getCollections().getCollectionArray();
52             collectionNames = new String JavaDoc[collections.length];
53             for (int i = 0; i < collections.length; i++) {
54                 NavigationTreeDocument.NavigationTree.Collections.Collection collection = collections[i];
55                 collectionNames[i] = collection.getName();
56             }
57         }
58
59         AggregateNode rootNode = new AggregateNode();
60
61         XmlObject[] nodesXml = navTreeXml.getNavigationTree().selectPath("*");
62         for (int i = 0; i < nodesXml.length; i++) {
63             // process all children except the collections element
64
if (!(nodesXml[i] instanceof NavigationTreeDocument.NavigationTree.Collections)) {
65                 Node childNode = build(nodesXml[i], counter, importStack);
66                 rootNode.add(childNode);
67             }
68         }
69
70         return rootNode;
71     }
72
73     private DocumentNode build(DocDocument.Doc nodeXml, Stack JavaDoc importStack) throws RepositoryException {
74         long branchId = this.branchId;
75         long languageId = this.languageId;
76         if (nodeXml.isSetBranch())
77             branchId = context.getBranchId(nodeXml.getBranch());
78         if (nodeXml.isSetLanguage())
79             languageId = context.getLanguageId(nodeXml.getLanguage());
80         NodeVisibility nodeVisibility = nodeXml.isSetVisibility() ? NodeVisibility.fromString(nodeXml.getVisibility()) : NodeVisibility.ALWAYS;
81         VariantKey variantKey = new VariantKey(nodeXml.getId(), branchId, languageId);
82         DocumentNode node = new DocumentNode(variantKey, nodeXml.getNodeId(), nodeXml.getLabel(), nodeVisibility, context,
83                 this.branchId, this.languageId, versionMode);
84
85         Counter counter = new Counter();
86         XmlObject[] nodesXml = nodeXml.selectPath("*");
87         for (int i = 0; i < nodesXml.length; i++) {
88             Node childNode = build(nodesXml[i], counter, importStack);
89             node.add(childNode);
90         }
91
92         return node;
93     }
94
95     private QueryNode build(QueryDocument.Query nodeXml) {
96         String JavaDoc select = nodeXml.getQ();
97
98         StringBuffer JavaDoc extraCond = null;
99         if (collectionNames != null) {
100             extraCond = new StringBuffer JavaDoc();
101             extraCond.append("InCollection(");
102             for (int i = 0; i < collectionNames.length; i++) {
103                 extraCond.append("'").append(collectionNames[i]).append("'");
104                 if (i < collectionNames.length - 1)
105                     extraCond.append(",");
106             }
107             extraCond.append(")");
108         }
109         if (!nodeXml.isSetFilterVariants() /* missing attribute means true */ || nodeXml.getFilterVariants()) {
110             if (extraCond == null)
111                 extraCond = new StringBuffer JavaDoc();
112             else
113                 extraCond.append(" and ");
114             extraCond.append(" branchId = ").append(branchId).append(" and languageId = ").append(languageId);
115         }
116
117         NodeVisibility nodeVisibility = nodeXml.isSetVisibility() ? NodeVisibility.fromString(nodeXml.getVisibility()) : NodeVisibility.ALWAYS;
118         return new QueryNode(select, extraCond != null ? extraCond.toString() : null, nodeVisibility, context, branchId, languageId, versionMode);
119     }
120
121     private GroupNode build(GroupDocument.Group nodeXml, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
122         String JavaDoc label = nodeXml.getLabel();
123         String JavaDoc id = NavigationUtil.makeNodeIdValid(nodeXml.getId());
124         if (id == null)
125             id = "g" + counter.augment();
126         NodeVisibility nodeVisibility = nodeXml.isSetVisibility() ? NodeVisibility.fromString(nodeXml.getVisibility()) : NodeVisibility.ALWAYS;
127         GroupNode node = new GroupNode(id, label, nodeVisibility);
128
129         Counter childCounter = new Counter();
130         XmlObject[] nodesXml = nodeXml.selectPath("*");
131         for (int i = 0; i < nodesXml.length; i++) {
132             Node childNode = build(nodesXml[i], childCounter, importStack);
133             node.add(childNode);
134         }
135
136         return node;
137     }
138
139     private Node build(ImportDocument.Import nodeXml, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
140         long branchId = this.branchId;
141         long languageId = this.languageId;
142         if (nodeXml.isSetBranch())
143             branchId = context.getBranchId(nodeXml.getBranch());
144         if (nodeXml.isSetLanguage())
145             languageId = context.getLanguageId(nodeXml.getLanguage());
146         long navDocId = nodeXml.getDocId();
147
148         VariantKey navigationDoc = new VariantKey(navDocId, branchId, languageId);
149         if (importStack.contains(navigationDoc))
150             return new ErrorNode("(error: recursive import of " + navigationDoc.getDocumentId() + ")");
151         else {
152             importStack.push(navigationDoc);
153             try {
154                 return NavigationFactory.build(navigationDoc, versionMode, context, counter, importStack);
155             } catch (Throwable JavaDoc e) {
156                 context.getLogger().error("Error building navtree in " + navigationDoc.getDocumentId() + " (specified in an import node).", e);
157                 return new ErrorNode("(failed import of " + navigationDoc.getDocumentId() + ")");
158             } finally {
159                 importStack.pop();
160             }
161         }
162     }
163
164     private LinkNode build(LinkDocument.Link nodeXml, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
165         String JavaDoc id = NavigationUtil.makeNodeIdValid(nodeXml.getId());
166         if (id == null)
167             id = "l" + counter.augment();
168         LinkNode node = new LinkNode(id, nodeXml.getUrl(), nodeXml.getLabel());
169
170         Counter childCounter = new Counter();
171         XmlObject[] nodesXml = nodeXml.selectPath("*");
172         for (int i = 0; i < nodesXml.length; i++) {
173             Node childNode = build(nodesXml[i], childCounter, importStack);
174             node.add(childNode);
175         }
176
177         return node;
178     }
179
180     private Node build(XmlObject object, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
181         Node node;
182
183         if (object instanceof DocDocument.Doc)
184             node = build((DocDocument.Doc)object, importStack);
185         else if (object instanceof QueryDocument.Query)
186             node = build((QueryDocument.Query)object);
187         else if (object instanceof GroupDocument.Group)
188             node = build((GroupDocument.Group)object, counter, importStack);
189         else if (object instanceof ImportDocument.Import)
190             node = build((ImportDocument.Import)object, counter, importStack);
191         else if (object instanceof LinkDocument.Link)
192             node = build((LinkDocument.Link)object, counter, importStack);
193         else
194             throw new RuntimeException JavaDoc("Unexpected type: " + object.getClass().getName());
195
196         return node;
197     }
198
199 }
200
Popular Tags