001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    package org.crsh.jcr;
020    
021    import org.xml.sax.Attributes;
022    import org.xml.sax.SAXException;
023    import org.xml.sax.helpers.DefaultHandler;
024    
025    import javax.xml.transform.OutputKeys;
026    import javax.xml.transform.Transformer;
027    import javax.xml.transform.sax.SAXTransformerFactory;
028    import javax.xml.transform.sax.TransformerHandler;
029    import javax.xml.transform.stream.StreamResult;
030    import java.io.ByteArrayInputStream;
031    import java.io.ByteArrayOutputStream;
032    import java.io.IOException;
033    import java.util.HashMap;
034    import java.util.Map;
035    
036    public class Exporter extends DefaultHandler {
037    
038      /** . */
039      private final Map<String, String> mappings;
040    
041      /** . */
042      private FileSystem fs;
043    
044      public Exporter(FileSystem fs) {
045        this.mappings = new HashMap<String, String>();
046        this.fs = fs;
047      }
048    
049      @Override
050      public void startPrefixMapping(String prefix, String uri) throws SAXException {
051        mappings.put(prefix, uri);
052      }
053    
054      @Override
055      public void endPrefixMapping(String prefix) throws SAXException {
056        mappings.remove(prefix);
057      }
058    
059      @Override
060      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
061        try {
062          String fileName = XML.fileName(qName);
063          fs.beginDirectory(fileName);
064    
065          //
066          ByteArrayOutputStream out = new ByteArrayOutputStream();
067          StreamResult streamResult = new StreamResult(out);
068    
069          //
070          SAXTransformerFactory tf = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
071    
072          //
073          TransformerHandler hd = tf.newTransformerHandler();
074          Transformer serializer = hd.getTransformer();
075          serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
076          serializer.setOutputProperty(OutputKeys.INDENT,"yes");
077          hd.setResult(streamResult);
078    
079          //
080          hd.startDocument();
081    
082          //
083          for (Map.Entry<String, String> mapping : mappings.entrySet()) {
084            String prefix = mapping.getKey();
085            hd.startPrefixMapping(prefix, mapping.getValue());
086          }
087    
088          //
089          hd.startElement(uri, localName, qName, attributes);
090          hd.endElement(uri, localName, qName);
091    
092          //
093          for (String prefix : mappings.keySet()) {
094            hd.endPrefixMapping(prefix);
095          }
096    
097          //
098          hd.endDocument();
099    
100          //
101          out.close();
102          byte[] content = out.toByteArray();
103          fs.file("crash__content.xml", content.length, new ByteArrayInputStream(content));
104        }
105        catch (Exception e) {
106          throw new SAXException(e);
107        }
108      }
109    
110      @Override
111      public void endElement(String uri, String localName, String qName) throws SAXException {
112        try {
113          String fileName = XML.fileName(qName);
114          fs.endDirectory(fileName);
115        }
116        catch (IOException e) {
117          throw new SAXException(e);
118        }
119      }
120    }