View Javadoc
1   package eu.simuline.relana.sys;
2   
3   import eu.simuline.relana.model.ClassLocator;
4   import eu.simuline.relana.model.InstanceLocator;
5   
6   import eu.simuline.util.sgml.ParseExceptionHandler;
7   //import eu.simuline.util.sgml.AttributesImpl;
8   
9   import java.net.URL;
10  import java.net.MalformedURLException;
11  
12  import java.util.Set;
13  import java.util.HashSet;
14  
15  import org.xml.sax.ContentHandler;
16  import org.xml.sax.Locator;
17  import org.xml.sax.SAXException;
18  import org.xml.sax.Attributes;
19  
20  
21  /**
22   * Enables an xml parser to read a relana project file 
23   * like <code>src/test/resources/eu/simuline/relana/proj.rml</code> 
24   * serving both as {@link ContentHandler} and as {@link ParseExceptionHandler} 
25   * as it implements {@link ProjectDesc} to hold the result of reading, 
26   * i.e. the content. 
27   *
28   * Created: Thu Apr 28 22:03:26 2005
29   *
30   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
31   * @version 1.0
32   */
33  public final class Project 
34      implements ProjectDesc, ContentHandler, ParseExceptionHandler {
35  
36      /* --------------------------------------------------------------------- *
37       * fields                                                                *
38       * --------------------------------------------------------------------- */
39  
40      private URL library;
41      private ClassLocator baseClass;
42      private final Set<InstanceLocator> outputEffects;
43  
44      /* --------------------------------------------------------------------- *
45       * constructors                                                          *
46       * --------------------------------------------------------------------- */
47  
48      public Project() {
49  	// InstanceLocator overwrites hashCode and is immutable 
50  	this.outputEffects = new HashSet<InstanceLocator>();
51      } // Project constructor
52  
53      /* --------------------------------------------------------------------- *
54       * getter methods implementing ProjectDesc                               *
55       * --------------------------------------------------------------------- */
56   
57  // Implementation of eu.simuline.relana.sys.ProjectDesc
58  
59      public URL getLibrary() {
60  	return this.library;
61      }
62  
63      public ClassLocator getBaseClass() {
64  	return this.baseClass;
65      }
66  
67      public Set<InstanceLocator> getOutputEffects() {
68  	return this.outputEffects;
69      }
70  
71      /* --------------------------------------------------------------------- *
72       * methods implementing ContentHandler                                   *
73       * --------------------------------------------------------------------- */
74  
75      public void setDocumentLocator(Locator locator) {
76        // is empty. 
77      }
78  
79      public void startDocument() throws SAXException {
80        // is empty. 
81      }
82  
83      public void endDocument() throws SAXException {
84        // is empty. 
85      }
86  
87      public void startPrefixMapping(String prefix,
88  				   String uri)
89  	throws SAXException {
90        // is empty. 
91      }
92  
93      public void endPrefixMapping(String prefix)
94  	throws SAXException {
95        // is empty. 
96      }
97  
98      public void startElement(String namespaceURI,
99  			     String localName,
100 			     String qName,
101 			     Attributes atts)
102 	throws SAXException {
103 	if ("Rml".equals(qName)) {
104 	    this.baseClass = ClassLocator
105 		.getLocator(atts.getValue("baseClass"));
106 	    try {
107 		this.library = new URL(atts.getValue("library"));
108 	    } catch (MalformedURLException e) {
109 		throw new SAXException// NOPMD
110 		    ("Found malformed url \"" + atts.getValue("library") + 
111 		     "\". ");
112 	    }
113 	    return;
114 	}
115 	if ("Output".equals(qName)) {
116 	    InstanceLocator loc = InstanceLocator
117 		.getLocator(atts.getValue("effect"));
118 	    this.outputEffects.add(loc);
119 	}
120     }
121 
122     public void endElement(String namespaceURI,
123 			   String localName,
124 			   String qName)
125 	throws SAXException {
126 	// is empty. 
127 	//this.events.add("TE</" + qName + ">");
128     }
129 
130     public void characters(char[] chr,
131 			   int start,
132 			   int length)
133 	throws SAXException {
134 	// is empty. 
135     }
136 
137     public void ignorableWhitespace(char[] chr,
138 				    int start,
139 				    int length)
140 	throws SAXException {
141 	// is empty. 
142     }
143 
144     public void processingInstruction(String target,
145 				      String data)
146 	throws SAXException {
147 	//System.out.println("P<" + target + ">");
148 	// is empty. 
149     }
150 
151     public void skippedEntity(String name)
152 	throws SAXException {
153 	// is empty. 
154     }
155 
156     /* --------------------------------------------------------------------- *
157      * methods implementing ParseExceptionHandler                            *
158      * --------------------------------------------------------------------- */
159 
160     @edu.umd.cs.findbugs.annotations.SuppressWarnings
161 	(value = "UC_USELESS_OBJECT", 
162 	 justification = "use if activated ")
163     public void foundMultipleAttribute(String attrName,
164 				       Object oldAttrValue) {
165 	// StringBuilder res = new StringBuilder();
166 	// res.append("Found second value for attribute \"");
167 	// res.append(attrName);
168 	// res.append("\"; overwritten ");
169 	// if (oldAttrValue == AttributesImpl.NO_VALUE) {
170 	//     res.append("no value. ");
171 	// } else {
172 	//     res.append("old value \"");
173 	//     res.append(oldAttrValue);
174 	//     res.append('\"');
175 	// }
176 
177 	//this.events.add(res.toString());
178     }
179 
180 
181     public void foundIllegalCharInTag(char chr) {
182 	//this.events.add("exc: ill letter in tag: " + chr);
183     }
184 
185     public void foundCharAfterEndOfEndTag(char chr) {
186 	//this.events.add("exc: etter after eo EndTag: " + chr);
187     }
188 
189     public void foundUnexpectedEndOfDocument() {
190       // is empty. 
191     }
192 
193     /* --------------------------------------------------------------------- *
194      * further methods                                                       *
195      * --------------------------------------------------------------------- */
196 
197     public String toString() {
198 	StringBuilder res = new StringBuilder();
199 	res.append("\n<Rml library=\"");
200 	res.append(getLibrary().toString());
201 	res.append("\" baseClass=\"");
202 	res.append(getBaseClass().getPath().toString());
203 	res.append("\">\n</Rml>\n");
204 	return res.toString();
205     }
206 } // Project