View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.core.curnit;
24  
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  import java.beans.Expression;
29  import java.beans.Statement;
30  import java.io.OutputStream;
31  
32  import net.sf.sail.core.beans.Pod;
33  
34  
35  /**
36   * Extends XMLEncoder to provide XML generation tailored to <b>Pod</b>s.
37   * 
38   * @author turadg
39   */
40  public class PodXMLEncoder extends SAILXMLEncoder {
41  	/**
42  	 * Logger for this class
43  	 */
44  	private static final Logger logger = Logger.getLogger(PodXMLEncoder.class
45  			.getName());
46  
47  	private Pod pod;
48  
49  	public PodXMLEncoder(Pod pod, OutputStream out) {
50  		super(out);
51  		this.pod = pod;
52  	}
53  
54  	public void writePod() {
55  		if (logger.isLoggable(Level.FINE)) {
56  			logger.fine("n===WRITING POD: " + pod);
57  		}
58  		writeObject(pod);
59  	}
60  
61  	@Override
62  	public void writeExpression(Expression oldExp) {
63  		Object expValue = null;
64  		try {
65  			expValue = oldExp.getValue();
66  		} catch (Exception e) {
67  			// TODO Auto-generated catch block
68  			logger.warning("exception: " + e);
69  		}
70  		if (logger.isLoggable(Level.FINER)) {
71  			logger.finer("expr: " + oldExp + " has value " + expValue);
72  		}
73  		if (expValue instanceof Curnit) {
74  			if (logger.isLoggable(Level.FINER)) {
75  				logger
76  						.finer("\\ skipping above because pods can't reference curnits");
77  			}
78  			return;
79  		}
80  		if (pod.isTransient(expValue)) {
81  			if (logger.isLoggable(Level.FINER)) {
82  				logger.finer("\\ skipping above because pod imports the value");
83  			}
84  			return;
85  		}
86  		super.writeExpression(oldExp);
87  	}
88  
89  	@Override
90  	public void writeStatement(Statement stm) {
91  		if (logger.isLoggable(Level.FINEST)) {
92  			logger.finest(pod + " " + stm);
93  		}
94  
95  		Object target = stm.getTarget();
96  		Object[] arguments = stm.getArguments();
97  
98  		if (target instanceof Pod && target != pod) {
99  			if (logger.isLoggable(Level.FINEST)) {
100 				logger.finest("ttskipped (target is different pod)");
101 			}
102 			return; // skip this statement
103 		}
104 
105 		// imports are always unary
106 		Object value = arguments[0];
107 
108 		if (value instanceof Pod && value != pod) {
109 			if (logger.isLoggable(Level.FINEST)) {
110 				logger.finest("ttskipped (value is different pod)");
111 			}
112 			return; // skip this statement
113 		}
114 
115 		if (pod.isTransient(value)) {
116 			if (logger.isLoggable(Level.FINEST)) {
117 				logger.finest("ttskipped (value is transient in this pod)");
118 			}
119 			return; // skip this statement
120 		}
121 
122 		if (logger.isLoggable(Level.FINEST)) {
123 			logger.finest("");
124 		}
125 		// otherwise, let the super do its thing
126 		super.writeStatement(stm);
127 	}
128 
129 }