KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > processor > tree > Sitemap


1 /*
2  * $Header: /home/cvs/jakarta-slide/projector/src/java/org/apache/slide/projector/processor/tree/Sitemap.java,v 1.3 2004/07/28 09:47:24 ib Exp $
3  * $Revision: 1.3 $
4  * $Date: 2004/07/28 09:47:24 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.projector.processor.tree;
25
26 import java.io.InputStream JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Stack JavaDoc;
32 import java.util.logging.Level JavaDoc;
33 import java.util.logging.Logger JavaDoc;
34
35 import org.apache.slide.projector.ConfigurableProcessor;
36 import org.apache.slide.projector.ConfigurationException;
37 import org.apache.slide.projector.Context;
38 import org.apache.slide.projector.Result;
39 import org.apache.slide.projector.descriptor.ParameterDescriptor;
40 import org.apache.slide.projector.descriptor.ResultDescriptor;
41 import org.apache.slide.projector.descriptor.ResultEntryDescriptor;
42 import org.apache.slide.projector.descriptor.StateDescriptor;
43 import org.apache.slide.projector.i18n.DefaultMessage;
44 import org.apache.slide.projector.processor.SimpleProcessor;
45 import org.apache.slide.projector.value.ArrayValue;
46 import org.apache.slide.projector.value.StreamableValue;
47 import org.apache.slide.projector.value.StringValue;
48 import org.apache.slide.projector.value.Value;
49 import org.xml.sax.InputSource JavaDoc;
50 import org.xml.sax.helpers.AttributesImpl JavaDoc;
51
52 import de.zeigermann.xml.simpleImporter.DefaultSimpleImportHandler;
53 import de.zeigermann.xml.simpleImporter.SimpleImporter;
54 import de.zeigermann.xml.simpleImporter.SimplePath;
55
56 /**
57  * The Sitemap class
58  *
59  */

60 public class Sitemap implements ConfigurableProcessor {
61     private static Logger JavaDoc logger = Logger.getLogger(Sitemap.class.getName());
62
63     private final static ParameterDescriptor[] parameterDescriptors = new ParameterDescriptor[0];
64     private static ResultDescriptor resultDescriptors;
65
66     private static Result result = new Result(StateDescriptor.OK);
67
68     public void configure(StreamableValue config) throws ConfigurationException {
69         try {
70             InputStream JavaDoc configuration = config.getInputStream();
71             SimpleImporter importer = new SimpleImporter();
72             ConfigurationHandler handler = new ConfigurationHandler();
73             importer.addSimpleImportHandler(handler);
74             importer.parse(new InputSource JavaDoc(configuration));
75             Page rootPage = handler.getRootPage();
76             List JavaDoc resources = new ArrayList JavaDoc();
77             buildArray(resources, rootPage);
78             Value[] array = new Value[resources.size()];
79             ArrayValue arrayResource = new ArrayValue((Value [])resources.toArray(array));
80             result.addResultEntry(SimpleProcessor.OUTPUT, arrayResource);
81             resultDescriptors = new ResultDescriptor(
82                     new StateDescriptor[] { StateDescriptor.OK_DESCRIPTOR },
83                     new ResultEntryDescriptor[] {
84                         new ResultEntryDescriptor(SimpleProcessor.OUTPUT, new DefaultMessage("sitemap/output"), ArrayValue.CONTENT_TYPE, false)
85                     });
86         } catch (Exception JavaDoc exception) {
87             logger.log(Level.SEVERE, "Error while parsing sitemap configuration", exception);
88         }
89     }
90
91     private void buildArray(List JavaDoc resources, Page page) {
92         for ( Iterator JavaDoc j = page.getChildren().iterator(); j.hasNext(); ) {
93             Page child = (Page)j.next();
94             if ( child.getId() != null ) {
95                 resources.add(new StringValue(child.getId()));
96             }
97             if ( child.getChildren().size() > 0 ) {
98                 List JavaDoc childrenResources = new ArrayList JavaDoc();
99                 buildArray(childrenResources, child);
100                 Value[] array = new Value[childrenResources.size()];
101                 ArrayValue arrayResource = new ArrayValue((Value [])childrenResources.toArray(array));
102                 resources.add(arrayResource);
103             }
104         }
105     }
106
107     public Result process(Map JavaDoc parameter, Context context) throws Exception JavaDoc {
108         return result;
109     }
110
111     public ParameterDescriptor[] getParameterDescriptors() {
112         return parameterDescriptors;
113     }
114
115     public ResultDescriptor getResultDescriptor() {
116         return resultDescriptors;
117     }
118
119     class Page {
120         private List JavaDoc children = new ArrayList JavaDoc();
121         private String JavaDoc id;
122
123         public Page(String JavaDoc id) {
124             this.id = id;
125         }
126
127         public String JavaDoc getId() {
128             return id;
129         }
130
131         public void addChild(Page page) {
132             children.add(page);
133         }
134
135         public List JavaDoc getChildren() {
136             return children;
137         }
138     }
139
140     class ConfigurationHandler extends DefaultSimpleImportHandler {
141         private Stack JavaDoc pageStack = new Stack JavaDoc();
142         private Page rootPage;
143
144         public Page getRootPage() {
145             return rootPage;
146         }
147
148         public void startElement(SimplePath path, String JavaDoc name, AttributesImpl JavaDoc attributes, String JavaDoc leadingCDdata) {
149             if ( path.matches("page") ) {
150                 String JavaDoc id = attributes.getValue(org.apache.slide.projector.processor.tree.TreeRenderer.ID);
151                 Page page = new Page(id);
152                 Page parentPage = (Page)pageStack.peek();
153                 parentPage.addChild(page);
154                 pageStack.push(page);
155             } else if ( path.matches("sitemap" ) ) {
156                 Page page = new Page(null);
157                 pageStack.push(page);
158                 rootPage = page;
159             }
160         }
161
162         public void endElement(SimplePath path, String JavaDoc name) {
163             if ( path.matches("page") ) {
164                 pageStack.pop();
165             }
166         }
167     }
168 }
169
Popular Tags