1 17 18 19 20 package org.apache.lenya.cms.cocoon.transformation; 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.SourceResolver; 28 import org.apache.cocoon.transformation.AbstractSAXTransformer; 29 import org.apache.lenya.cms.cocoon.workflow.WorkflowHelper; 30 import org.apache.lenya.cms.publication.Document; 31 import org.apache.lenya.cms.publication.PageEnvelope; 32 import org.apache.lenya.cms.publication.PageEnvelopeFactory; 33 import org.apache.lenya.cms.workflow.WorkflowFactory; 34 import org.apache.lenya.workflow.Event; 35 import org.apache.lenya.workflow.Situation; 36 import org.apache.lenya.workflow.SynchronizedWorkflowInstances; 37 import org.apache.lenya.workflow.Workflow; 38 import org.apache.lenya.workflow.WorkflowException; 39 import org.xml.sax.Attributes ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.helpers.AttributesImpl ; 42 43 47 public class WorkflowMenuTransformer extends AbstractSAXTransformer { 48 public static final String MENU_ELEMENT = "menu"; 49 public static final String ITEM_ELEMENT = "item"; 50 public static final String EVENT_ATTRIBUTE = "event"; 51 52 55 public void startElement(String uri, String localName, String raw, Attributes attr) 56 throws SAXException { 57 boolean passed = true; 58 59 if (hasWorkflow() && localName.equals(ITEM_ELEMENT)) { 60 String event = attr.getValue(Workflow.NAMESPACE, EVENT_ATTRIBUTE); 61 if (getLogger().isDebugEnabled()) { 62 getLogger().debug("Event: [" + event + "]"); 63 } 64 65 if (event != null) { 67 passed = false; 68 69 AttributesImpl attributes = new AttributesImpl (attr); 70 71 int hrefIndex = attributes.getIndex("href"); 72 if (hrefIndex > -1) { 73 74 if (!containsEvent(event)) { 75 if (getLogger().isDebugEnabled()) { 76 getLogger().debug("Removing href attribute"); 77 } 78 attributes.removeAttribute(hrefIndex); 79 } else { 80 if (getLogger().isDebugEnabled()) { 81 getLogger().debug("Adding event to href attribute"); 82 } 83 String href = attributes.getValue("href"); 84 attributes.setValue(hrefIndex, href + "&lenya.event=" + event); 85 } 86 87 } 88 89 super.startElement(uri, localName, raw, attributes); 90 } 91 } 92 93 if (passed) { 94 super.startElement(uri, localName, raw, attr); 95 } 96 97 } 98 99 102 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) 103 throws ProcessingException, SAXException , IOException { 104 105 super.setup(resolver, objectModel, src, parameters); 106 107 PageEnvelope envelope = null; 108 109 try { 110 envelope = PageEnvelopeFactory.getInstance().getPageEnvelope(objectModel); 111 } catch (Exception e) { 112 throw new ProcessingException(e); 113 } 114 115 Document document = envelope.getDocument(); 116 WorkflowFactory factory = WorkflowFactory.newInstance(); 117 118 setHasWorkflow(factory.hasWorkflow(document)); 119 120 if (hasWorkflow()) { 121 Situation situation = null; 122 123 try { 124 setInstance(factory.buildSynchronizedInstance(document)); 125 situation = WorkflowHelper.buildSituation(objectModel); 126 } catch (Exception e) { 127 throw new ProcessingException(e); 128 } 129 130 try { 131 this.events = getInstance().getExecutableEvents(situation); 132 133 if (getLogger().isDebugEnabled()) { 134 getLogger().debug("Executable events: "); 135 for (int i = 0; i < events.length; i++) { 136 getLogger().debug(" [" + events[i] + "]"); 137 } 138 } 139 140 } catch (WorkflowException e) { 141 throw new ProcessingException(e); 142 } 143 } 144 } 145 146 private boolean hasWorkflow; 147 private SynchronizedWorkflowInstances instance; 148 149 154 protected SynchronizedWorkflowInstances getInstance() { 155 return instance; 156 } 157 158 private Event[] events; 159 160 165 protected boolean containsEvent(String eventName) { 166 boolean result = false; 167 168 for (int i = 0; i < events.length; i++) { 169 if (events[i].getName().equals(eventName)) { 170 result = true; 171 } 172 } 173 174 return result; 175 } 176 177 181 protected boolean hasWorkflow() { 182 return hasWorkflow; 183 } 184 185 189 public void setHasWorkflow(boolean hasWorkflow) { 190 this.hasWorkflow = hasWorkflow; 191 } 192 193 197 public void setInstance(SynchronizedWorkflowInstances instance) { 198 this.instance = instance; 199 } 200 } 201 | Popular Tags |