001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle 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
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019/*
020 * To change this template, choose Tools | Templates
021 * and open the template in the editor.
022 */
023package pt.ua.dicoogle.DicomLog;
024
025import java.io.*;
026// SAX classes.
027
028//JAXP 
029import javax.xml.transform.*;
030import javax.xml.transform.stream.*;
031import javax.xml.transform.sax.*;
032
033
034import java.util.ArrayList;
035
036import org.xml.sax.Attributes;
037import org.xml.sax.SAXException;
038import org.xml.sax.helpers.*;
039
040import org.xml.sax.InputSource;
041import org.xml.sax.XMLReader;
042
043/**
044 *
045 * @author Luís A. Bastião Silva <bastiao@ua.pt>
046 */
047public class LogXML extends DefaultHandler
048{
049
050    //private final String filename = Platform.homePath() + "DICOM_Services_Log.xml";
051    private final String filename = "./DICOM_Services_Log.xml";
052
053    private LogDICOM logs = null;
054    private boolean logOn = false;
055    private String type = "";
056    private String date = "";
057    private String ae = "";
058    private String add = "";
059    private String params = "";
060
061    public LogXML()
062    {
063        logs = LogDICOM.getInstance();
064
065    }
066
067    @Override
068    public void startElement(String uri, String localName, String qName,
069            Attributes attr)
070    {
071        if (localName.equals("log"))
072        {
073            this.logOn = true;
074        } else if (this.logOn && !localName.equals(""))
075        {
076            //...
077            this.type = localName;
078            this.ae = this.resolveAttrib("ae", attr, localName);
079            this.date = this.resolveAttrib("date", attr, localName);
080            this.add = this.resolveAttrib("add", attr, localName);
081        }
082    }
083
084    @Override
085    public void endElement(String uri, String localName, String qName)
086    {
087
088        if (localName.equals("log"))
089        {
090            this.logOn = false;
091        } else if (!localName.equals(""))
092        {
093            logs.addLine(new LogLine(type, date, ae, add, params));
094        }
095    }
096
097    private String resolveAttrib(String attr, Attributes attribs, String defaultValue)
098    {
099        String tmp = attribs.getValue(attr);
100        return (tmp != null) ? (tmp) : (defaultValue);
101    }
102
103    public LogDICOM getXML()
104    {
105        try
106        {
107            File file = new File(filename);
108            if (!file.exists())
109            {
110
111                return logs;
112            }
113
114            InputSource src = new InputSource(new FileInputStream(file));
115            XMLReader r = null;
116            try
117            {
118                r = XMLReaderFactory.createXMLReader();
119            } catch (SAXException ex)
120            {
121            }
122            r.setContentHandler(this);
123            r.parse(src);
124            return logs;
125        } catch (IOException ex)
126        {
127        } catch (SAXException ex)
128        {
129        }
130        return null;
131    }
132
133    public void printXML() throws TransformerConfigurationException
134    {
135
136
137        FileOutputStream out = null;
138
139        try
140        {
141            out = new FileOutputStream(filename);
142            PrintWriter pw = new PrintWriter(out);
143            StreamResult streamResult = new StreamResult(pw);
144            SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
145            //      SAX2.0 ContentHandler.
146            TransformerHandler hd = tf.newTransformerHandler();
147            Transformer serializer = hd.getTransformer();
148            serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
149            serializer.setOutputProperty(OutputKeys.METHOD, "xml");
150            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
151            serializer.setOutputProperty(OutputKeys.STANDALONE, "yes");
152            hd.setResult(streamResult);
153            hd.startDocument();
154
155            //Get a processing instruction
156            //hd.processingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"mystyle.xsl\"");
157            AttributesImpl atts = new AttributesImpl();
158
159
160            //root element
161            hd.startElement("", "", "log", atts);
162
163            ArrayList<LogLine> list = logs.getLl();
164            atts.clear();
165            for (LogLine l : list)
166            {
167                atts.addAttribute("", "", "date", "", l.getDate());
168                atts.addAttribute("", "", "ae", "", l.getAe());
169                atts.addAttribute("", "", "add", "", l.getAdd());
170                atts.addAttribute("","","params","",l.getParams());
171
172                hd.startElement("", "", l.getType(), atts);
173                atts.clear();
174
175                hd.endElement("", "", l.getType());
176
177            }
178            hd.endElement("", "", "log");
179
180            hd.endDocument();
181
182        } catch (TransformerConfigurationException ex)
183        {
184        } catch (SAXException ex)
185        {
186        } catch (FileNotFoundException ex)
187        {
188        } finally
189        {
190            try
191            {
192                out.close();
193            } catch (IOException ex)
194            {
195            }
196        }
197
198    }
199}