Attachment 'SelectiveSitetreeGenerator.java'

Download

   1 /*
   2  * Copyright  1999-2004 The Apache Software Foundation
   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  */
  17 
  18 /* @version $Id$*/
  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.lenya.cms.publication.Label;
  31 import org.apache.lenya.cms.publication.PageEnvelope;
  32 import org.apache.lenya.cms.publication.PageEnvelopeFactory;
  33 import org.apache.lenya.cms.publication.Publication;
  34 import org.apache.lenya.cms.publication.SiteTree;
  35 import org.apache.lenya.cms.publication.SiteTreeNode;
  36 import org.apache.log4j.Logger;
  37 import org.xml.sax.SAXException;
  38 import org.xml.sax.helpers.AttributesImpl;
  39 import org.xml.sax.helpers.NamespaceSupport;
  40 
  41 /**
  42  * Generates only the part of the XML from the sitetree, corresponding to a given
  43  * node. The node is specified by the sitemap parameters area/documentid.
  44  * The topNodes will alway be generated.
  45  *   
  46  */
  47 public class SelectiveSitetreeGenerator extends SiteTreeGenerator {
  48 	
  49 	/*
  50 	 * unfortunatly, some importante functions and variables are private,
  51 	 * that's why we redeclare a lot. 
  52 	 */
  53     
  54 
  55 	private static Logger log = Logger.getLogger(SelectiveSitetreeGenerator.class);
  56     protected Publication publication;
  57 
  58     /** Parameter which denotes the documentid of the clicked node */
  59     protected String documentid;
  60 
  61 
  62     /**
  63      * Convenience object, so we don't need to create an AttributesImpl for
  64      * every element.
  65      */
  66     protected AttributesImpl attributes;
  67 
  68     
  69     protected static final String PARAM_DOCUMENTID = "documentid";
  70 
  71     
  72 
  73     /**
  74      * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver,
  75      *      java.util.Map, java.lang.String,
  76      *      org.apache.avalon.framework.parameters.Parameters)
  77      */
  78     public void setup(SourceResolver resolver, Map objectModel, String src,
  79             Parameters par) throws ProcessingException, SAXException,
  80             IOException {
  81         super.setup(resolver, objectModel, src, par);
  82 
  83         PageEnvelope envelope = null;
  84 
  85         if (getLogger().isDebugEnabled()) {
  86             Request request = ObjectModelHelper.getRequest(objectModel);
  87             getLogger().debug(
  88                     "Resolving page envelope for URL ["
  89                             + request.getRequestURI() + "]");
  90         }
  91 
  92         this.area = par.getParameter(AREA_PARAMETER, null);
  93         this.documentid = par.getParameter(PARAM_DOCUMENTID, null);
  94 
  95      
  96 
  97         try {
  98             envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(
  99                     objectModel);
 100             
 101             if ( documentid == null || documentid.trim().length() == 0 ) {
 102             	documentid = envelope.getDocument().getId();	
 103             }
 104             
 105             if ( area == null || area.trim().length() == 0 ) {
 106             	area = envelope.getDocument().getArea();
 107             }
 108             
 109         } catch (Exception e) {
 110             throw new ProcessingException("Resolving page envelope failed: ", e);
 111         }
 112 
 113         this.publication = envelope.getPublication();
 114         this.attributes = new AttributesImpl();
 115 
 116     }
 117     
 118 
 119     
 120     
 121     /**
 122      * @see org.apache.cocoon.generation.Generator#generate()
 123      */
 124     public void generate() throws SAXException {
 125         log.debug("generate");
 126         // Start the document and set the namespace.
 127         this.contentHandler.startDocument();
 128         // Default namespace.
 129         this.contentHandler.startPrefixMapping("", SiteTree.NAMESPACE_URI);
 130         this.contentHandler.startPrefixMapping(I18N_PX, I18N_NS);
 131 
 132         generateSiteTree(sitetree);
 133 
 134         // End the document.
 135         this.contentHandler.endPrefixMapping("");
 136         this.contentHandler.endDocument();
 137     }
 138     
 139     
 140     private void generateSiteTree(SiteTree tree) throws SAXException {
 141     	attributes.clear();
 142         // TODO: Do not hardcode "Authoring" label!!!
 143     	attributes.addAttribute("", LABEL_ATTRIBUTE, LABEL_ATTRIBUTE, CDATA, "Authoring");
 144     	attributes.addAttribute(I18N_NS, ATTR_ATTRIBUTE, Q_ATTR_ATTRIBUTE, CDATA, "label");
 145 
 146         this.contentHandler.startElement(SiteTree.NAMESPACE_URI, SITE_ELEMENT, SITE_ELEMENT, attributes);
 147 
 148         SiteTreeNode[] topNodes = tree.getTopNodes();
 149         for (int i = 0; i < topNodes.length; i++) {
 150             generateNodes(topNodes[i], this.documentid);
 151         }
 152 
 153         this.contentHandler.endElement(SiteTree.NAMESPACE_URI, SITE_ELEMENT, SITE_ELEMENT);
 154     }
 155 
 156 
 157     private void generateNodes(SiteTreeNode node, String docid) throws SAXException {
 158     	
 159         String nodeid;
 160         String childid;
 161 
 162         if (node == null)
 163             return;
 164         if (docid.startsWith("/"))
 165             docid = docid.substring(1);
 166         if (docid.indexOf("/") != -1) {
 167             nodeid = docid.substring(0, docid.indexOf("/"));
 168             childid = docid.substring(docid.indexOf("/") + 1);
 169         } else {
 170             nodeid = docid;
 171             childid = "";
 172         }
 173     	
 174     	attributes.clear();
 175     	attributes.addAttribute("", ID_ATTRIBUTE, ID_ATTRIBUTE, CDATA, node.getId());
 176         if (node.getHref() != null)
 177         	attributes.addAttribute("", HREF_ATTRIBUTE, HREF_ATTRIBUTE, CDATA, node.getHref());
 178         if (node.getSuffix() != null)
 179         	attributes.addAttribute("", SUFFIX_ATTRIBUTE, SUFFIX_ATTRIBUTE, CDATA, node.getSuffix());
 180         attributes.addAttribute("", LINK_ATTRIBUTE, LINK_ATTRIBUTE, CDATA, Boolean.toString(node
 181                 .hasLink()));
 182         attributes.addAttribute("", VISIBLEINNAV_ATTRIBUTE, VISIBLEINNAV_ATTRIBUTE, CDATA, Boolean
 183                 .toString(node.visibleInNav()));
 184 
 185         this.contentHandler.startElement(SiteTree.NAMESPACE_URI, NODE_ELEMENT, NODE_ELEMENT, attributes);
 186 
 187         Label[] labels = node.getLabels();
 188         for (int i = 0; i < labels.length; i++)
 189             generateLabels(labels[i]);
 190         SiteTreeNode[] children = node.getChildren();
 191         //yes, we should descend
 192     	if (node.getId().equals(nodeid)) {
 193     		
 194         for (int i = 0; i < children.length; i++) {
 195         		 generateNodes(children[i], childid);
 196             }
 197         }//end if 
 198 
 199         this.contentHandler.endElement(SiteTree.NAMESPACE_URI, NODE_ELEMENT, NODE_ELEMENT);
 200     }
 201     
 202     
 203     
 204     
 205     private void generateLabels(Label label) throws SAXException {
 206         attributes.clear();
 207         attributes.addAttribute(NamespaceSupport.XMLNS, LANG_ATTRIBUTE, Q_LANG_ATTRIBUTE, CDATA, label
 208                 .getLanguage());
 209 
 210         this.contentHandler
 211                 .startElement(SiteTree.NAMESPACE_URI, LABEL_ELEMENT, LABEL_ELEMENT, attributes);
 212         char[] labelA = label.getLabel().toCharArray();
 213         this.contentHandler.characters(labelA, 0, labelA.length);
 214         this.contentHandler.endElement(SiteTree.NAMESPACE_URI, LABEL_ELEMENT, LABEL_ELEMENT);
 215     }
 216 
 217 
 218    
 219 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.