View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.editors;
23  
24  import java.io.*;
25  import java.util.*;
26  
27  import org.apache.log4j.Logger;
28  import org.fosstrak.llrp.commander.util.*;
29  import org.llrp.ltkGenerator.generated.ParameterDefinition;
30  
31  /**
32  * ...
33  * @author zhanghao
34  *
35  */
36  public class ConfigurationModel {
37  	
38  	/**
39  	 * Log4j instance.
40  	 */
41  	private static Logger log = Logger.getLogger(ConfigurationModel.class);
42  	
43  	private static String baseProps[] = { "llrp", "<llrp:GET_READER_CONFIG>",
44  			"<llrp:AntennaID>", "<llrp:RequestedData>", "<llrp:GPIPortNum>",
45  			"<llrp:GPOPortNum>", "<llrp:ADD_ROSPEC>", "<llrp:ROSpec>",
46  			"<llrp:Priority>", "<llrp:CurrentState>" };
47  
48  	private ArrayList<String> reservedWords;
49  	
50  	private Properties properties;
51  	private String rootLogger;
52  	private String[] appenders;
53  	
54  	public ConfigurationModel(String configuration) throws IOException {
55  		properties = new Properties();
56  		properties.load(new ByteArrayInputStream(configuration.getBytes()));
57  		rootLogger = properties.getProperty("log4j.rootLogger");
58  		appenders = parseCategory(rootLogger);
59  
60  		reservedWords = new ArrayList<String>();
61  		
62  		List<Object> list = LLRP.getLlrpDefintion().getMessageDefinitionOrParameterDefinitionOrChoiceDefinition();
63  		for (Object o : list){
64  			if (o instanceof ParameterDefinition){
65  				ParameterDefinition pdef = (ParameterDefinition) o;
66  				reservedWords.add(pdef.getName());
67  				log.debug(pdef.getName());
68  			}
69  		}
70  		
71  	}
72  
73  	public List getCompletions(String prefix) {
74  		
75  		log.debug("Here55555");
76  		
77  		List completions = new LinkedList();
78  	
79  		for (int i = 0; i < appenders.length; i++) {
80  			if (testCompletion(appenders[i], prefix))
81  				completions.add(appenders[i]);
82  		}
83  		
84  		/*
85  		Iterator<String> reserve = reservedWords.iterator();
86  		while (reserve.hasNext()) {
87  			String word = reserve.next();
88  			if (testCompletion(word, prefix))
89  				completions.add(word);
90  		}
91  		*/
92  		
93  		for (int i = 0; i < baseProps.length; i++) {
94  			if (testCompletion(baseProps[i], prefix))
95  				completions.add(baseProps[i]);
96  		}
97  		
98  		return completions;
99  	}
100 
101 	private boolean testCompletion(String completion, String prefix) {
102 		return completion.toLowerCase().startsWith(prefix.toLowerCase())
103 				&& (completion.lastIndexOf(":") == prefix.lastIndexOf(":"));
104 	}
105 
106 	private String[] parseCategory(String value) {
107 		List appenders = new LinkedList();
108 
109 		if (value != null) {
110 			StringTokenizer st = new StringTokenizer(value, ",");
111 
112 			if (!(value.startsWith(",") || value.equals(""))) {
113 				st.nextToken();
114 			}
115 
116 			while (st.hasMoreTokens()) {
117 				String appenderName = st.nextToken().trim();
118 				if (appenderName == null || appenderName.equals(","))
119 					continue;
120 				appenders.add("log4j.appender." + appenderName);
121 			}
122 		}
123 		return (String[]) appenders.toArray(new String[0]);
124 	}
125 }