1 16 package org.outerj.daisy.publisher.serverimpl.docpreparation; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.Locator ; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.helpers.AttributesImpl ; 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 ; 33 34 37 public class DaisyLinkEnhancerHandler implements ContentHandler { 38 private ContentHandler 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 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 namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 65 boolean forwardStartElement = true; 66 if (namespaceURI.equals("") && (localName.equals("a") || localName.equals("img"))) { 67 String 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 matcher = Constants.DAISY_LINK_PATTERN.matcher(href); 77 if (matcher.matches()) { 78 String 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 branch = matcher.group(3); 86 if (branch == null || branch.equals("")) 87 branch = String.valueOf(documentBranchId); 88 String 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 e) { 93 } 95 96 if (document != null) { 97 String 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 e) { 115 } 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 newAttrs = new AttributesImpl (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 linkInfoAttrs = new AttributesImpl (atts); 142 linkInfoAttrs.addAttribute("", "documentName", "documentName", "CDATA", version.getDocumentName()); 143 String 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 partInfoAttrs = new AttributesImpl (); 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 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 attrs, Version version) { 173 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 e) { 179 imageWidthFieldId = -2; 181 imageHeightFieldId = -2; 182 } 183 } 184 185 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 { 195 consumer.endDocument(); 196 } 197 198 public void startDocument () throws SAXException { 199 consumer.startDocument(); 200 } 201 202 public void characters (char ch[], int start, int length) throws SAXException { 203 consumer.characters(ch, start, length); 204 } 205 206 public void ignorableWhitespace (char ch[], int start, int length) throws SAXException { 207 consumer.ignorableWhitespace(ch, start, length); 208 } 209 210 public void endPrefixMapping (String prefix) throws SAXException { 211 consumer.endPrefixMapping(prefix); 212 } 213 214 public void skippedEntity (String name) throws SAXException { 215 consumer.skippedEntity(name); 216 } 217 218 public void setDocumentLocator (Locator locator) { 219 consumer.setDocumentLocator(locator); 220 } 221 222 public void processingInstruction (String target, String data) throws SAXException { 223 consumer.processingInstruction(target, data); 224 } 225 226 public void startPrefixMapping (String prefix, String uri) throws SAXException { 227 consumer.startPrefixMapping(prefix, uri); 228 } 229 230 public void endElement (String namespaceURI, String localName, String qName) throws SAXException { 231 consumer.endElement(namespaceURI, localName, qName); 232 } 233 234 } 235 | Popular Tags |