001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: XMLSerializerTask.java 8200 2006-04-05 06:39:43Z pelletib $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.ant.jonasbase;
025:
026: import java.io.File;
027: import java.io.FileNotFoundException;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030:
031: import org.w3c.dom.Document;
032:
033: import org.apache.tools.ant.BuildException;
034:
035: import org.objectweb.jonas_lib.xml.XMLSerializer;
036:
037: /**
038: * Allow to serialize a XML DOM structure
039: * @author Philippe Coq
040: */
041: public class XMLSerializerTask extends JTask implements BaseTaskItf {
042:
043: /**
044: * Info for the logger
045: */
046: private static final String INFO = "[XML Serializer] ";
047:
048: /**
049: * XML document to serialize
050: */
051: private Document xmlDoc = null;
052:
053: /**
054: * XML file name
055: */
056: private String fileName = null;
057:
058: /**
059: * Default constructor
060: */
061: public XMLSerializerTask() {
062: super ();
063: }
064:
065: /**
066: * Set the xml document to serialize
067: * @param xmlDoc xml document
068: */
069: public void setXmlDoc(Document xmlDoc) {
070: this .xmlDoc = xmlDoc;
071: }
072:
073: /**
074: * Set the xml filename
075: * @param fileName xml filename
076: */
077: public void setXmlFileName(String fileName) {
078: this .fileName = fileName;
079: }
080:
081: /**
082: * Check the properties
083: */
084: private void checkProperties() {
085: if (xmlDoc == null) {
086: throw new BuildException(INFO + "XML document is missing.");
087: }
088: if (fileName == null) {
089: throw new BuildException(INFO + "XML filename is missing.");
090: }
091:
092: }
093:
094: /**
095: * Execute this task
096: */
097: public void execute() {
098: checkProperties();
099:
100: // Path to JONAS_BASE
101: String jBaseConf = getDestDir().getPath() + File.separator
102: + "conf";
103: String xmlFile = jBaseConf + File.separator + fileName;
104:
105: // Serialize the XML document
106: FileOutputStream os;
107: try {
108: os = new FileOutputStream(xmlFile);
109: } catch (FileNotFoundException e) {
110: throw new BuildException(INFO + "XML filename " + xmlFile
111: + " is not valid", e);
112: }
113: XMLSerializer xmlSer = new XMLSerializer(xmlDoc);
114: try {
115: xmlSer.serialize(os);
116: } catch (IOException e) {
117: throw new BuildException(INFO
118: + "Error during serialization of " + xmlFile, e);
119: }
120:
121: }
122: }
|