Revision 2169

tmp/org.txm.analec.rcp/src/org/txm/macro/urs/democrat/TrimPropertiesMacro.groovy (revision 2169)
1
// Copyright © 2016 ENS de Lyon, CNRS, University of Franche-Comté
2
// Licensed under the terms of the GNU General Public License (http://www.gnu.org/licenses)
3
// @author mdecorde
4
// STANDARD DECLARATIONS
5
package org.txm.macro.urs.democrat
6

  
7
import org.kohsuke.args4j.*
8
import groovy.transform.Field
9
import org.txm.rcp.swt.widget.parameters.*
10
import org.txm.annotation.urs.*
11
import org.txm.macro.urs.AnalecUtils
12
import org.txm.searchengine.cqp.corpus.*
13
import visuAnalec.donnees.Corpus
14
import visuAnalec.donnees.Structure;
15
import visuAnalec.elements.*
16
import visuAnalec.vue.Vue
17

  
18
if (!(corpusViewSelection instanceof MainCorpus)) {
19
	println "Corpora selection is not a MainCorpus: "+corpusViewSelection
20
	return;
21
}
22

  
23
@Field @Option(name="element", usage="Unite|Relation|Schema", widget="StringArray", metaVar="Unite	Relation	Schema", required=true, def="Unite")
24
		String element
25
@Field @Option(name="ursql", usage="select the elements with a URSQL", widget="String", required=true, def="")
26
		String ursql
27
	
28
@Field @Option(name="replaceValues", usage="do replace the property values", widget="Boolean", required=true, def="false")
29
		def replaceValues
30
@Field @Option(name="trimProperties", usage="remove heading and trailing blanks", widget="Boolean", required=true, def="true")
31
		def trimProperties
32
@Field @Option(name="fixSpaces", usage="replace several blanks with one blank", widget="Boolean", required=true, def="true")
33
		def fixSpaces
34
		@Field @Option(name="fixNewLines", usage="replace", widget="Boolean", required=true, def="true")
35
		def fixNewLines
36
@Field @Option(name="debug", usage="Show internal variable content", widget="StringArray", metaVar="OFF	ON	ALL	REALLY ALL", required=true, def="OFF")
37
		debug
38
if (!ParametersDialog.open(this)) return
39

  
40
	if (debug == "OFF") debug = 0; else if (debug == "ON") debug = 1; else if (debug == "ALL") debug = 2 else if (debug == "REALLY ALL") debug = 3
41

  
42
int nCreated = 0 // count the number of created CHAINE
43
int nUpdated = 0 // count the number of updated CHAINE
44

  
45
CQPCorpus corpus = corpusViewSelection
46
Corpus analecCorpus = URSCorpora.getCorpus(corpus);
47

  
48
Class clazz = Unite.class;
49
def elements = null
50
if ("Schema".equals(element)) {
51
	clazz = Schema.class;
52
	elements = analecCorpus.getTousSchemas()
53
} else if ("Relation".equals(element)){
54
	clazz = Relation.class;
55
	elements = analecCorpus.getToutesRelations()
56
} else if ("Unite".equals(element)){
57
	clazz = Unite.class;
58
	elements = analecCorpus.getToutesUnites()
59
}
60

  
61
elements = AnalecUtils.filterElements(debug, elements, ursql)
62

  
63
println "Fixing ${elements.size()} $ursql $element"
64
int nUpdate = 0;
65
// update the already existing CHAINES schemas
66

  
67
	for (def e : elements) {
68
		def props = e.getProps()
69
		if (debug > 2) println "		e=$props"
70
		for (def k : props.keySet()) {
71
			String p = props[k]
72
			
73
			if (trimProperties) {
74
				p = p.trim()
75
			}
76
			if (fixNewLines) {
77
				p = p.replaceAll("\n", "")
78
			}
79
			if (fixSpaces) {
80
				p = p.replaceAll("[ ]++", " ")
81
			}
82
			
83
			if (p != props[k]) {
84
				nUpdate++
85
			}
86
			if (replaceValues) {
87
				props[k] = p
88
				if (debug > 3) println "			props[$k]=${props[k]} -> $p"
89
			} else if (p != props[k]) {
90
				println "			props[$k]=${props[k]} -> $p"
91
			}
92
		}
93
	}
94

  
95
println "Done: $nUpdate update(s)"

Also available in: Unified diff