Révision 3013

tmp/org.txm.searchengine.cqp.core/src/org/txm/searchengine/cqp/ReferencePattern.java (revision 3013)
32 32
import java.util.HashMap;
33 33
import java.util.Iterator;
34 34
import java.util.List;
35
import java.util.regex.Matcher;
36
import java.util.regex.Pattern;
35 37

  
36 38
import org.apache.commons.lang.StringUtils;
37 39
import org.txm.core.messages.TXMCoreMessages;
......
40 42
import org.txm.searchengine.cqp.corpus.StructuralUnit;
41 43
import org.txm.searchengine.cqp.corpus.StructuralUnitProperty;
42 44
import org.txm.searchengine.cqp.corpus.VirtualProperty;
45
import org.txm.searchengine.cqp.corpus.WordProperty;
43 46
import org.txm.utils.logger.Log;
44 47

  
45 48
import com.google.gson.Gson;
......
64 67
	 */
65 68
	@SuppressWarnings("unlikely-arg-type")
66 69
	public static ReferencePattern stringToReferencePattern(CQPCorpus corpus, String v) {
70
		ReferencePattern ref = fromGSonString(corpus, v);
71
		if (ref == null) {
72
			ref = fromPatternString(corpus, v);
73
		}
74
		if (ref == null) {
75
			ref = fromTabulatedString(corpus, v);
76
		}
77
		return ref;
78
	}
79
	
80
	/**
81
	 * 
82
	 * @param corpus
83
	 * @param v
84
	 * @return a list of word and structure properties read from a String
85
	 */
86
	@SuppressWarnings("unlikely-arg-type")
87
	public static ReferencePattern fromGSonString(CQPCorpus corpus, String v) {
67 88
		try {
68 89
			Gson gson = new Gson();
69

  
90
			
70 91
			HashMap<?, ?> values = gson.fromJson(v, HashMap.class);
71 92
			Object oProperties = values.get("properties");
72 93
			Object oFormat = values.get("format");
......
104 125
			return props;
105 126
		}
106 127
		catch (Exception e) {
107
			System.out.println("Error: " + v + " property not found in " + corpus);
128
			// System.out.println("Error: " + v + " property not found in " + corpus);
108 129
			return null;
109 130
		}
110 131
	}
111 132
	
112 133
	/**
134
	 * 
135
	 * @param corpus
136
	 * @param v "${strct_prop} like string"
137
	 * @return
138
	 */
139
	protected static ReferencePattern fromPatternString(CQPCorpus corpus, String v) {
140
		try {
141
			ReferencePattern props = new ReferencePattern();
142
			if (v == null) return props;
143
			if (v.length() == 0) return props;
144
			
145
			Pattern pattern = Pattern.compile("\\$\\{([^\\}]+)\\}");
146
			Matcher matcher = pattern.matcher(v);
147
			int nPropExpected = 0;
148
			while (matcher.find()) {
149
				nPropExpected++;
150
				String gg = matcher.group(1).trim();
151
				String[] split = gg.split("_");
152
				if (split.length == 2) {
153
					String sstruct = split[0];
154
					String sprop = split[1];
155
					StructuralUnit struct = corpus.getStructuralUnit(sstruct);
156
					if (struct != null) {
157
						StructuralUnitProperty prop = struct.getProperty(sprop);
158
						if (prop != null) {
159
							props.addProperty(prop);
160
						}
161
					}
162
				}
163
				else if (split.length == 1) {
164
					String sprop = split[0];
165
					WordProperty prop = corpus.getProperty(sprop);
166
					if (prop != null) {
167
						props.addProperty(prop);
168
					}
169
				}
170
			}
171
			
172
			if (nPropExpected > 0 && props.getProperties().size() == nPropExpected) {
173
				props.setPattern(matcher.replaceAll("%s"));
174
				return props;
175
			}
176
			else {
177
				return null;
178
			}
179
		}
180
		catch (Exception e) {
181
			// System.out.println("Error: " + v + " property not found in " + corpus);
182
			return null;
183
		}
184
	}
185
	
186
	protected static ReferencePattern fromTabulatedString(CQPCorpus corpus, String v) {
187
		try {
188
			ReferencePattern props = new ReferencePattern();
189
			if (v == null) return props;
190
			if (v.length() == 0) return props;
191
			
192
			String[] split = v.split("\t");
193
			for (String s : split) {
194
				s = s.trim();
195
				if (s.contains("_")) {
196
					String[] split2 = s.split("_", 2);
197
					StructuralUnit su = corpus.getStructuralUnit(split2[0]);
198
					if (su != null) {
199
						Property prop = su.getProperty(split2[1]);
200
						if (prop != null) {
201
							props.addProperty(prop);
202
						}
203
					}
204
				}
205
				else {
206
					Property prop = corpus.getProperty(s);
207
					if (prop != null) {
208
						props.addProperty(prop);
209
					}
210
				}
211
			}
212
			return props;
213
		}
214
		catch (Exception e) {
215
			// System.out.println("Error: " + v + " property not found in " + corpus);
216
			return null;
217
		}
218
	}
219
	
220
	/**
113 221
	 * Instantiates a new reference pattern.
114 222
	 */
115 223
	public ReferencePattern() {
......
254 362
		}
255 363
		if (title.length() == 0) {
256 364
			title = TXMCoreMessages.common_reference;
257
		} else {
365
		}
366
		else {
258 367
			title = title.substring(0, title.length() - 2);
259 368
		}
260 369
		return title;
......
267 376
		}
268 377
		
269 378
		Gson gson = new Gson();
270
		HashMap<String, Object> values = new HashMap<String, Object>();
379
		HashMap<String, Object> values = new HashMap<>();
271 380
		values.put("properties", names);
272 381
		values.put("format", pattern.getPattern());
273 382
		return gson.toJson(values);

Formats disponibles : Unified diff