View Javadoc

1   package pl.psnc.dl.ege.webapp.request;
2   
3   import java.io.ByteArrayInputStream;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.xml.sax.Attributes;
8   import org.xml.sax.InputSource;
9   import org.xml.sax.SAXException;
10  import org.xml.sax.XMLReader;
11  import org.xml.sax.helpers.DefaultHandler;
12  import org.xml.sax.helpers.XMLReaderFactory;
13  
14  import pl.psnc.dl.ege.types.ConversionsPath;
15  
16  /**
17   * Provides SAX parsing functionality - for decoding
18   * xml conversion properties.
19   * <br/><br/>
20   * Translates contained properties into format understandable 
21   * for converter. 
22   * 
23   * @author mariuszs
24   *
25   */
26  public class ConversionsPropertiesHandler
27  	extends DefaultHandler
28  {
29  
30  	Map<Integer, Map<String, String>> properties = new HashMap<Integer, Map<String, String>>();
31  
32  	private Integer currentIndex;
33  
34  	private String currentId;
35  
36  	/**
37  	 * Constructor.
38  	 * 
39  	 * @param xmlProperties - xml data as String
40  	 * @throws RequestResolvingException
41  	 */
42  	public ConversionsPropertiesHandler(String xmlProperties)
43  		throws RequestResolvingException
44  	{
45  		try {
46  			XMLReader xmlReader = XMLReaderFactory.createXMLReader();
47  			xmlReader.setContentHandler(this);
48  			if(xmlProperties != null){
49  				xmlReader.parse(new InputSource(new ByteArrayInputStream(
50  					xmlProperties.getBytes())));
51  			}
52  		}
53  		catch (Throwable e) {
54  			throw new RequestResolvingException(e.getMessage());
55  		}
56  	}
57  
58  
59  	@Override
60  	public void startElement(String uri, String localName, String name,
61  			Attributes attributes)
62  		throws SAXException
63  	{
64  		if (localName.equals("conversion")) {
65  			currentIndex = Integer.parseInt((attributes.getValue("index")));
66  			Map<String,String> props = properties.get(currentIndex);
67  			if(props!=null){
68  				throw new SAXException("Error reading properties: vertices with same index ("+currentIndex+") found!");
69  			}
70  			else{
71  				props = new HashMap<String,String>();
72  				properties.put(currentIndex,props);
73  			}
74  		}
75  		if (localName.equals("property")) {
76  			currentId = attributes.getValue("id");
77  		}
78  	}
79  
80  
81  	@Override
82  	public void characters(char buf[], int offset, int len)
83  		throws SAXException
84  	{
85  		StringBuffer val = new StringBuffer();
86  		for (int i = offset; i < offset + len; i++) {
87  			switch (buf[i]) {
88  				case '\\':
89  					break;
90  				case '"':
91  					break;
92  				case '\n':
93  					break;
94  				case '\r':
95  					break;
96  				case '\t':
97  					break;
98  				default:
99  					val.append(buf[i]);
100 					break;
101 			}
102 		}
103 		Map<String, String> props = properties.get(currentIndex);
104 		props.put(currentId, val.toString());
105 	}
106 
107 	/**
108 	 * Assigns properties to conversions path.<br/>
109 	 * Trying to assign properties to wrong conversions path will result in exception.
110 	 * 
111 	 * @param cp - conversions path
112 	 * @throws RequestResolvingException
113 	 */
114 	public void applyPathProperties(ConversionsPath cp) throws RequestResolvingException
115 	{
116 		for (int i = 0; i < cp.getPath().size(); i++) {
117 			cp.getPath().get(i).getConversionActionArguments().setProperties(
118 				properties.get(i));
119 		}
120 	}
121 
122 }