KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > serverimpl > docpreparation > DaisyLinkEnhancerHandler


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.publisher.serverimpl.docpreparation;
17
18 import org.xml.sax.ContentHandler JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.Locator JavaDoc;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.helpers.AttributesImpl JavaDoc;
23 import org.outerj.daisy.repository.*;
24 import org.outerj.daisy.navigation.NavigationManager;
25 import org.outerj.daisy.navigation.NavigationVersionMode;
26 import org.outerj.daisy.util.Constants;
27 import org.outerj.daisy.publisher.serverimpl.PublisherImpl;
28 import org.outerj.daisy.publisher.serverimpl.requestmodel.PublisherVersionMode;
29 import org.outerj.daisy.xmlutil.SaxBuffer;
30 import org.apache.avalon.framework.logger.Logger;
31
32 import java.util.regex.Matcher JavaDoc;
33
34 /**
35  * Adds the document name as an attribute on links pointing to daisy documents.
36  */

37 public class DaisyLinkEnhancerHandler implements ContentHandler JavaDoc {
38     private ContentHandler JavaDoc consumer;
39     private Repository repository;
40     private VariantKey navigationDoc;
41     private PublisherVersionMode versionMode;
42     private NavigationVersionMode navVersionMode;
43     private NavigationManager navigationManager;
44     private Logger logger;
45     private long documentBranchId;
46     private long documentLanguageId;
47     private long imageWidthFieldId = -1;
48     private long imageHeightFieldId = -1;
49
50     public DaisyLinkEnhancerHandler(Repository repository, long documentBranchId,
51             long documentLanguageId, VariantKey navigationDoc, PublisherVersionMode versionMode,
52             ContentHandler JavaDoc consumer, Logger logger) {
53         this.repository = repository;
54         this.consumer = consumer;
55         this.navigationDoc = navigationDoc;
56         this.versionMode = versionMode;
57         this.navVersionMode = versionMode == PublisherVersionMode.LAST ? NavigationVersionMode.LAST : NavigationVersionMode.LIVE;
58         this.navigationManager = (NavigationManager)repository.getExtension("NavigationManager");
59         this.logger = logger;
60         this.documentBranchId = documentBranchId;
61         this.documentLanguageId = documentLanguageId;
62     }
63
64     public void startElement (String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
65         boolean forwardStartElement = true;
66         if (namespaceURI.equals("") && (localName.equals("a") || localName.equals("img"))) {
67             String JavaDoc href;
68             boolean isLink = false;
69             if (localName.equals("a")) {
70                 href = atts.getValue("href");
71                 isLink = true;
72             } else {
73                 href = atts.getValue("src");
74             }
75             if (href != null) {
76                 Matcher JavaDoc matcher = Constants.DAISY_LINK_PATTERN.matcher(href);
77                 if (matcher.matches()) {
78                     String JavaDoc navigationPath = null;
79                     Version version = null;
80
81                     Document document = null;
82                     long documentId;
83                     try {
84                         documentId = Long.parseLong(matcher.group(1));
85                         String JavaDoc branch = matcher.group(3);
86                         if (branch == null || branch.equals(""))
87                             branch = String.valueOf(documentBranchId);
88                         String JavaDoc language = matcher.group(5);
89                         if (language == null || language.equals(""))
90                             language = String.valueOf(documentLanguageId);
91                         document = repository.getDocument(documentId, branch, language, false);
92                     } catch (Exception JavaDoc e) {
93                         // ignore (invalid document id, non existing document, no permission, ...)
94
}
95
96                     if (document != null) {
97                         String JavaDoc versionSpec = matcher.group(7);
98                         try {
99                             if (versionSpec != null) {
100                                 if (versionSpec.equalsIgnoreCase("last")) {
101                                     version = document.getLastVersion();
102                                 } else if (versionSpec.equalsIgnoreCase("live")) {
103                                     version = document.getLiveVersion();
104                                 } else {
105                                     version = document.getVersion(Long.parseLong(versionSpec));
106                                 }
107                             } else {
108                                 if (versionMode == PublisherVersionMode.LAST)
109                                     version = document.getLastVersion();
110                                 else
111                                     version = document.getLiveVersion();
112
113                             }
114                         } catch (Exception JavaDoc e) {
115                             // ignore exceptions that happen when retrieving version
116
}
117                     }
118
119                     if (isLink && navigationDoc != null && document != null) {
120                         try {
121                             navigationPath = navigationManager.reverseLookup(document.getVariantKey(), navigationDoc, navVersionMode);
122                         } catch (RepositoryException e) {
123                             logger.error("Error performing reverse document lookup for navigation tree " + navigationDoc + " and document " + document.getVariantKey(), e);
124                         }
125                     }
126
127                     if (version != null) {
128                         AttributesImpl JavaDoc newAttrs = new AttributesImpl JavaDoc(atts);
129                         if (navigationPath != null)
130                             newAttrs.addAttribute(PublisherImpl.NAMESPACE, "navigationPath", "p:navigationPath", "CDATA", navigationPath);
131
132                         if (localName.equals("img")) {
133                             addImageAttributes(newAttrs, version);
134                         }
135
136                         consumer.startElement(namespaceURI, localName, qName, newAttrs);
137                         forwardStartElement = false;
138
139                         SaxBuffer linkInfoBuffer = new SaxBuffer();
140                         try {
141                             AttributesImpl JavaDoc linkInfoAttrs = new AttributesImpl JavaDoc(atts);
142                             linkInfoAttrs.addAttribute("", "documentName", "documentName", "CDATA", version.getDocumentName());
143                             String JavaDoc documentTypeName = repository.getRepositorySchema().getDocumentTypeById(document.getDocumentTypeId(), false).getName();
144                             linkInfoAttrs.addAttribute("", "documentType", "documentType", "CDATA", documentTypeName);
145
146                             linkInfoBuffer.startElement(PublisherImpl.NAMESPACE, "linkInfo", "p:linkInfo", linkInfoAttrs);
147                             Part[] parts = version.getParts().getArray();
148                             for (int i = 0; i < parts.length; i++) {
149                                 AttributesImpl JavaDoc partInfoAttrs = new AttributesImpl JavaDoc();
150                                 partInfoAttrs.addAttribute("", "id", "id", "CDATA", String.valueOf(parts[i].getTypeId()));
151                                 partInfoAttrs.addAttribute("", "name", "name", "CDATA", parts[i].getTypeName());
152                                 if (parts[i].getFileName() != null)
153                                     partInfoAttrs.addAttribute("", "fileName", "fileName", "CDATA", parts[i].getFileName());
154                                 linkInfoBuffer.startElement(PublisherImpl.NAMESPACE, "linkPartInfo", "p:linkPartInfo", partInfoAttrs);
155                                 linkInfoBuffer.endElement(PublisherImpl.NAMESPACE, "linkPartInfo", "p:linkPartInfo");
156                             }
157                             linkInfoBuffer.endElement(PublisherImpl.NAMESPACE, "linkInfo", "p:linkInfo");
158                         } catch (Throwable JavaDoc e) {
159                             linkInfoBuffer = null;
160                         }
161                         if (linkInfoBuffer != null)
162                             linkInfoBuffer.toSAX(consumer);
163                     }
164                 }
165             }
166         }
167
168         if (forwardStartElement)
169             consumer.startElement(namespaceURI, localName, qName, atts);
170     }
171
172     private void addImageAttributes(AttributesImpl JavaDoc attrs, Version version) {
173         // If the ID and existence of the ImageWidth and ImageHeight fields has not yet been determined
174
if (imageWidthFieldId == -1 || imageHeightFieldId == -1) {
175             try {
176                 imageWidthFieldId = repository.getRepositorySchema().getFieldTypeByName("ImageWidth", false).getId();
177                 imageHeightFieldId = repository.getRepositorySchema().getFieldTypeByName("ImageHeight", false).getId();
178             } catch (Throwable JavaDoc e) {
179                 // ignore this error, sets fields to -2 to avoid trying the same for every image
180
imageWidthFieldId = -2;
181                 imageHeightFieldId = -2;
182             }
183         }
184
185         // If the ImageWidth and ImageHeight fields have been successfully found
186
if (imageWidthFieldId > 0 && imageHeightFieldId > 0) {
187             if (version.hasField(imageWidthFieldId))
188                 attrs.addAttribute(PublisherImpl.NAMESPACE, "imageWidth", "p:imageWidth", "CDATA", version.getField(imageWidthFieldId).getValue().toString());
189             if (version.hasField(imageHeightFieldId))
190                 attrs.addAttribute(PublisherImpl.NAMESPACE, "imageHeight", "p:imageHeight", "CDATA", version.getField(imageHeightFieldId).getValue().toString());
191         }
192     }
193
194     public void endDocument() throws SAXException JavaDoc {
195         consumer.endDocument();
196     }
197
198     public void startDocument () throws SAXException JavaDoc {
199         consumer.startDocument();
200     }
201
202     public void characters (char ch[], int start, int length) throws SAXException JavaDoc {
203         consumer.characters(ch, start, length);
204     }
205
206     public void ignorableWhitespace (char ch[], int start, int length) throws SAXException JavaDoc {
207         consumer.ignorableWhitespace(ch, start, length);
208     }
209
210     public void endPrefixMapping (String JavaDoc prefix) throws SAXException JavaDoc {
211         consumer.endPrefixMapping(prefix);
212     }
213
214     public void skippedEntity (String JavaDoc name) throws SAXException JavaDoc {
215         consumer.skippedEntity(name);
216     }
217
218     public void setDocumentLocator (Locator JavaDoc locator) {
219         consumer.setDocumentLocator(locator);
220     }
221
222     public void processingInstruction (String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
223         consumer.processingInstruction(target, data);
224     }
225
226     public void startPrefixMapping (String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
227         consumer.startPrefixMapping(prefix, uri);
228     }
229
230     public void endElement (String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
231         consumer.endElement(namespaceURI, localName, qName);
232     }
233
234 }
235
Popular Tags