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

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

  
16 17
import org.txm.objects.*
17 18
import org.txm.importer.ApplyXsl2
19
import org.txm.utils.logger.Log
18 20

  
19 21
class XTZPager extends Pager {
20 22

  
......
43 45
		imagesDirectory = new File(module.getSourceDirectory(), "images")
44 46
	}
45 47

  
46
	public void process(ArrayList<File> files) {
47
		super.process(files);
48
	@Override
49
	public void process(List<String> orderedTextIDs) {
50
		super.process(orderedTextIDs);
48 51

  
49
		if (files == null) {
50
			files = inputDirectory.listFiles();
51
			if (files != null) Collections.sort(files);
52
		}
52
		if (orderedTextIDs == null) { module.getProject().getTextsID() }
53 53

  
54 54
		if (!doDefaultEditionStep()) return;
55 55
		if (!doFacsEditionStep()) return;
56
		
57
		// remove extra XSL editions -> they will be recreated by the doPostEditionXSLStep call 
56

  
57
		// remove extra XSL editions -> they will be recreated by the doPostEditionXSLStep call
58 58
		for (EditionDefinition eDef : project.getEditionDefinitions()) {
59 59
			if (eDef.getName() != "facs" && eDef.getName() != "default") {
60 60
				eDef.delete();
......
74 74
		}
75 75

  
76 76
		def second = 0
77
		def texts = module.getProject().getTexts()
78
		println "-- Building 'default' edition of  ${texts.size()} texts..."
77 79

  
78
		println "-- Building 'default' edition of  ${files.size()} texts..."
79

  
80 80
		def css = ["css/txm.css", "css/${corpusname}.css"] // default CSS inclusion
81 81

  
82 82
		// scan existing css files that must be declared in each HTML page
83 83
		if (cssDirectory.exists()) {
84 84
			def cssFiles = cssDirectory.listFiles();
85
			if (cssFiles != null)
85
			if (cssFiles != null) {
86 86
				for (File cssFile : cssFiles) {
87 87
					if (cssFile.isFile() && !cssFile.isHidden() && cssFile.getName().endsWith(".css"))
88 88
						css << "css/"+cssFile.getName();
89 89
				}
90
			}
90 91
		}
91 92

  
92
		ConsoleProgressBar cpb = new ConsoleProgressBar(files.size())
93
		for (File txmFile : files) {
93
		ConsoleProgressBar cpb = new ConsoleProgressBar(texts.size())
94
		for (Text text : texts) {
95

  
96
			File txmFile = text.getXMLTXMFile()
94 97
			try {
95 98
				cpb.tick()
96
				String textname = txmFile.getName();
97
				int i = textname.lastIndexOf(".");
98
				if (i > 0) textname = textname.substring(0, i);
99 99

  
100
				String textname = text.getName()
101

  
100 102
				File firstHTMLPageFile = new File(outputDirectory, textname+"_1.html");
101
				if (firstHTMLPageFile.exists() && firstHTMLPageFile.lastModified() >= txmFile.lastModified()) continue;
103
				boolean mustBuild = false;
104
				if (!firstHTMLPageFile.exists() || txmFile.lastModified() >= firstHTMLPageFile.lastModified()) {
105
					mustBuild = true
106
				}
107
				
108
				if (!text.isDirty() && !mustBuild) {
109
					Log.finer("skipping 'default html' step of $text");
110
					continue
111
				}
102 112

  
113
				Edition edition = text.getEdition("default")
114
				if (edition != null) {
115
					edition.delete()
116
				}
117
				
118
				edition = new Edition(text);
119

  
103 120
				List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
104 121
				List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
105 122

  
106
				Text t = project.getText(textname);
107
				if (t == null) {
108
					t = new Text(project);
109
				}
110
				t.setName(textname);
111
				t.setSourceFile(txmFile)
112
				t.setTXMFile(txmFile)
113 123

  
114 124
				def ed = new XTZDefaultPagerStep(this, txmFile, textname, NoSpaceBefore, NoSpaceAfter, css);
115 125
				if (!ed.process()) {
116 126
					println "Fail to build 'default' edition for text: $txmFile"
117 127
					continue;
118 128
				}
119
				Edition edition = t.getEdition("default")
120
				if (edition == null) {
121
					edition = new Edition(t);
122
				}
129
				
123 130
				edition.setName("default");
124 131
				edition.setIndex(outputDirectory.getAbsolutePath());
125
				
126
				for (i = 0 ; i < ed.getPageFiles().size();) {
132

  
133
				for (int i = 0 ; i < ed.getPageFiles().size();) {
127 134
					File f = ed.getPageFiles().get(i);
128 135
					String wordid = "w_0";
129 136
					if (i < ed.getIdx().size()) wordid = ed.getIdx().get(i);
......
165 172

  
166 173
		String imageDirectoryPath = project.getEditionDefinition("facs").getImagesDirectory();
167 174
		File imageDirectory = null
168
		
175

  
169 176
		if (imageDirectoryPath != null) {
170 177
			imageDirectoryPath = imageDirectoryPath.trim()
171 178
			imageDirectory = new File(imageDirectoryPath)
......
176 183

  
177 184
		def second = 0
178 185

  
179
		println "-- Building 'facs' edition of ${files.size()} texts..."
186
		def texts = module.getProject().getTexts()
187
		println "-- Building 'facs' edition of ${texts.size()} texts..."
180 188
		File newEditionDirectory = new File(htmlDirectory, "facs");
181 189
		newEditionDirectory.mkdir();
182 190

  
183
		ConsoleProgressBar cpb = new ConsoleProgressBar(files.size())
184
		for (File txmFile : files) {
191
		ConsoleProgressBar cpb = new ConsoleProgressBar(texts.size())
192
		for (Text text : texts) {
185 193
			cpb.tick()
186
			String txtname = txmFile.getName();
187
			int i = txtname.lastIndexOf(".");
188
			if (i > 0) txtname = txtname.substring(0, i);
189 194

  
195
			File txmFile = text.getXMLTXMFile()
196
			String txtname = text.getName()
197

  
190 198
			File firstHTMLPageFile = new File(newEditionDirectory, txtname+"_1.html");
191
			if (firstHTMLPageFile.exists() && firstHTMLPageFile.lastModified() >= txmFile.lastModified()) continue;
199
			boolean mustBuild = false;
200
			if (!firstHTMLPageFile.exists() || txmFile.lastModified() >= firstHTMLPageFile.lastModified()) {
201
				mustBuild = true
202
			}
203
			
204
			if (!text.isDirty() && !mustBuild) {
205
				Log.finer("skipping 'default html' step of $text");
206
				continue
207
			}
192 208

  
209
			Edition edition = text.getEdition("facs")
210
			if (edition != null) {
211
				edition.delete()
212
			}
213
			
214
			edition = new Edition(text);
215

  
193 216
			List<String> NoSpaceBefore = LangFormater.getNoSpaceBefore(lang);
194 217
			List<String> NoSpaceAfter = LangFormater.getNoSpaceAfter(lang);
195 218

  
196
			Text t = project.getText(txtname);
197
			if (t == null) {
198
				t = new Text(project);
199
			}
200
			t.setName(txtname);
201
			t.setSourceFile(txmFile)
202
			t.setTXMFile(txmFile)
203

  
204
			Edition edition = t.getEdition("facs")
205
			if (edition == null) {
206
				edition = new Edition(t);
207
			}
208 219
			edition.setName("facs");
209 220
			edition.setIndex(outputDirectory.getAbsolutePath());
210 221

  
......
216 227
				}
217 228

  
218 229
				def pages = ed.getPageFiles()
219
				for (i = 0 ; i < pages.size();) {
230
				for (int i = 0 ; i < pages.size();) {
220 231
					File f = pages[i][0];
221 232
					String wordid = pages[i][1]
222 233
					//TODO replace '""+(++i)' with something that fetch/findout the page 'name'
......
231 242
			}
232 243
		}
233 244

  
234
		
245

  
235 246
		if (!imageDirectoryPath.startsWith("http") && imageDirectory != null) { // copy files only if local
236 247
			File editionImagesDirectory = new File(newEditionDirectory, "res/images/"+corpusname+"/facs");
237 248
			editionImagesDirectory.mkdirs();
......
281 292
			String s = project.getEditionDefinition("default").getWordsPerPage();
282 293
			if (s != null && s.length() > 0)
283 294

  
284
			// shared XSL parameters
295
				// shared XSL parameters
285 296
			xslParams["number-words-per-page"] = Integer.parseInt(s);
286 297
			xslParams["pagination-element"] = project.getEditionDefinition("default").getPageElement();
287 298
			xslParams["import-xml-path"] = project.getProjectDirectory()
288 299
			//println "XSL PARAMS: "+xslParams
289 300

  
290 301
			def xslFiles = xslDirectory.listFiles()
291
			xslFiles = xslFiles.sort() { f -> 
302
			xslFiles = xslFiles.sort() { f ->
292 303
				try {
293
				return Integer.parseInt(f.getName().substring(0, f.getName().indexOf("-")))
304
					return Integer.parseInt(f.getName().substring(0, f.getName().indexOf("-")))
294 305
				} catch(Exception e) {}
295 306
				return -1;
296 307
			}
......
323 334

  
324 335
				if (editionsCreated[editionName] == null) { // first XSL, replace an edition
325 336
					editionsCreated[editionName] = xslFile
326
					//if (!importModule.isUpdatingCorpus()) {
327
					//TODO: optimisation if update is enable,
337

  
328 338
					newEditionDirectory.deleteDir(); // delete previous edition if any
329
					//}
330 339
					newEditionDirectory.mkdir()
331 340

  
332 341
					boolean deleteOutputFiles = "pager" == pagerStep;
342

  
333 343
					if (ApplyXsl2.processImportSources(xslFile, inputDirectory, newEditionDirectory, xslParams, deleteOutputFiles)) {
334 344
						println ""
335 345
					} else {
......
384 394

  
385 395
					getFirstWordIDs(textName, editionName, newEditionDirectory, xslFile, txmFile);
386 396
				}
387
				
397

  
388 398
				def editionDeclaration = project.getEditionDefinition(editionName); // create the edition definition
389 399
				editionDeclaration.setBuildEdition(true)
390 400
				editionDeclaration.setPageBreakTag(project.getEditionDefinition("default").getPageElement())

Also available in: Unified diff