1 17 18 19 20 package org.apache.lenya.cms.cocoon.generation; 21 22 import java.io.IOException ; 23 import java.io.Serializable ; 24 import java.util.Map ; 25 26 import org.apache.avalon.framework.parameters.ParameterException; 27 import org.apache.avalon.framework.parameters.Parameterizable; 28 import org.apache.avalon.framework.parameters.Parameters; 29 import org.apache.cocoon.ProcessingException; 30 import org.apache.cocoon.caching.CacheableProcessingComponent; 31 import org.apache.cocoon.environment.SourceResolver; 32 import org.apache.cocoon.generation.ServiceableGenerator; 33 import org.apache.excalibur.source.SourceValidity; 34 import org.apache.excalibur.source.impl.validity.TimeStampValidity; 35 import org.apache.lenya.cms.publication.Label; 36 import org.apache.lenya.cms.publication.LastModified; 37 import org.apache.lenya.cms.publication.Publication; 38 import org.apache.lenya.cms.publication.PublicationException; 39 import org.apache.lenya.cms.publication.PublicationFactory; 40 import org.apache.lenya.cms.publication.SiteTree; 41 import org.apache.lenya.cms.publication.SiteTreeException; 42 import org.apache.lenya.cms.publication.SiteTreeNode; 43 import org.apache.log4j.Logger; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.helpers.AttributesImpl ; 46 import org.xml.sax.helpers.NamespaceSupport ; 47 48 53 public class SiteTreeGenerator extends ServiceableGenerator implements Parameterizable, 54 CacheableProcessingComponent { 55 private static Logger log = Logger.getLogger(SiteTreeGenerator.class); 56 57 protected final static String I18N_PX = "i18n"; 58 protected final static String I18N_NS = "http://apache.org/cocoon/i18n/2.1"; 59 60 protected final static String CDATA = "CDATA"; 61 62 protected final static String SITE_ELEMENT = "site"; 63 protected final static String NODE_ELEMENT = "node"; 64 protected final static String LABEL_ELEMENT = "label"; 65 66 protected final static String LABEL_ATTRIBUTE = "label"; 67 protected final static String ATTR_ATTRIBUTE = "attr"; 68 protected final static String Q_ATTR_ATTRIBUTE = I18N_PX + ":" + ATTR_ATTRIBUTE; 69 protected final static String ID_ATTRIBUTE = "id"; 70 protected final static String LANG_ATTRIBUTE = "lang"; 71 protected final static String HREF_ATTRIBUTE = "href"; 72 protected final static String LINK_ATTRIBUTE = "link"; 73 protected final static String VISIBLEINNAV_ATTRIBUTE = "visibleinnav"; 74 protected final static String SUFFIX_ATTRIBUTE = "suffix"; 75 76 protected final static String Q_LANG_ATTRIBUTE = "xml:lang"; 77 78 81 public final static String AREA_PARAMETER = "area"; 82 83 private final AttributesImpl atts = new AttributesImpl (); 84 85 SiteTree sitetree = null; 86 String area = null; 87 88 92 public void parameterize(Parameters parameters) throws ParameterException { 93 } 94 95 99 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 100 throws ProcessingException, SAXException , IOException { 101 log.debug("setup"); 102 try { 103 Publication publication = PublicationFactory.getPublication(objectModel); 104 area = par.getParameter(AREA_PARAMETER); 105 sitetree = publication.getTree(area); 106 } catch (PublicationException e) { 107 throw new ProcessingException("Unable to get sitetree: publication exception.", e); 108 } catch (ParameterException e) { 109 throw new ProcessingException("Unable to get sitetree: parameter 'area' not found.", e); 110 } catch (SiteTreeException e) { 111 throw new ProcessingException("Unable to get sitetree.", e); 112 } 113 } 114 115 118 public void generate() throws SAXException { 119 log.debug("generate"); 120 this.contentHandler.startDocument(); 122 this.contentHandler.startPrefixMapping("", SiteTree.NAMESPACE_URI); 124 this.contentHandler.startPrefixMapping(I18N_PX, I18N_NS); 125 126 generateSiteTree(sitetree); 127 128 this.contentHandler.endPrefixMapping(""); 130 this.contentHandler.endDocument(); 131 } 132 133 private void generateSiteTree(SiteTree tree) throws SAXException { 134 atts.clear(); 135 atts.addAttribute("", LABEL_ATTRIBUTE, LABEL_ATTRIBUTE, CDATA, "Authoring"); 137 atts.addAttribute(I18N_NS, ATTR_ATTRIBUTE, Q_ATTR_ATTRIBUTE, CDATA, "label"); 138 139 this.contentHandler.startElement(SiteTree.NAMESPACE_URI, SITE_ELEMENT, SITE_ELEMENT, atts); 140 141 SiteTreeNode[] topNodes = tree.getTopNodes(); 142 for (int i = 0; i < topNodes.length; i++) { 143 generateNodes(topNodes[i]); 144 } 145 146 this.contentHandler.endElement(SiteTree.NAMESPACE_URI, SITE_ELEMENT, SITE_ELEMENT); 147 } 148 149 private void generateNodes(SiteTreeNode node) throws SAXException { 150 atts.clear(); 151 atts.addAttribute("", ID_ATTRIBUTE, ID_ATTRIBUTE, CDATA, node.getId()); 152 if (node.getHref() != null) 153 atts.addAttribute("", HREF_ATTRIBUTE, HREF_ATTRIBUTE, CDATA, node.getHref()); 154 if (node.getSuffix() != null) 155 atts.addAttribute("", SUFFIX_ATTRIBUTE, SUFFIX_ATTRIBUTE, CDATA, node.getSuffix()); 156 atts.addAttribute("", LINK_ATTRIBUTE, LINK_ATTRIBUTE, CDATA, Boolean.toString(node 157 .hasLink())); 158 atts.addAttribute("", VISIBLEINNAV_ATTRIBUTE, VISIBLEINNAV_ATTRIBUTE, CDATA, Boolean 159 .toString(node.visibleInNav())); 160 161 this.contentHandler.startElement(SiteTree.NAMESPACE_URI, NODE_ELEMENT, NODE_ELEMENT, atts); 162 163 Label[] labels = node.getLabels(); 164 for (int i = 0; i < labels.length; i++) 165 generateLabels(labels[i]); 166 SiteTreeNode[] children = node.getChildren(); 167 for (int i = 0; i < children.length; i++) 168 generateNodes(children[i]); 169 170 this.contentHandler.endElement(SiteTree.NAMESPACE_URI, NODE_ELEMENT, NODE_ELEMENT); 171 } 172 173 private void generateLabels(Label label) throws SAXException { 174 atts.clear(); 175 atts.addAttribute(NamespaceSupport.XMLNS, LANG_ATTRIBUTE, Q_LANG_ATTRIBUTE, CDATA, label 176 .getLanguage()); 177 178 this.contentHandler 179 .startElement(SiteTree.NAMESPACE_URI, LABEL_ELEMENT, LABEL_ELEMENT, atts); 180 char[] labelA = label.getLabel().toCharArray(); 181 this.contentHandler.characters(labelA, 0, labelA.length); 182 this.contentHandler.endElement(SiteTree.NAMESPACE_URI, LABEL_ELEMENT, LABEL_ELEMENT); 183 } 184 185 188 public void recycle() { 189 log.debug("recycle"); 190 super.recycle(); 191 sitetree = null; 192 area = null; 193 } 194 195 198 public Serializable getKey() { 199 return area; 200 } 201 202 205 public SourceValidity getValidity() { 206 if (!(sitetree instanceof LastModified)) { 208 return null; 209 } else { 210 return new TimeStampValidity(((LastModified) sitetree).getLastModified()); 211 } 212 } 213 } | Popular Tags |