1 17 18 19 20 package org.apache.lenya.cms.cocoon.generation; 21 22 import java.io.IOException ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.parameters.Parameters; 26 import org.apache.cocoon.ProcessingException; 27 import org.apache.cocoon.environment.ObjectModelHelper; 28 import org.apache.cocoon.environment.Request; 29 import org.apache.cocoon.environment.SourceResolver; 30 import org.apache.cocoon.generation.AbstractGenerator; 31 import org.apache.lenya.cms.publication.Label; 32 import org.apache.lenya.cms.publication.PageEnvelope; 33 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 34 import org.apache.lenya.cms.publication.Publication; 35 import org.apache.lenya.cms.publication.SiteTree; 36 import org.apache.lenya.cms.publication.SiteTreeException; 37 import org.apache.lenya.cms.publication.SiteTreeNode; 38 import org.xml.sax.SAXException ; 39 import org.xml.sax.helpers.AttributesImpl ; 40 41 50 public class SitetreeFragmentGenerator extends AbstractGenerator { 51 52 protected Publication publication; 53 54 55 protected String documentid; 56 57 58 protected String area; 59 60 64 protected boolean initialTree; 65 66 70 protected String [] areas = null; 71 72 76 protected AttributesImpl attributes; 77 78 protected static final String PARAM_AREA = "area"; 79 80 protected static final String PARAM_DOCUMENTID = "documentid"; 81 82 protected static final String PARAM_INITIAL = "initial"; 83 84 protected static final String PARAM_AREAS = "areas"; 85 86 87 protected static final String URI = "http://apache.org/cocoon/lenya/sitetree/1.0"; 88 89 protected static final String XML_URI = "http://www.w3.org/XML/1998/namespace"; 90 91 92 protected static final String PREFIX = "site"; 93 94 protected static final String XML_PREFIX = "xml"; 95 96 protected static final String NODE_NODE = "node"; 97 98 protected static final String NODE_LABEL = "label"; 99 100 protected static final String NODE_SITE = "site"; 101 102 protected static final String NODE_FRAGMENT = "fragment"; 103 104 protected static final String ATTR_ID = "id"; 105 106 protected static final String ATTR_FOLDER = "folder"; 107 108 protected static final String ATTR_AREA = "area"; 109 110 protected static final String ATTR_LABEL = "label"; 111 112 protected static final String ATTR_VISIBLEINNAV = "visibleinnav"; 113 114 protected static final String ATTR_LINK = "link"; 115 116 protected static final String ATTR_BASE = "base"; 117 118 protected static final String ATTR_SUFFIX = "suffix"; 119 120 protected static final String ATTR_HREF = "href"; 121 122 protected static final String ATTR_LANG = "lang"; 123 124 129 public void setup(SourceResolver resolver, Map objectModel, String src, 130 Parameters par) throws ProcessingException, SAXException , 131 IOException { 132 super.setup(resolver, objectModel, src, par); 133 134 PageEnvelope envelope = null; 135 136 if (getLogger().isDebugEnabled()) { 137 Request request = ObjectModelHelper.getRequest(objectModel); 138 getLogger().debug( 139 "Resolving page envelope for URL [" 140 + request.getRequestURI() + "]"); 141 } 142 143 this.area = par.getParameter(PARAM_AREA, null); 144 this.documentid = par.getParameter(PARAM_DOCUMENTID, null); 145 146 if (par.isParameter(PARAM_INITIAL)) { 147 this.initialTree = Boolean.valueOf( 148 par.getParameter(PARAM_INITIAL, null)).booleanValue(); 149 } else { 150 this.initialTree = false; 151 } 152 153 if (par.isParameter(PARAM_AREAS)) { 154 String parAreas = par.getParameter(PARAM_AREAS, null); 155 this.areas = parAreas.split(","); 156 } else { 157 String temp[] = { "authoring", "archive", "trash" }; 158 this.areas = temp; 159 } 160 161 if (this.getLogger().isDebugEnabled()) { 162 this.getLogger().debug("Parameter area: " + this.area); 163 this.getLogger().debug("Parameter documentid: " + this.documentid); 164 this.getLogger() 165 .debug("Parameter initialTree: " + this.initialTree); 166 String areasStr = ""; 167 for (int i = 0; i < this.areas.length; i++) 168 areasStr += this.areas[i] + " "; 169 this.getLogger().debug("Parameter areas: " + areasStr); 170 } 171 172 try { 173 envelope = PageEnvelopeFactory.getInstance().getPageEnvelope( 174 objectModel); 175 } catch (Exception e) { 176 throw new ProcessingException("Resolving page envelope failed: ", e); 177 } 178 179 this.publication = envelope.getPublication(); 180 this.attributes = new AttributesImpl (); 181 182 } 183 184 187 public void generate() throws IOException , SAXException , 188 ProcessingException { 189 190 try { 191 192 this.contentHandler.startDocument(); 193 this.contentHandler.startPrefixMapping(PREFIX, URI); 194 195 attributes.clear(); 196 if (!initialTree) { 197 attributes.addAttribute("", ATTR_AREA, ATTR_AREA, "CDATA", 198 this.area); 199 attributes.addAttribute("", ATTR_BASE, ATTR_BASE, "CDATA", 200 this.documentid); 201 } 202 203 this.contentHandler.startElement(URI, NODE_FRAGMENT, PREFIX + ':' 204 + NODE_FRAGMENT, this.attributes); 205 206 if (this.initialTree) { 207 for (int i = 0; i < this.areas.length; i++) { 208 generateFragmentInitial(this.areas[i]); 209 } 210 } else { 211 generateFragment(); 212 } 213 214 this.contentHandler.endElement(URI, NODE_FRAGMENT, PREFIX + ':' 215 + NODE_FRAGMENT); 216 217 this.contentHandler.endPrefixMapping(PREFIX); 218 this.contentHandler.endDocument(); 219 220 } catch (SAXException e) { 221 throw new ProcessingException(e); 222 } catch (SiteTreeException e) { 223 throw new ProcessingException(e); 224 } 225 226 } 227 228 236 protected void generateFragment() throws SiteTreeException, SAXException , 237 ProcessingException { 238 239 SiteTree siteTree = null; 240 if (!this.area.equals(Publication.AUTHORING_AREA) 241 && !this.area.equals(Publication.ARCHIVE_AREA) 242 && !this.area.equals(Publication.TRASH_AREA) 243 && !this.area.equals(Publication.STAGING_AREA) 244 && !this.area.equals(Publication.LIVE_AREA)) { 245 throw new ProcessingException("Invalid area: " + this.area); 246 } 247 siteTree = publication.getTree(this.area); 248 249 SiteTreeNode node = siteTree.getNode(this.documentid); 250 if (this.getLogger().isDebugEnabled()) { 251 this.getLogger().debug( 252 "Node with documentid " + documentid + " found."); 253 } 254 if (node == null) 255 throw new SiteTreeException("Node with documentid " + documentid 256 + " not found."); 257 258 SiteTreeNode[] children = node.getChildren(); 259 260 for (int i = 0; i < children.length; i++) { 261 startNode(NODE_NODE, children[i]); 262 addLabels(children[i]); 263 endNode(NODE_NODE); 264 } 265 } 266 267 277 protected void generateFragmentInitial(String siteArea) 278 throws SiteTreeException, SAXException , ProcessingException { 279 280 SiteTree siteTree = publication.getTree(siteArea); 281 282 String label = ""; 283 String isFolder = ""; 284 285 if (siteArea.equals(Publication.AUTHORING_AREA)) 287 label = "Authoring"; 288 if (siteArea.equals(Publication.ARCHIVE_AREA)) 289 label = "Archive"; 290 if (siteArea.equals(Publication.TRASH_AREA)) 291 label = "Trash"; 292 if (siteArea.equals(Publication.LIVE_AREA)) 293 label = "Live"; 294 if (siteArea.equals(Publication.STAGING_AREA)) 295 label = "Staging"; 296 297 if (siteTree.getTopNodes().length > 0) 298 isFolder = "true"; 299 else 300 isFolder = "false"; 301 302 this.attributes.clear(); 303 this.attributes.addAttribute("", ATTR_AREA, ATTR_AREA, "CDATA", 304 siteArea); 305 this.attributes.addAttribute("", ATTR_FOLDER, ATTR_FOLDER, "CDATA", 306 isFolder); 307 this.attributes 308 .addAttribute("", ATTR_LABEL, ATTR_LABEL, "CDATA", label); 309 310 startNode(NODE_SITE); 311 312 if (area.equals(siteArea)) { 313 generateFragmentRecursive(siteTree.getTopNodes(), this.documentid); 314 } 315 316 endNode(NODE_SITE); 317 } 318 319 328 protected void generateFragmentRecursive(SiteTreeNode[] nodes, String docid) 329 throws SiteTreeException, SAXException { 330 String nodeid; 331 String childid; 332 333 if (nodes == null) 334 return; 335 if (docid.startsWith("/")) 336 docid = docid.substring(1); 337 if (docid.indexOf("/") != -1) { 338 nodeid = docid.substring(0, docid.indexOf("/")); 339 childid = docid.substring(docid.indexOf("/") + 1); 340 } else { 341 nodeid = docid; 342 childid = ""; 343 } 344 345 for (int i = 0; i < nodes.length; i++) { 346 startNode(NODE_NODE, nodes[i]); 347 addLabels(nodes[i]); 348 if (nodes[i].getId().equals(nodeid)) { 349 generateFragmentRecursive(nodes[i].getChildren(), childid); 350 } 351 endNode(NODE_NODE); 352 } 353 } 354 355 363 protected void startNode(String nodeName) throws SAXException { 364 this.contentHandler.startElement(URI, nodeName, 365 PREFIX + ':' + nodeName, this.attributes); 366 } 367 368 378 protected void startNode(String nodeName, SiteTreeNode node) 379 throws SAXException { 380 setNodeAttributes(node); 381 this.contentHandler.startElement(URI, nodeName, 382 PREFIX + ':' + nodeName, this.attributes); 383 } 384 385 393 protected void setNodeAttributes(SiteTreeNode node) throws SAXException { 394 this.attributes.clear(); 395 396 String id = node.getId(); 397 String isVisible = Boolean.toString(node.visibleInNav()); 398 String hasLink = Boolean.toString(node.hasLink()); 399 String href = node.getHref(); 400 String suffix = node.getSuffix(); 401 String isFolder = isFolder(node); 402 403 if (this.getLogger().isDebugEnabled()) { 404 this.getLogger().debug("adding attribute id: " + id); 405 this.getLogger().debug( 406 "adding attribute visibleinnav: " + isVisible); 407 this.getLogger().debug("adding attribute link: " + hasLink); 408 if (href != null) 409 this.getLogger().debug("adding attribute href: " + href); 410 if (suffix != null) 411 this.getLogger().debug("adding attribute suffix: " + suffix); 412 this.getLogger().debug("adding attribute folder: " + isFolder); 413 } 414 this.attributes.addAttribute("", ATTR_ID, ATTR_ID, "CDATA", id); 415 this.attributes.addAttribute("", ATTR_VISIBLEINNAV, ATTR_VISIBLEINNAV, 416 "CDATA", isVisible); 417 this.attributes 418 .addAttribute("", ATTR_LINK, ATTR_LINK, "CDATA", hasLink); 419 if (href != null) 420 this.attributes.addAttribute("", ATTR_HREF, ATTR_HREF, "CDATA", 421 href); 422 if (suffix != null) 423 this.attributes.addAttribute("", ATTR_SUFFIX, ATTR_SUFFIX, "CDATA", 424 suffix); 425 this.attributes.addAttribute("", ATTR_FOLDER, ATTR_FOLDER, "CDATA", 426 isFolder); 427 } 428 429 438 protected String isFolder(SiteTreeNode node) { 439 if (node.getChildren().length > 0) 440 return "true"; 441 else 442 return "false"; 443 } 444 445 453 protected void endNode(String nodeName) throws SAXException { 454 this.contentHandler.endElement(URI, nodeName, PREFIX + ':' + nodeName); 455 } 456 457 463 protected void addLabels(SiteTreeNode node) throws SAXException { 464 Label[] labels = node.getLabels(); 465 466 for (int i = 0; i < labels.length; i++) { 467 String lang = labels[i].getLanguage(); 468 if (lang == null) 469 lang = ""; 470 addLabel(labels[i].getLabel(), lang); 471 } 472 } 473 474 483 protected void addLabel(String label, String language) throws SAXException { 484 this.attributes.clear(); 485 this.attributes.addAttribute(XML_URI, ATTR_LANG, XML_PREFIX + ":" 486 + ATTR_LANG, "CDATA", language); 487 488 this.contentHandler.startElement(URI, NODE_LABEL, PREFIX + ':' 489 + NODE_LABEL, this.attributes); 490 char[] labelArray = label.toCharArray(); 491 this.contentHandler.characters(labelArray, 0, labelArray.length); 492 this.contentHandler.endElement(URI, NODE_LABEL, PREFIX + ':' 493 + NODE_LABEL); 494 } 495 496 } 497 | Popular Tags |