001: /*
002:
003: * LIUS - Lucene Index Update and Search
004: * http://sourceforge.net/projects/lius/
005: *
006: * Copyright (c) 2005, Laval University Library. All rights reserved.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Lesser General Public
010: * License as published by the Free Software Foundation; either
011: * version 2.1 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public
019: * License along with this library; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package ca.ulaval.bibl.lius.util;
024:
025: /**
026: *
027: * @author Rida Benjelloun (rida.benjelloun@bibl.ulaval.ca)
028: *
029: */
030:
031: import java.io.FileInputStream;
032: import java.io.FileOutputStream;
033: import java.io.IOException;
034: import java.io.PrintWriter;
035: import java.util.List;
036: import java.util.Vector;
037:
038: import javax.xml.transform.Templates;
039:
040: import org.apache.log4j.Logger;
041: import org.jdom.Document;
042: import org.jdom.output.XMLOutputter;
043: import org.jdom.transform.JDOMSource;
044:
045: import ca.ulaval.bibl.lius.config.LiusField;
046:
047: public class LiusUtils {
048:
049: static Logger logger = Logger.getRootLogger();
050:
051: public static void printResults(List ls) {
052:
053: System.out.println("Nb doc = " + ls.size());
054:
055: for (int i = 0; i < ls.size(); i++) {
056:
057: List document = (List) ls.get(i);
058:
059: for (int j = 0; j < document.size(); j++) {
060:
061: LiusField lf = (LiusField) document.get(j);
062:
063: String name = lf.getLabel();
064:
065: Vector values = lf.getValues();
066:
067: System.out.print(name + " : ");
068:
069: for (int k = 0; k < values.size(); k++) {
070:
071: System.out.println("\t" + values.get(k));
072:
073: }
074:
075: }
076:
077: System.out.println("==============================");
078:
079: }
080:
081: }
082:
083: /**
084: *
085: * Permet d'enregistrer le Document XML JDOM dans un fichier.
086: *
087: * <br/><br/>
088: *
089: * Saves the XML JDOM Document in a file.
090: *
091: */
092:
093: public static void saveInXmlFile(Document doc, String file) {
094:
095: XMLOutputter xop = new XMLOutputter(" ", true);
096:
097: try {
098:
099: xop.output(doc, new FileOutputStream(file));
100:
101: }
102:
103: catch (IOException ex) {
104:
105: logger.error(ex.getMessage());
106:
107: }
108:
109: }
110:
111: public static void XslTransformation(Document xmlDoc, String xsl,
112:
113: PrintWriter out) {
114:
115: //response.setContentType("text/html");
116:
117: try {
118:
119: javax.xml.transform.TransformerFactory tFactory =
120:
121: javax.xml.transform.TransformerFactory.newInstance();
122:
123: javax.xml.transform.Source xmlSource = new JDOMSource(
124: xmlDoc);
125:
126: Templates template = tFactory
127: .newTemplates(new javax.xml.transform.stream.StreamSource(new
128:
129: FileInputStream(xsl)));
130:
131: javax.xml.transform.Transformer transformer = template
132: .newTransformer();
133:
134: transformer.transform(xmlSource,
135:
136: new javax.xml.transform.stream.StreamResult(out));
137:
138: }
139:
140: catch (Exception e) {
141:
142: out.write(e.getMessage());
143:
144: logger.error(e.getMessage());
145:
146: }
147:
148: out.close();
149:
150: }
151:
152: }
|