1 17 18 19 20 package org.apache.lenya.cms.cocoon.transformation; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.apache.avalon.framework.parameters.ParameterException; 29 import org.apache.avalon.framework.parameters.Parameterizable; 30 import org.apache.avalon.framework.parameters.Parameters; 31 import org.apache.cocoon.ProcessingException; 32 import org.apache.cocoon.environment.SourceResolver; 33 import org.apache.cocoon.transformation.AbstractSAXTransformer; 34 import org.apache.lenya.cms.publication.Document; 35 import org.apache.lenya.cms.publication.DocumentBuildException; 36 import org.apache.lenya.cms.publication.DocumentBuilder; 37 import org.apache.lenya.cms.publication.DocumentException; 38 import org.apache.lenya.cms.publication.PageEnvelope; 39 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 40 import org.apache.lenya.cms.publication.Publication; 41 import org.apache.lenya.cms.publication.SiteTree; 42 import org.apache.lenya.cms.publication.SiteTreeNode; 43 import org.xml.sax.Attributes ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.helpers.AttributesImpl ; 46 47 60 public class DocumentIndexTransformer extends AbstractSAXTransformer implements Parameterizable { 61 62 private String namespace; 63 private String cIncludeNamespace; 64 65 public static final String CHILDREN_ELEMENT = "children"; 66 public static final String ABSTRACT_ATTRIBUTE = "abstract"; 67 68 public static final String NAMESPACE = "http://apache.org/cocoon/lenya/documentindex/1.0"; 69 public static final String PREFIX = "index:"; 70 71 74 public void parameterize(Parameters parameters) throws ParameterException { 75 this.namespace = parameters.getParameter("namespace", null); 76 this.cIncludeNamespace = parameters.getParameter("cIncludeNamespace", null); 77 } 78 79 private Document document; 80 81 private Publication publication; 82 83 private String area; 84 85 private DocumentBuilder builder; 86 87 private SiteTree siteTree; 88 89 92 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) 93 throws ProcessingException, SAXException , IOException { 94 try { 95 96 super.setup(resolver, objectModel, src, parameters); 97 98 parameterize(parameters); 99 100 PageEnvelope envelope = null; 101 envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 102 103 setDocument(envelope.getDocument()); 104 setPublication(document.getPublication()); 105 setArea(document.getArea()); 106 setBuilder(document.getPublication().getDocumentBuilder()); 107 setSiteTree(publication.getTree(area)); 108 109 } catch (Exception e) { 110 throw new ProcessingException(e); 111 } 112 113 } 114 115 118 public void startElement(String uri, String localName, String raw, Attributes attr) 119 throws SAXException { 120 121 if (uri != null 122 && uri.equals(namespace) 123 && cIncludeNamespace != null 124 && localName.equals(CHILDREN_ELEMENT)) { 125 126 if (getLogger().isInfoEnabled()) { 127 getLogger().info("Inserting index"); 128 } 129 130 String cIncludePrefix = ""; 131 if (!this.cIncludeNamespace.equals("")) { 132 cIncludePrefix = "ci:"; 133 } 134 135 String documentId = document.getId(); 136 String language = document.getLanguage(); 137 String defaultLanguage = publication.getDefaultLanguage(); 138 SiteTreeNode[] children = siteTree.getNode(documentId).getChildren(); 139 140 super.startElement(uri, localName, raw, attr); 141 142 for (int i = 0; i < children.length; i++) { 143 String childId = documentId + "/" + children[i].getId(); 144 145 String url = builder.buildCanonicalUrl(publication, area, childId, language); 147 Document doc; 148 try { 149 doc = builder.buildDocument(publication, url); 150 } catch (DocumentBuildException e) { 151 throw new SAXException (e); 152 } 153 File file = doc.getFile(); 154 155 if (!file.exists()) { 156 getLogger().debug( 158 "There is no child file " 159 + file.getAbsolutePath() 160 + " in the same language as the parent document [" 161 + language 162 + "]"); 163 164 String [] availableLanguages = null; 166 try { 167 availableLanguages = doc.getLanguages(); 168 } catch (DocumentException e) { 169 throw new SAXException (e); 170 } 171 172 List languages = new ArrayList (); 173 for (int l = 0; l < availableLanguages.length; l++) { 174 if (availableLanguages[l].equals(language)) { 175 getLogger().debug( 176 "Do nothing because language was already tested: [" 177 + availableLanguages[l] 178 + "]"); 179 } else if (availableLanguages[l].equals(defaultLanguage)) { 180 languages.add(0, availableLanguages[l]); 181 } else { 182 languages.add(availableLanguages[l]); 183 } 184 } 185 186 int j = 0; 187 while (!file.exists() && j < languages.size()) { 188 String newlanguage = (String ) languages.get(j); 189 url = builder.buildCanonicalUrl(publication, area, childId, newlanguage); 190 try { 191 doc = builder.buildDocument(publication, url); 192 } catch (DocumentBuildException e) { 193 throw new SAXException (e); 194 } 195 file = doc.getFile(); 196 197 j++; 198 } 199 } 200 201 if (file.exists()) { 202 String path; 204 try { 205 path = file.getCanonicalPath(); 206 } catch (IOException e) { 207 throw new SAXException (e); 208 } 209 210 AttributesImpl attribute = new AttributesImpl (); 211 attribute.addAttribute("", "href", "href", "", url); 212 super.startElement(NAMESPACE, "child", PREFIX + "child", attribute); 213 214 AttributesImpl attributes = new AttributesImpl (); 215 attributes.addAttribute("", "src", "src", "", path); 216 attributes.addAttribute("", "element", "element", "", "included"); 217 218 super.startElement( 219 this.cIncludeNamespace, 220 "include", 221 cIncludePrefix + "include", 222 attributes); 223 super.endElement(this.cIncludeNamespace, "include", cIncludePrefix + "include"); 224 super.endElement(NAMESPACE, "child", PREFIX + "child"); 225 } else { 226 getLogger().warn("There are no existing file for the child with id " + childId); 228 } 229 230 } 231 } else { 232 super.startElement(uri, localName, raw, attr); 233 } 234 235 } 236 237 240 public SiteTree getSiteTree() { 241 return siteTree; 242 } 243 244 247 public void setSiteTree(SiteTree tree) { 248 siteTree = tree; 249 } 250 251 254 public void setArea(String string) { 255 area = string; 256 } 257 258 261 public void setBuilder(DocumentBuilder builder) { 262 this.builder = builder; 263 } 264 265 268 public void setDocument(Document document) { 269 this.document = document; 270 } 271 272 275 public void setPublication(Publication publication) { 276 this.publication = publication; 277 } 278 279 } 280 | Popular Tags |