KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > BrowseIdsApple


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.frontend;
17
18 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
19 import org.outerj.daisy.repository.Repository;
20 import org.outerj.daisy.repository.Document;
21 import org.outerj.daisy.repository.Version;
22 import org.outerj.daisy.repository.Part;
23 import org.outerj.daisy.repository.schema.RepositorySchema;
24 import org.outerj.daisy.repository.variant.Branch;
25 import org.outerj.daisy.repository.variant.Language;
26 import org.apache.cocoon.components.flow.apples.StatelessAppleController;
27 import org.apache.cocoon.components.flow.apples.AppleRequest;
28 import org.apache.cocoon.components.flow.apples.AppleResponse;
29 import org.apache.cocoon.environment.Request;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.avalon.framework.service.ServiceException;
33 import org.apache.avalon.framework.logger.LogEnabled;
34 import org.apache.avalon.framework.logger.Logger;
35 import org.apache.excalibur.xml.sax.SAXParser;
36 import org.xml.sax.helpers.DefaultHandler JavaDoc;
37 import org.xml.sax.Attributes JavaDoc;
38 import org.xml.sax.SAXException JavaDoc;
39 import org.xml.sax.InputSource JavaDoc;
40
41 import java.util.HashSet JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Arrays JavaDoc;
45 import java.io.InputStream JavaDoc;
46
47 /**
48  * Apple to let the user select from the IDs in a document, this is called from the wysiwyg editor.
49  */

50 public class BrowseIdsApple extends AbstractDaisyApple implements StatelessAppleController, Serviceable, LogEnabled {
51     private ServiceManager serviceManager;
52     private Logger logger;
53
54     public void service(ServiceManager serviceManager) throws ServiceException {
55         this.serviceManager = serviceManager;
56     }
57
58     public void enableLogging(Logger logger) {
59         this.logger = logger;
60     }
61
62     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
63         Request request = appleRequest.getCocoonRequest();
64         Repository repository = WikiHelper.getRepository(request, serviceManager);
65         long documentId = RequestUtil.getLongParameter(request, "documentId");
66         long branchId = RequestUtil.getBranchId(request, Branch.MAIN_BRANCH_ID, repository);
67         long languageId = RequestUtil.getLanguageId(request, Language.DEFAULT_LANGUAGE_ID, repository);
68         String JavaDoc versionParam = request.getParameter("version");
69
70         Document document = repository.getDocument(documentId, branchId, languageId, false);
71         Version version;
72
73         if (versionParam == null)
74             versionParam = WikiHelper.getVersionMode(request).toString();
75
76         if (versionParam.equalsIgnoreCase("live")) {
77             version = document.getLiveVersion();
78             if (version == null)
79                 version = document.getLastVersion();
80         } else if (versionParam.equalsIgnoreCase("last")) {
81             version = document.getLastVersion();
82         } else {
83             long versionId = 0;
84             try {
85                 versionId = Long.parseLong(versionParam);
86             } catch (NumberFormatException JavaDoc e) {
87                 throw new Exception JavaDoc("Invalid version ID: " + versionId);
88             }
89             version = document.getVersion(versionId);
90         }
91
92         String JavaDoc[] ids = extractIds(version, repository);
93         Arrays.sort(ids);
94
95         Map JavaDoc viewData = new HashMap JavaDoc();
96         viewData.put("ids", ids);
97         viewData.put("documentId", String.valueOf(documentId));
98         viewData.put("branchId", String.valueOf(branchId));
99         viewData.put("languageId", String.valueOf(languageId));
100         viewData.put("documentName", version.getDocumentName());
101         viewData.put("version", versionParam);
102         appleResponse.sendPage("Template-select_id-Pipe", viewData);
103     }
104
105     private String JavaDoc[] extractIds(Version version, Repository repository) throws Exception JavaDoc {
106         SAXParser parser = null;
107         InputStream JavaDoc is = null;
108         try {
109             parser = (SAXParser)serviceManager.lookup(SAXParser.ROLE);
110             HashSet JavaDoc ids = new HashSet JavaDoc();
111             Part[] parts = version.getParts().getArray();
112             RepositorySchema schema = repository.getRepositorySchema();
113             for (int i = 0; i < parts.length; i++) {
114                 Part part = parts[i];
115                 if (part.getMimeType().equals("text/xml") && schema.getPartTypeById(part.getTypeId(), false).isDaisyHtml()) {
116                     is = part.getDataStream();
117                     parser.parse(new InputSource JavaDoc(is), new IdCollector(ids));
118                     is.close();
119                 }
120             }
121             return (String JavaDoc[])ids.toArray(new String JavaDoc[ids.size()]);
122         } finally {
123             if (is != null)
124                 try { is.close(); } catch (Throwable JavaDoc e) {logger.error("Error closing part input stream.", e);}
125             if (parser != null)
126                 serviceManager.release(parser);
127         }
128     }
129
130     private static class IdCollector extends DefaultHandler JavaDoc {
131         private final HashSet JavaDoc ids;
132
133         public IdCollector(HashSet JavaDoc ids) {
134             this.ids = ids;
135         }
136
137         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
138             String JavaDoc id = attributes.getValue("id");
139             if (id != null)
140                 ids.add(id);
141         }
142     }
143 }
144
Popular Tags