KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > projector > expression > EventExpression


1 /*
2  * $Header: /home/cvs/jakarta-slide/projector/src/java/org/apache/slide/projector/expression/EventExpression.java,v 1.4 2004/07/28 09:48:21 ib Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/07/28 09:48:21 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.projector.expression;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.slide.projector.Projector;
31 import org.apache.slide.projector.engine.Job;
32 import org.apache.slide.projector.engine.Scheduler;
33 import org.apache.slide.projector.value.URIValue;
34 import org.apache.webdav.lib.Subscriber;
35
36 import de.zeigermann.xml.XMLStringWriter;
37 import de.zeigermann.xml.XMLWriter;
38
39 /**
40  * The EventExpression class
41  *
42  */

43 public class EventExpression implements Expression, Subscriber {
44     public final static String JavaDoc URI = "uri";
45     public final static String JavaDoc DEPTH = "depth";
46     
47     private String JavaDoc method;
48     private Map JavaDoc properties = new HashMap JavaDoc();
49     private boolean eval = false;
50     private Job job;
51
52     public EventExpression(String JavaDoc method) {
53         this.method = method;
54     }
55
56     public void addProperty(String JavaDoc key, String JavaDoc value) {
57         properties.put(key, value);
58     }
59     
60     public Expression activate(Job job) {
61         EventExpression activatedExpression = new EventExpression(method);
62         activatedExpression.setJob(job);
63         activatedExpression.properties.putAll(properties);
64         String JavaDoc uri = (String JavaDoc)properties.get(URI);
65         int depth = 0;
66         String JavaDoc depthProperty = (String JavaDoc)properties.get(DEPTH);
67         if ( depthProperty != null ) {
68             depth = Integer.valueOf(depthProperty).intValue();
69         }
70         Projector.getRepository().subscribe(method, new URIValue(uri), depth, activatedExpression, Projector.getCredentials());
71         return activatedExpression;
72     }
73
74     public void setJob(Job job) {
75         this.job = job;
76     }
77     
78     public boolean evaluate() {
79         return eval;
80     }
81
82     public void save(XMLStringWriter writer) {
83         if ( eval ) {
84             new TrueExpression().save(writer);
85         } else {
86             writer.writeStartTag(XMLWriter.createStartTag("event", "method", method));
87             for ( Iterator JavaDoc i = properties.entrySet().iterator(); i.hasNext(); ) {
88                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
89                 writer.writeEmptyElement(XMLWriter.createEmptyTag("property", new String JavaDoc[] { "key", "value" }, new String JavaDoc[] { (String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue() }));
90             }
91             writer.writeEndTag(XMLWriter.createEndTag("event"));
92         }
93     }
94
95     public void notify(String JavaDoc uri, Map JavaDoc information) {
96         eval = true;
97         Projector.getRepository().unsubscribe(new URIValue((String JavaDoc)properties.get(URI)), this, Projector.getCredentials());
98         Scheduler.getInstance().notify(job);
99     }
100 }
Popular Tags