View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.vafer.jdeb.descriptors;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.InputStreamReader;
22  import java.io.StringReader;
23  import java.text.ParseException;
24  import java.util.HashMap;
25  import java.util.HashSet;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.vafer.jdeb.utils.Utils;
30  import org.vafer.jdeb.utils.VariableResolver;
31  
32  /**
33   * A descriptor holds the usual key value pairs.
34   *
35   * @see <a href="http://www.debian.org/doc/debian-policy/ch-controlfields.html">Debian Policy Manual - Control files and their fields</a>
36   * 
37   * @author Torsten Curdt <tcurdt@vafer.org>
38   */
39  public abstract class AbstractDescriptor {
40  	
41  	private final Map values = new HashMap();
42  	private final VariableResolver resolver;
43  	
44  	public AbstractDescriptor( final VariableResolver pResolver ) {
45  		resolver = pResolver;
46  	}
47  
48  	public AbstractDescriptor( final AbstractDescriptor pDescriptor ) {
49  		values.putAll(pDescriptor.values);
50  		resolver = pDescriptor.resolver;
51  	}
52  
53  	protected void parse( final InputStream pInput ) throws IOException, ParseException {
54  		final BufferedReader br = new BufferedReader(new InputStreamReader(pInput));
55  		StringBuffer buffer = new StringBuffer();
56  		String key = null;
57  		int linenr = 0;
58  		while(true) {
59  			final String line = br.readLine();
60  			
61  			if (line == null) {
62  				if (buffer.length() > 0) {
63  					// flush value of previous key
64  					set(key, buffer.toString());
65  					buffer = null;
66  				}
67  				break;
68  			}
69  
70  			linenr++;
71  
72  			if (line.length() == 0) {
73  				throw new ParseException("Empty line", linenr);
74  			}
75  			
76  			final char first = line.charAt(0); 
77  			if (Character.isLetter(first)) {
78  				
79  				// new key
80  				
81  				if (buffer.length() > 0) {
82  					// flush value of previous key
83  					set(key, buffer.toString());
84  					buffer = new StringBuffer();
85  				}
86  				
87  				
88  				final int i = line.indexOf(':');
89  				
90  				if (i < 0) {
91  					throw new ParseException("Line misses ':' delimitter", linenr);
92  				}
93  				
94  				key = line.substring(0, i);
95  				buffer.append(line.substring(i+1).trim());
96  				
97  				continue;
98  			}
99  			
100 			// continuing old value
101 			buffer.append('\n').append(line.substring(1));
102 		}
103 		br.close();
104 		
105 	}
106 	
107 	public void set( final String pKey, final String pValue ) {
108 
109 		if (resolver != null) {
110 			try {
111 				values.put(pKey, Utils.replaceVariables(resolver, pValue, "[[", "]]"));
112 				return;
113 			} catch (ParseException e) {
114 				// FIXME maybe throw an Exception?
115 			}
116 		}
117 		
118 		values.put(pKey, pValue);
119 	}
120 	
121 	public String get( final String pKey ) {
122 		return (String)values.get(pKey);
123 	}
124 
125 	public abstract String[] getMandatoryKeys();
126 	
127 	public boolean isValid() {
128 		return invalidKeys().size() == 0;
129 	}
130 	
131 	public Set invalidKeys() {
132 		final Set invalid = new HashSet();
133 
134 		final String[] mk = getMandatoryKeys();
135 		for (int i = 0; i < mk.length; i++) {
136 			if (get(mk[i]) == null) {
137 				invalid.add(mk[i]);
138 			}
139 		}
140 		
141 		return invalid;
142 	}
143 	
144 	String toString( final String[] pKeys ) {
145 		final StringBuffer s = new StringBuffer();
146 		for (int i = 0; i < pKeys.length; i++) {
147 			final String key = pKeys[i];
148 			final String value = (String) values.get(key);
149 			if (value != null) {
150 				s.append(key).append(":");
151 
152 				try {
153 					BufferedReader reader = new BufferedReader(new StringReader(value));
154 					String line;
155 					while ((line = reader.readLine()) != null) {
156 						if (line.length() != 0 && !Character.isWhitespace(line.charAt(0))) {
157 							s.append(' ');
158 						}
159 
160 						s.append(line).append('\n');
161 					}
162 				} catch (IOException e) {
163 					e.printStackTrace();
164 				}
165 			}			
166 		}
167 		return s.toString();
168 	}
169 }