KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > books > frontend > BookGroup


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.books.frontend;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.apache.cocoon.xml.AttributesImpl;
21
22 import java.util.List JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Collections JavaDoc;
26
27 class BookGroup implements Comparable JavaDoc {
28     private final String JavaDoc name;
29     private List JavaDoc childGroups = new ArrayList JavaDoc();
30     private List JavaDoc childNodes = new ArrayList JavaDoc();
31
32     public BookGroup(String JavaDoc name) {
33         this.name = name;
34     }
35
36     public BookGroup getGroup(String JavaDoc path) {
37         path = path.trim();
38
39         // removing leading slash if any, also handles case where user enters stupid things '///'
40
while (path.length() > 0 && path.charAt(0) == '/') {
41             path = path.substring(1);
42         }
43
44         if (path.length() == 0) {
45             return this;
46         }
47
48         String JavaDoc name;
49         String JavaDoc subpath = null;
50
51         int slashPos = path.indexOf('/');
52         if (slashPos != -1) {
53             name = path.substring(0, slashPos);
54             subpath = path.substring(slashPos + 1);
55         } else {
56             name = path;
57         }
58
59         BookGroup childGroup = getChild(name);
60         if (childGroup == null) {
61             childGroup = new BookGroup(name);
62             childGroups.add(childGroup);
63         }
64
65         if (subpath != null) {
66             return childGroup.getGroup(subpath);
67         } else {
68             return childGroup;
69         }
70     }
71
72     private BookGroup getChild(String JavaDoc name) {
73         Iterator JavaDoc childGroupsIt = childGroups.iterator();
74         while (childGroupsIt.hasNext()) {
75             BookGroup group = (BookGroup)childGroupsIt.next();
76             if (group.name.equals(name))
77                 return group;
78         }
79         return null;
80     }
81
82     public void addChild(BookGroupChild child) {
83         childNodes.add(child);
84     }
85
86     public int compareTo(Object JavaDoc o) {
87         BookGroup otherGroup = (BookGroup)o;
88         return name.compareTo(otherGroup.name);
89     }
90
91     public void generateSaxFragment(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc {
92         AttributesImpl groupAttrs = new AttributesImpl();
93         groupAttrs.addAttribute("", "name", "name", "CDATA", name);
94         contentHandler.startElement("", "group", "group", groupAttrs);
95
96         Collections.sort(childGroups);
97         Collections.sort(childNodes);
98
99         Iterator JavaDoc childGroupsIt = childGroups.iterator();
100         while (childGroupsIt.hasNext()) {
101             ((BookGroup)childGroupsIt.next()).generateSaxFragment(contentHandler);
102         }
103
104         Iterator JavaDoc childNodesIt = childNodes.iterator();
105         while (childNodesIt.hasNext()) {
106             ((BookGroupChild)childNodesIt.next()).generateSaxFragment(contentHandler);
107         }
108
109         contentHandler.endElement("", "group", "group");
110     }
111
112     interface BookGroupChild extends Comparable JavaDoc {
113         void generateSaxFragment(ContentHandler JavaDoc contentHandler) throws SAXException JavaDoc;
114     }
115 }
116
Popular Tags