KickJava   Java API By Example, From Geeks To Geeks.

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


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.outerj.daisy.repository.*;
19 import org.outerj.daisy.navigation.NavigationVersionMode;
20 import org.outerx.daisy.x10Bookdef.BookDocument;
21 import org.outerx.daisy.x10Bookdef.SectionDocument;
22 import org.outerx.daisy.x10Bookdef.QueryDocument;
23 import org.outerx.daisy.x10Bookdef.ImportNavigationTreeDocument;
24 import org.apache.xmlbeans.XmlObject;
25 import org.apache.xmlbeans.SchemaType;
26
27 import java.util.Stack JavaDoc;
28
29 public class BookNavigationBuilder implements NavigationFactory.NavigationBuilder {
30     private CommonNavigationManager.Context context;
31     private long branchId;
32     private long languageId;
33     private NavigationVersionMode versionMode;
34
35     public Node build(XmlObject xmlObject, long branchId, long languageId, NavigationVersionMode versionMode,
36             CommonNavigationManager.Context context, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
37         this.branchId = branchId;
38         this.languageId = languageId;
39         this.context = context;
40         this.versionMode = versionMode;
41         return build(((BookDocument)xmlObject).getBook(), counter, importStack);
42     }
43
44     public SchemaType getSchemaType() {
45         return BookDocument.type;
46     }
47
48     private Node build(BookDocument.Book bookXml, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
49         AggregateNode rootNode = new AggregateNode();
50
51         XmlObject[] nodesXml = bookXml.getContent().selectPath("*");
52         for (int i = 0; i < nodesXml.length; i++) {
53             Node childNode = build(nodesXml[i], counter, importStack);
54             rootNode.add(childNode);
55         }
56
57         return rootNode;
58     }
59
60     private Node build(SectionDocument.Section nodeXml, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
61         AbstractParentNode node;
62         if (nodeXml.isSetDocumentId()) {
63             long branchId = this.branchId;
64             long languageId = this.languageId;
65             if (nodeXml.isSetBranch())
66                 branchId = context.getBranchId(nodeXml.getBranch());
67             if (nodeXml.isSetLanguage())
68                 languageId = context.getLanguageId(nodeXml.getLanguage());
69             VariantKey variantKey = new VariantKey(nodeXml.getDocumentId(), branchId, languageId);
70             node = new DocumentNode(variantKey, null, null, NodeVisibility.ALWAYS, context, this.branchId, this.languageId, versionMode);
71         } else {
72             String JavaDoc id = "g" + counter.augment();
73             node = new GroupNode(id, nodeXml.getTitle(), NodeVisibility.ALWAYS);
74         }
75
76         Counter childCounter = new Counter();
77         XmlObject[] nodesXml = nodeXml.selectPath("*");
78         for (int i = 0; i < nodesXml.length; i++) {
79             Node childNode = build(nodesXml[i], childCounter, importStack);
80             node.add(childNode);
81         }
82
83         return node;
84     }
85
86     private QueryNode build(QueryDocument.Query nodeXml) {
87         String JavaDoc select = nodeXml.getQ();
88
89         StringBuffer JavaDoc extraCond = null;
90         if (!nodeXml.isSetFilterVariants() /* missing attribute means true */ || nodeXml.getFilterVariants()) {
91             extraCond = new StringBuffer JavaDoc();
92             extraCond.append(" branchId = ").append(branchId).append(" and languageId = ").append(languageId);
93         }
94
95         return new QueryNode(select, extraCond != null ? extraCond.toString() : null, NodeVisibility.ALWAYS, context, branchId, languageId, versionMode);
96     }
97
98     private Node build(ImportNavigationTreeDocument.ImportNavigationTree nodeXml, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
99         long documentId = nodeXml.getId();
100         long branchId = this.branchId;
101         long languageId = this.languageId;
102         if (nodeXml.isSetBranch())
103             branchId = context.getBranchId(nodeXml.getBranch());
104         if (nodeXml.isSetLanguage())
105             languageId = context.getLanguageId(nodeXml.getLanguage());
106
107         VariantKey navigationDoc = new VariantKey(documentId, branchId, languageId);
108         if (importStack.contains(navigationDoc))
109             return new ErrorNode("(error: recursive import of " + navigationDoc.getDocumentId() + ")");
110         else {
111             importStack.push(navigationDoc);
112             try {
113                 return NavigationFactory.build(new VariantKey(documentId, branchId, languageId), versionMode, context, counter, importStack);
114             } finally {
115                 importStack.pop();
116             }
117         }
118     }
119
120     private Node build(XmlObject object, Counter counter, Stack JavaDoc importStack) throws RepositoryException {
121         Node node;
122
123         if (object instanceof SectionDocument.Section)
124             node = build((SectionDocument.Section)object, counter, importStack);
125         else if (object instanceof QueryDocument.Query)
126             node = build((QueryDocument.Query)object);
127         else if (object instanceof ImportNavigationTreeDocument.ImportNavigationTree)
128             node = build((ImportNavigationTreeDocument.ImportNavigationTree)object, counter, importStack);
129         else
130             throw new RuntimeException JavaDoc("Unexpected type: " + object.getClass().getName());
131
132         return node;
133     }
134 }
135
Popular Tags