Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / bfm / BuildBiblio.groovy @ 187

History | View | Annotate | Download (5.9 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21

    
22
//
23
// This file is part of the TXM platform.
24
//
25
// The TXM platform is free software: you can redistribute it and/or modif y
26
// it under the terms of the GNU General Public License as published by
27
// the Free Software Foundation, either version 3 of the License, or
28
// (at your option) any later version.
29
//
30
// The TXM platform is distributed in the hope that it will be useful,
31
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
// GNU General Public License for more details.
34
//
35
// You should have received a copy of the GNU General Public License
36
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
37
// 
38
// 
39
// 
40
// $LastChangedDate:$
41
// $LastChangedRevision:$
42
// $LastChangedBy:$ 
43
//
44
package org.txm.importer.bfm;
45

    
46
import java.io.File;
47
import java.io.FileInputStream;
48
import java.io.InputStreamReader;
49
import java.nio.charset.Charset;
50
import org.txm.utils.*;
51

    
52
import javax.xml.stream.*;
53
import java.net.URL;
54

    
55
// TODO: Auto-generated Javadoc
56
/**
57
 * The Class BuildBiblio.
58
 */
59
class BuildBiblio {
60
        
61
        /**
62
         * Process.
63
         *
64
         * @param csvfile the csvfile
65
         * @param separator the separator
66
         * @param encoding the encoding
67
         * @param outputfile the outputfile
68
         * @param buildIndex the build index
69
         * @return true, if successful
70
         */
71
        public static boolean process(File csvfile, String separator, String encoding, File outputfile, boolean buildIndex) {
72
                if(!(csvfile.exists() && csvfile.canRead()))
73
                {
74
                        println "csvfile does not exist"
75
                        return false;
76
                }
77
                
78
                if(!outputfile.getParentFile().exists())
79
                {
80
                        println "outpurfile's parent does not exist"
81
                        return false;
82
                }
83
                
84
                CsvReader csvreader = new CsvReader(csvfile.getAbsolutePath(), separator.charAt(0), Charset.forName(encoding))
85
                
86
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
87
                FileOutputStream output = new FileOutputStream(outputfile)
88
                XMLStreamWriter writer = factory.createXMLStreamWriter(output, "UTF-8");
89
                
90
                if(csvreader == null)
91
                {
92
                        println "failed to build csvreader"
93
                        return false;
94
                }
95
                
96
                writer.writeStartDocument("UTF-8","1.0");
97
                writer.writeStartElement("html");
98
                //header
99
                writer.writeStartElement("head");
100
                //writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>");
101
                writer.writeStartElement("meta");
102
                writer.writeAttribute("http-equiv","Content-Type");
103
                writer.writeAttribute("content","text/html");
104
                writer.writeAttribute("charset","UTF-8");
105
                writer.writeEndElement();//meta
106
                writer.writeStartElement("title");
107
                writer.writeCharacters("Fiches biblio BFM")
108
                writer.writeEndElement();//title
109
                writer.writeEndElement();//head
110
                
111
                //get titles
112
                csvreader.readHeaders();
113
                def titles = [];
114
                for(String title : csvreader.getHeaders())
115
                {
116
                        titles << title
117
                }
118
                
119
                writer.writeStartElement("body");
120
                
121
                if(buildIndex)
122
                {
123
                writer.writeStartElement("ul");
124
                while (csvreader.readRecord()){
125
                        
126
                        writer.writeStartElement("li");
127
                        writer.writeStartElement("a");
128
                        writer.writeAttribute("href", "#"+csvreader.get("id"))
129
                        writer.writeCharacters(csvreader.get("id"))
130
                        writer.writeEndElement();//a
131
                        writer.writeEndElement();//li                        
132
                }
133
                writer.writeEndElement();//ul
134
                }
135
                
136
                println "Metadata declared: "+titles
137
                csvreader = new CsvReader(csvfile.getAbsolutePath(), separator.charAt(0), Charset.forName(encoding))
138
                csvreader.readHeaders();
139
                while (csvreader.readRecord()){
140
                        writer.writeStartElement("div");
141
                        writer.writeAttribute("class", "fiche");
142
                        
143
                        writer.writeStartElement("a")
144
                        writer.writeAttribute("name", csvreader.get("id"));
145
                        writer.writeEndElement();//a
146
                        
147
                        writer.writeStartElement("h1");
148
                        String auteur = csvreader.get("Auteur")
149
                        String titre = csvreader.get("Titre")
150
                        if(auteur != null && auteur != "anonyme")
151
                                writer.writeCharacters(auteur+", ")
152
                        writer.writeCharacters(titre);
153
                        writer.writeEndElement();//h1
154
                        
155
                        writer.writeStartElement("ul");
156
                        for (int i = 0 ; i < titles.size() ; i++) {
157
                                writer.writeStartElement("li");
158
                                writer.writeStartElement("b");
159
                                writer.writeCharacters(titles.get(i)+" : ")
160
                                writer.writeEndElement();
161
                                
162
                                writer.writeCharacters(csvreader.get(titles.get(i)))
163
                                writer.writeEndElement();//li
164
                        }
165
                        
166
                        writer.writeEndElement();//ul
167
                        writer.writeEndElement();//div
168
                }
169
                
170
                writer.writeEndElement();//body
171
                writer.close();
172
                output.close()
173
                return true;
174
        }
175
        
176
        /**
177
         * The main method.
178
         *
179
         * @param args the arguments
180
         */
181
        public static void main(String[] args)
182
        {
183
                File csvfile = new File("C:\\Documents and Settings\\alavrent\\Mes documents\\bfm - local\\Bases bibliographiques (copie)\\Fiches_biblio_TXM.txt")
184
                File outputfile = new File("C:\\Documents and Settings\\alavrent\\Mes documents\\bfm - local\\Bases bibliographiques (copie)\\Fiches_biblio_TXM.html")
185
                String separator = "\t"
186
                String encoding = "cp1252"
187
                
188
                if(!BuildBiblio.process(csvfile, separator, encoding, outputfile, false))
189
                        println("Error.")
190
                else
191
                        println "Success";
192
        }
193
}