1 16 package org.apache.cocoon.generation; 17 18 import java.io.IOException ; 19 import java.util.Map ; 20 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.cocoon.ProcessingException; 23 import org.apache.cocoon.xml.XMLUtils; 24 import org.apache.cocoon.components.source.InspectableSource; 25 import org.apache.cocoon.components.source.LockableSource; 26 import org.apache.cocoon.components.source.RestrictableSource; 27 import org.apache.cocoon.components.source.VersionableSource; 28 import org.apache.cocoon.components.source.helpers.GroupSourcePermission; 29 import org.apache.cocoon.components.source.helpers.PrincipalSourcePermission; 30 import org.apache.cocoon.components.source.helpers.SourceLock; 31 import org.apache.cocoon.components.source.helpers.SourcePermission; 32 import org.apache.cocoon.components.source.helpers.SourceProperty; 33 import org.apache.cocoon.environment.SourceResolver; 34 import org.apache.excalibur.source.SourceException; 35 import org.apache.excalibur.source.TraversableSource; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.helpers.AttributesImpl ; 38 39 68 public class TraversableSourceDescriptionGenerator extends TraversableGenerator { 69 70 protected static final String MIME_TYPE_ATTR_NAME = "mimeType"; 71 72 private static final String REVISION_ATTR_NAME = "revision"; 73 private static final String REVISIONBRANCH_ATTR_NAME = "branch"; 74 75 private static final String PROPERTIES_NODE_NAME = "properties"; 76 private static final String PROPERTIES_NODE_QNAME = PREFIX + ":" + PROPERTIES_NODE_NAME; 77 78 private static final String PERMISSIONS_NODE_NAME = "permissions"; 79 private static final String PERMISSIONS_NODE_QNAME = PREFIX + ":" + PERMISSIONS_NODE_NAME; 80 private static final String PERMISSION_NODE_NAME = "permission"; 81 private static final String PERMISSION_NODE_QNAME = PREFIX + ":" + PERMISSION_NODE_NAME; 82 83 private static final String LOCKS_NODE_NAME = "locks"; 84 private static final String LOCKS_NODE_QNAME = PREFIX + ":" + LOCKS_NODE_NAME; 85 private static final String LOCK_NODE_NAME = "lock"; 86 private static final String LOCK_NODE_QNAME = PREFIX + ":" + LOCK_NODE_NAME; 87 88 private static final String PRINCIPAL_ATTR_NAME = "principal"; 89 private static final String GROUP_ATTR_NAME = "group"; 90 private static final String PRIVILEGE_ATTR_NAME = "privilege"; 91 private static final String INHERITABLE_ATTR_NAME = "inheritable"; 92 private static final String NEGATIVE_ATTR_NAME = "negative"; 93 94 private static final String TYPE_ATTR_NAME = "type"; 95 private static final String EXPIRATION_ATTR_NAME = "expiration"; 96 private static final String EXCLUSIVE_ATTR_NAME = "exclusive"; 97 98 99 100 private boolean properties = true; 101 102 103 private boolean permissions = true; 104 105 106 private boolean locks = true; 107 108 109 private boolean version = true; 110 111 112 121 public void setup(SourceResolver resolver, Map objectModel, 122 String location, 123 Parameters parameters) 124 throws ProcessingException, SAXException , IOException { 125 126 super.setup(resolver, objectModel, location, parameters); 127 128 this.properties = parameters.getParameterAsBoolean("properties", true); 129 super.cacheKeyParList.add(String.valueOf(String.valueOf(this.properties))); 130 131 this.permissions = parameters.getParameterAsBoolean("permissions", true); 132 super.cacheKeyParList.add(String.valueOf(this.permissions)); 133 134 this.locks = parameters.getParameterAsBoolean("locks", true); 135 super.cacheKeyParList.add(String.valueOf(this.locks)); 136 137 this.version = parameters.getParameterAsBoolean("version", true); 138 super.cacheKeyParList.add(String.valueOf(this.version)); 139 140 if (getLogger().isDebugEnabled()) { 141 getLogger().debug("properties: " + this.properties); 142 getLogger().debug("permissions: " + this.permissions); 143 getLogger().debug("locks: " + this.locks); 144 getLogger().debug("version: " + this.version); 145 } 146 } 147 148 153 protected final void addContent(TraversableSource source) 154 throws SAXException , ProcessingException { 155 156 super.addContent(source); 157 try { 158 if (this.properties && (source instanceof InspectableSource)) { 159 pushSourceProperties((InspectableSource) source); 160 } 161 if (this.permissions && (source instanceof RestrictableSource)) { 162 pushSourcePermissions((RestrictableSource) source); 163 } 164 if (this.locks && (source instanceof LockableSource)) { 165 pushSourceLocks((LockableSource) source); 166 } 167 } catch (SourceException e) { 168 throw new ProcessingException(e); 169 } 170 171 } 172 173 182 protected void setNodeAttributes(TraversableSource source) throws SAXException , ProcessingException { 183 super.setNodeAttributes(source); 184 if (!source.isCollection()) { 185 String mimeType = source.getMimeType(); 186 if (mimeType != null) { 187 attributes.addAttribute("", MIME_TYPE_ATTR_NAME, MIME_TYPE_ATTR_NAME, 188 "CDATA", source.getMimeType()); 189 } 190 } 191 if (this.version && source instanceof VersionableSource) { 192 try { 193 VersionableSource versionablesource = (VersionableSource) source; 194 if (versionablesource.isVersioned()) { 195 if ((versionablesource.getSourceRevision()!=null) && 196 (versionablesource.getSourceRevision().length()>0)) { 197 attributes.addAttribute("", 198 REVISION_ATTR_NAME, 199 REVISION_ATTR_NAME, "CDATA", 200 versionablesource.getSourceRevision()); 201 } 202 203 if ((versionablesource.getSourceRevisionBranch()!=null) && 204 (versionablesource.getSourceRevisionBranch().length()> 205 0)) { 206 attributes.addAttribute("", 207 REVISIONBRANCH_ATTR_NAME, 208 REVISIONBRANCH_ATTR_NAME, 209 "CDATA", 210 versionablesource.getSourceRevisionBranch()); 211 } 212 } 213 } catch (SourceException e) { 214 throw new ProcessingException(e); 215 } 216 } 217 } 218 219 225 private void pushSourceProperties(InspectableSource source) 226 throws SAXException , SourceException { 227 228 SourceProperty[] properties = source.getSourceProperties(); 229 if (properties != null && properties.length > 0) { 230 this.contentHandler.startElement(URI, PROPERTIES_NODE_NAME, 231 PROPERTIES_NODE_QNAME, XMLUtils.EMPTY_ATTRIBUTES); 232 for (int i = 0; i < properties.length; i++) { 233 SourceProperty property = properties[i]; 234 property.toSAX(this.contentHandler); 235 } 236 this.contentHandler.endElement(URI, PROPERTIES_NODE_NAME, 237 PROPERTIES_NODE_QNAME); 238 } 239 } 240 241 242 247 private void pushSourcePermissions(RestrictableSource source) 248 throws SAXException , SourceException { 249 SourcePermission[] permissions = source.getSourcePermissions(); 250 251 if (permissions != null && permissions.length > 0) { 252 this.contentHandler.startElement(URI, 253 PERMISSIONS_NODE_NAME, 254 PERMISSIONS_NODE_QNAME, 255 XMLUtils.EMPTY_ATTRIBUTES); 256 257 for (int i = 0; i < permissions.length; i++) { 258 AttributesImpl attributes = new AttributesImpl (); 259 260 if (permissions[i] instanceof PrincipalSourcePermission) { 261 attributes.addAttribute("", PRINCIPAL_ATTR_NAME, 262 PRINCIPAL_ATTR_NAME, "CDATA", 263 ((PrincipalSourcePermission) permissions[i]).getPrincipal()); 264 } else if (permissions[i] instanceof GroupSourcePermission) { 265 attributes.addAttribute("", GROUP_ATTR_NAME, 266 GROUP_ATTR_NAME, "CDATA", 267 ((GroupSourcePermission) permissions[i]).getGroup()); 268 } 269 270 attributes.addAttribute("", PRIVILEGE_ATTR_NAME, 271 PRIVILEGE_ATTR_NAME, "CDATA", 272 permissions[i].getPrivilege()); 273 attributes.addAttribute("", INHERITABLE_ATTR_NAME, 274 INHERITABLE_ATTR_NAME, "CDATA", 275 String.valueOf(permissions[i].isInheritable())); 276 attributes.addAttribute("", NEGATIVE_ATTR_NAME, 277 NEGATIVE_ATTR_NAME, "CDATA", 278 String.valueOf(permissions[i].isNegative())); 279 280 this.contentHandler.startElement(URI, 281 PERMISSION_NODE_NAME, 282 PERMISSION_NODE_QNAME, 283 attributes); 284 this.contentHandler.endElement(URI, 285 PERMISSION_NODE_NAME, 286 PERMISSION_NODE_QNAME); 287 } 288 289 this.contentHandler.endElement(URI, PERMISSIONS_NODE_NAME, 290 PERMISSIONS_NODE_QNAME); 291 } 292 } 293 294 299 public void pushSourceLocks(LockableSource source) 300 throws SAXException , SourceException { 301 SourceLock[] locks = source.getSourceLocks(); 302 303 if (locks != null && locks.length > 0) { 304 this.contentHandler.startElement(URI, LOCKS_NODE_NAME, 305 LOCKS_NODE_QNAME, 306 XMLUtils.EMPTY_ATTRIBUTES); 307 308 for (int i = 0; locks.length > 0; i++) { 309 SourceLock lock = locks[i]; 310 311 AttributesImpl attributes = new AttributesImpl (); 312 attributes.addAttribute("", PRINCIPAL_ATTR_NAME, 313 PRINCIPAL_ATTR_NAME, "CDATA", 314 lock.getSubject()); 315 attributes.addAttribute("", TYPE_ATTR_NAME, TYPE_ATTR_NAME, 316 "CDATA", lock.getType()); 317 attributes.addAttribute("", EXPIRATION_ATTR_NAME, 318 EXPIRATION_ATTR_NAME, "CDATA", 319 lock.getExpiration().toString()); 320 attributes.addAttribute("", INHERITABLE_ATTR_NAME, 321 INHERITABLE_ATTR_NAME, "CDATA", 322 String.valueOf(lock.isInheritable())); 323 attributes.addAttribute("", EXCLUSIVE_ATTR_NAME, 324 EXCLUSIVE_ATTR_NAME, "CDATA", 325 String.valueOf(lock.isExclusive())); 326 327 this.contentHandler.startElement(URI, LOCK_NODE_NAME, 328 LOCK_NODE_QNAME, attributes); 329 this.contentHandler.endElement(URI, LOCK_NODE_NAME, 330 LOCK_NODE_QNAME); 331 } 332 333 this.contentHandler.endElement(URI, LOCKS_NODE_NAME, 334 LOCKS_NODE_QNAME); 335 } 336 } 337 338 } 339 | Popular Tags |