Revision 1137 tmp/org.txm.groovy.core/src/groovy/org/txm/scripts/importer/xtz/XTZPager.groovy

XTZPager.groovy (revision 1137)
12 12
import org.txm.utils.i18n.*
13 13
import org.txm.importer.xtz.*
14 14
import javax.xml.stream.*
15
import org.txm.objects.*
15 16

  
16 17
class XTZPager extends Pager {
17 18

  
18
	BaseParameters params;
19
	Project project;
19 20

  
20 21
	Element corpusElem;
21 22
	String lang;
......
28 29
	public XTZPager(ImportModule module) {
29 30
		super(module, "default");
30 31

  
31
		params = module.getParameters()
32
		project = module.getProject()
32 33

  
33
		corpusElem = params.corpora.get(corpusname);
34
		lang = corpusElem.getAttribute("lang");
35
		wordsPerPage = params.getWordsPerPage("default")
36
		page_element = params.getPageElement("default")
37
		wordTag = module.getParameters().getWordElement().getTextContent()
34
		lang = project.getLang();
35
		wordsPerPage = project.getEditionDefinition("default").getWordsPerPage()
36
		page_element = project.getEditionDefinition("default").getPageElement()
37
		wordTag = project.getTokenizerWordElement()
38 38

  
39 39
		cssDirectory = new File(module.getSourceDirectory(), "css")
40 40
		jsDirectory = new File(module.getSourceDirectory(), "js")
......
59 59

  
60 60
	public boolean doDefaultEditionStep() {
61 61

  
62
		boolean build_edition = module.getParameters().getDoEdition("default")
62
		boolean build_edition = project.getEditionDefinition("default").getBuildEdition()
63 63
		if (!build_edition) {
64 64
			return true;
65 65
		}
......
94 94
				List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
95 95
				List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
96 96

  
97
				Element texts = params.getTextsElement(corpusElem);
98
				Element text = params.getTextElement(texts, textname);
99
				if (text == null) { // just in case
100
					text = params.addText(corpusElem, textname, txmFile)
97
				Text t = project.getText(textname);
98
				if (t == null) {
99
					t = new Text(project);
101 100
				}
101
				t.setName(textname);
102
				t.setSourceFile(txmFile)
103
				t.setTXMFile(txmFile)
104

  
102 105
				def ed = new XTZDefaultPagerStep(this, txmFile, textname, NoSpaceBefore, NoSpaceAfter, css);
103 106
				if (!ed.process()) {
104 107
					println "Fail to build 'default' edition for text: $txmFile"
105 108
					continue;
106 109
				}
107
				Element edition = params.getEditionElement(text, "default");
110
				Edition edition = t.getEdition("default")
108 111
				if (edition == null) {
109
					edition = params.addEdition(text, "default", outputDirectory.getAbsolutePath(), "html");
110
				} else { // remove existing pages if any
111
					def children = edition.getChildNodes();
112
					while (children.getLength() > 0 ) {
113
						edition.removeChild(children.item(0))
114
					}
112
					edition = new Edition(t);
115 113
				}
114
				edition.setName("default");
115
				edition.setIndex(outputDirectory.getAbsolutePath());
116 116

  
117 117
				for (i = 0 ; i < ed.getPageFiles().size();) {
118 118
					File f = ed.getPageFiles().get(i);
119 119
					String wordid = ed.getIdx().get(i);
120
					params.addPage(edition, ""+(++i), wordid);
120
					edition.addPage(""+(++i), wordid);
121 121
				}
122 122
			} catch(Exception e) {
123 123
				println "Error: could not create $txmFile 'default' edition: "+e
......
149 149

  
150 150
	public boolean doFacsEditionStep() {
151 151

  
152
		boolean mustBuildFacsEdition = module.getParameters().getDoFacsEdition();
152
		boolean mustBuildFacsEdition = project.getEditionDefinition("facs").getBuildEdition()
153 153
		if (!mustBuildFacsEdition) return true;
154 154

  
155
		String imageDirectoryPath = module.getParameters().getFacsEditionImageDirectory().trim();
155
		String imageDirectoryPath = project.getEditionDefinition("facs").getImagesDirectory().trim();
156 156
		File imageDirectory = new File(imageDirectoryPath);
157 157
		if (!imageDirectoryPath.startsWith("http") && imageDirectoryPath.length()== 0 && !imageDirectory.exists() && !imageDirectory.isDirectory()) {
158 158
			imageDirectory = null;
......
164 164
		File newEditionDirectory = new File(htmlDirectory, "facs");
165 165
		newEditionDirectory.mkdir();
166 166

  
167
		// declare the "facs" edition
168
		Element editionDefinitionElement = params.getEditionDefinitionElement(corpusElem, "facs");
169
		if (editionDefinitionElement == null) {
170
			editionDefinitionElement = params.addEditionDefinition(corpusElem, "facs", "groovy", "XTZPager.groovy")
171
		}
172
		Element textsElement = params.getTextsElement(corpusElem)
173

  
174 167
		ConsoleProgressBar cpb = new ConsoleProgressBar(files.size())
175 168
		for (File txmFile : files) {
176 169
			cpb.tick()
......
184 177
			List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
185 178
			List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
186 179

  
187
			// the text should already exists
188
			Element text = params.getTextElement(textsElement, txtname)
189
			if (text == null) { // just in case
190
				text = params.addText(corpusElem, txtname, txmFile)
180
			Text t = project.getText(txtname);
181
			if (t == null) {
182
				t = new Text(project);
191 183
			}
192
			Element edition = params.getEditionElement(text, "facs");
184
			t.setName(txtname);
185
			t.setSourceFile(txmFile)
186
			t.setTXMFile(txmFile)
187

  
188
			Edition edition = t.getEdition("facs")
193 189
			if (edition == null) {
194
				edition = params.addEdition(text, "facs", newEditionDirectory.getAbsolutePath(), "html");
195
			} else {
196
				def children = edition.getChildNodes();
197
				while (children.getLength() > 0 ) {
198
					edition.removeChild(children.item(0))
199
				}
190
				edition = new Edition(t);
200 191
			}
192
			edition.setName("facs");
193
			edition.setIndex(outputDirectory.getAbsolutePath());
201 194

  
202 195
			try {
203 196
				def ed = new XTZFacsPagerStep(txmFile, newEditionDirectory, imageDirectory, txtname, corpusname, "pb", "facs", wordTag, debug);
......
213 206
					String wordid = pages[i][1]
214 207
					//TODO replace '""+(++i)' with something that fetch/findout the page 'name'
215 208
					// TODO or move the Edition and Page corpus declaration in the XTZDefaultPagerStep
216
					params.addPage(edition, ""+(++i), wordid);
209
					edition.addPage(""+(++i), wordid);
217 210
					//println "add facs page: $f $wordid"
218 211
				}
219 212
			} catch (Exception e) {
......
252 245
			FileCopy.copyFiles(imagesDirectory, imageshtmlDirectory)
253 246
		}
254 247

  
255
		Element editionsElement = params.getEditionsElement(corpusElem);
256
		editionsElement.setAttribute("default","default,facs");
248
		project.setDefaultEditionName("default,facs");
257 249

  
258 250
		println ""
259 251
		return true;
......
269 261
		if (xslDirectory.exists()) {
270 262

  
271 263
			// prepare XSL parameters
272
			def xslParams = module.getParameters().getXsltParams(corpusElem);
273
			String s = module.getParameters().getWordsPerPage("default");
264
			def xslParams = module.getProject().getXsltParameters();
265
			String s = module.getProject().getEditionDefinition("default").getWordsPerPage()
274 266
			if (s != null && s.length() > 0)
275 267

  
276 268
				// shared XSL parameters
277 269
				xslParams["number-words-per-page"] = Integer.parseInt(s);
278
			xslParams["pagination-element"] = module.getParameters().getPageElement("default")
279
			xslParams["import-xml-path"] = module.getParameters().paramFile.toURI().toString()
270
			xslParams["pagination-element"] = module.getProject().getEditionDefinition("default").getPageElement()
271
			xslParams["import-xml-path"] = module.getProject().getProjectDirectory()
280 272
			//println "XSL PARAMS: "+xslParams
281 273

  
282 274
			def xslFiles = xslDirectory.listFiles()
......
360 352
			ConsoleProgressBar cpb = new ConsoleProgressBar(editionsCreated.keySet().size())
361 353
			for (String editionName : editionsCreated.keySet()) {
362 354
				cpb.tick()
363
				//ensure edition definition is declared
364
				def editionsElement = params.getEditionsElement(corpusElem)
365
				def e = params.getEditionDefinitionElement(editionsElement, editionName);
366
				if (e == null) {
367
					params.addEditionDefinition(corpusElem, editionName, "xsl", editionsCreated[editionName].toString());
368
				}
369 355

  
370 356
				File newEditionDirectory = new File(htmlDirectory, editionName);
371 357
				File xslFile = editionsCreated[editionName]
......
375 361
					int idx4 = textName.indexOf(".")
376 362
					if (idx4 > 0) textName = textName.substring(0, idx4);
377 363

  
378
					getFirstWordIDs(textName, editionName, newEditionDirectory, xslFile);
364
					getFirstWordIDs(textName, editionName, newEditionDirectory, xslFile, txmFile);
379 365
				}
380 366
			}
381 367
			println ""
......
383 369
		return true;
384 370
	}
385 371

  
386
	private void getFirstWordIDs(String textName, String editionName, File newEditionDirectory, File xslFile) {
372
	private void getFirstWordIDs(String textName, String editionName, File newEditionDirectory, File xslFile, File txmFile) {
387 373
		//		println "call getFirstWordIDs textName=$textName editionName=$editionName dir=$newEditionDirectory xsl=$xslFile"
388
		Element textsElement = params.getTextsElement(corpusElem)
389
		Element textElement = params.getTextElement(textsElement, textName)
390
		if (textElement == null) { // just in case
391
			textElement = params.addText(corpusElem, textName, new File(inputDirectory, textName+".xml"))
374
		Text t = project.getText(textName);
375
		if (t == null) {
376
			t = new Text(project);
392 377
		}
378
		t.setName(textName);
379
		t.setSourceFile(txmFile)
380
		t.setTXMFile(txmFile)
393 381

  
394
		Element editionElement = params.getEditionElement(textElement, editionName);
395
		if (editionElement == null) { // the edition does not exist, declare it
396
			editionElement = params.addEdition(textElement, editionName, newEditionDirectory.getAbsolutePath(), "html");
397
		} else { // the edition already exists, remove pages from the existing edition
398
			def children = editionElement.getChildNodes();
399
			while (children.getLength() > 0 ) {
400
				editionElement.removeChild(children.item(0))
401
			}
382
		Edition edition = t.getEdition(editionName)
383
		if (edition == null) {
384
			edition = new Edition(t);
402 385
		}
403
		editionElement.setAttribute("mode", "xsl");
404
		editionElement.setAttribute("script", xslFile.getName());
386
		edition.setName(editionName);
387
		edition.setIndex(outputDirectory.getAbsolutePath());
405 388

  
406 389
		LinkedHashMap<File, String> words = new LinkedHashMap<File, String>()
407 390
		def files = []
......
420 403
			if (pagename.startsWith(textName+"_")) { // this is a page
421 404
				String firstWordID = getMetaContent(f);
422 405
				pagename = pagename.substring((textName+"_").length(), pagename.indexOf(".html")) // !!!!
423
				params.addPage(editionElement, pagename, firstWordID)
406
				edition.addPage(pagename, firstWordID)
424 407
			}
425 408
		}
426 409
	}

Also available in: Unified diff