| 1 16 package org.outerj.daisy.frontend; 17 18 import org.outerj.daisy.repository.Repository; 19 import org.outerj.daisy.repository.RepositoryException; 20 import org.outerj.daisy.repository.variant.VariantManager; 21 import org.outerj.daisy.frontend.components.siteconf.SiteConf; 22 import org.outerj.daisy.util.VersionHelper; 23 import org.apache.excalibur.xml.sax.XMLizable; 24 import org.apache.cocoon.xml.AttributesImpl; 25 import org.apache.cocoon.xml.SaxBuffer; 26 import org.apache.cocoon.environment.Request; 27 import org.apache.cocoon.components.ContextHelper; 28 import org.apache.avalon.framework.context.Context; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.SAXException ; 31 import org.xml.sax.Attributes ; 32 33 import java.util.Properties ; 34 import java.io.IOException ; 35 36 40 public class PageContext implements XMLizable { 41 private String mountPoint; 42 private SiteConf siteConf; 43 private Repository repository; 44 private String layoutType; 45 private String skin; 46 private SaxBuffer skinConf; 47 private Context context; 48 private String versionMode; 49 private static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl(); 50 private static final Attributes versionAttributes; 51 static { 52 AttributesImpl attrs = new AttributesImpl(); 53 54 Properties versionProps; 55 try { 56 versionProps = VersionHelper.getVersionProperties(SetVersionHeaderAction.class.getClassLoader(), 57 "org/outerj/daisy/frontend/versioninfo.properties"); 58 } catch (IOException e) { 59 throw new RuntimeException ("Could not load Daisy Wiki version information."); 60 } 61 62 attrs.addAttribute("", "version", "version", "CDATA", versionProps.getProperty("artifact.version")); 63 attrs.addAttribute("", "buildHostName", "buildHostName", "CDATA", versionProps.getProperty("build.hostname")); 64 attrs.addAttribute("", "buildDateTime", "buildDateTime", "CDATA", versionProps.getProperty("build.datetime")); 65 66 versionAttributes = attrs; 67 } 68 71 public PageContext(String mountPoint, Repository repository, String layoutType, String skin, SaxBuffer skinConf, Context context) { 72 init(mountPoint, null, repository, layoutType, skin, skinConf, context); 73 } 74 75 78 public PageContext(String mountPoint, SiteConf siteConf, Repository repository, String layoutType, String skin, Context context) { 79 init(mountPoint, siteConf, repository, layoutType, skin, null, context); 80 } 81 82 private void init(String mountPoint, SiteConf siteConf, Repository repository, String layoutType, String skin, 83 SaxBuffer skinConf, Context context) { 84 this.mountPoint = mountPoint; 85 this.siteConf = siteConf; 86 this.repository = repository; 87 this.layoutType = layoutType == null ? "default" : layoutType; 88 this.skin = skin; 89 this.skinConf = skinConf; 90 this.context = context; 91 } 92 93 public String getMountPoint() { 94 return mountPoint; 95 } 96 97 public SiteConf getSiteConf() { 98 return siteConf; 99 } 100 101 public Repository getRepository() { 102 return repository; 103 } 104 105 public String getSkin() { 106 return skin; 107 } 108 109 public String getLayoutType() { 110 return layoutType; 111 } 112 113 public String getVersionMode() { 114 if (versionMode == null) { 115 Request request = getRequest(); 116 if (request != null) 117 versionMode = WikiHelper.getVersionMode(request).toString(); 118 } 119 return versionMode; 120 } 121 122 public void toSAX(ContentHandler contentHandler) throws SAXException { 123 try { 128 contentHandler.startElement("", "context", "context", EMPTY_ATTRIBUTES); 129 130 contentHandler.startElement("", "versionInfo", "versionInfo", versionAttributes); 132 contentHandler.endElement("", "versionInfo", "versionInfo"); 133 134 if (mountPoint != null) 136 generateStringElement("mountPoint", mountPoint, contentHandler); 137 138 if (getVersionMode() != null) { 140 generateStringElement("versionMode", getVersionMode(), contentHandler); 141 } 142 143 if (siteConf != null && repository != null) { 145 AttributesImpl siteAttrs = new AttributesImpl(); 146 siteAttrs.addCDATAAttribute("name", siteConf.getName()); 147 siteAttrs.addCDATAAttribute("title", siteConf.getTitle()); 148 siteAttrs.addCDATAAttribute("description", siteConf.getDescription()); 149 siteAttrs.addCDATAAttribute("navigationDocId", String.valueOf(siteConf.getNavigationDocId())); 150 siteAttrs.addCDATAAttribute("collectionId", String.valueOf(siteConf.getCollectionId())); 151 siteAttrs.addCDATAAttribute("collection", repository.getCollectionManager().getCollection(siteConf.getCollectionId(), false).getName()); 152 siteAttrs.addCDATAAttribute("branchId", String.valueOf(siteConf.getBranchId())); 153 VariantManager variantManager = repository.getVariantManager(); 154 siteAttrs.addCDATAAttribute("branch", variantManager.getBranch(siteConf.getBranchId(), false).getName()); 155 siteAttrs.addCDATAAttribute("languageId", String.valueOf(siteConf.getLanguageId())); 156 siteAttrs.addCDATAAttribute("language", variantManager.getLanguage(siteConf.getLanguageId(), false).getName()); 157 158 contentHandler.startElement("", "site", "site", siteAttrs); 159 contentHandler.endElement("", "site", "site"); 160 } 161 162 SaxBuffer skinConf; 163 if (siteConf != null) 164 skinConf = siteConf.getSkinConf(); 165 else 166 skinConf = this.skinConf; 167 if (skinConf != null) 168 skinConf.toSAX(contentHandler); 169 170 if (repository != null) { 172 UserInfoStreamer.streamUserInfo(repository, contentHandler); 173 } 174 175 generateStringElement("layoutType", layoutType, contentHandler); 176 177 Request request = getRequest(); 178 if (request != null) { 179 String requestURI = request.getRequestURI(); 180 String queryString = request.getQueryString(); 181 if (queryString != null) { 182 requestURI = requestURI + "?" + queryString; 183 } 184 String method = request.getMethod(); 185 186 AttributesImpl requestAttrs = new AttributesImpl(); 187 requestAttrs.addAttribute("", "uri", "uri", "CDATA", requestURI); 188 requestAttrs.addAttribute("", "method", "method", "CDATA", method); 189 requestAttrs.addAttribute("", "server", "server", "CDATA", RequestUtil.getServer(request)); 190 contentHandler.startElement("", "request", "request", requestAttrs); 191 contentHandler.endElement("", "request", "request"); 192 } 193 194 if (skin != null) 195 generateStringElement("skin", skin, contentHandler); 196 197 contentHandler.endElement("", "context", "context"); 198 } catch (RepositoryException e) { 199 throw new SAXException ("Error in PageContext.toSAX", e); 200 } 201 } 202 203 private void generateStringElement(String name, String value, ContentHandler contentHandler) throws SAXException { 204 contentHandler.startElement("", name, name, EMPTY_ATTRIBUTES); 205 contentHandler.characters(value.toCharArray(), 0, value.length()); 206 contentHandler.endElement("", name, name); 207 } 208 209 private Request getRequest() { 210 if (context != null) 211 return ContextHelper.getRequest(context); 212 else 213 return null; 214 } 215 216 } 217 | Popular Tags |