001    /*
002     * Copyright (C) 2003-2009 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.crsh.util.XML;
022    import org.xml.sax.Attributes;
023    import org.xml.sax.SAXException;
024    import org.xml.sax.helpers.DefaultHandler;
025    
026    import javax.xml.transform.OutputKeys;
027    import javax.xml.transform.Transformer;
028    import javax.xml.transform.sax.SAXTransformerFactory;
029    import javax.xml.transform.sax.TransformerHandler;
030    import javax.xml.transform.stream.StreamResult;
031    import java.io.ByteArrayInputStream;
032    import java.io.ByteArrayOutputStream;
033    import java.io.IOException;
034    import java.util.HashMap;
035    import java.util.Map;
036    
037    /**
038     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
039     * @version $Revision$
040     */
041    public class Exporter extends DefaultHandler {
042    
043      /** . */
044      private final Map<String, String> mappings;
045    
046      /** . */
047      private FileSystem fs;
048    
049      public Exporter(FileSystem fs) {
050        this.mappings = new HashMap<String, String>();
051        this.fs = fs;
052      }
053    
054      @Override
055      public void startPrefixMapping(String prefix, String uri) throws SAXException {
056        mappings.put(prefix, uri);
057      }
058    
059      @Override
060      public void endPrefixMapping(String prefix) throws SAXException {
061        mappings.remove(prefix);
062      }
063    
064      @Override
065      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
066        try {
067          String fileName = XML.fileName(qName);
068          fs.startDirectory(fileName);
069    
070          //
071          ByteArrayOutputStream out = new ByteArrayOutputStream();
072          StreamResult streamResult = new StreamResult(out);
073    
074          //
075          SAXTransformerFactory tf = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
076    
077          //
078          TransformerHandler hd = tf.newTransformerHandler();
079          Transformer serializer = hd.getTransformer();
080          serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
081          serializer.setOutputProperty(OutputKeys.INDENT,"yes");
082          hd.setResult(streamResult);
083    
084          //
085          hd.startDocument();
086    
087          //
088          for (Map.Entry<String, String> mapping : mappings.entrySet()) {
089            String prefix = mapping.getKey();
090            hd.startPrefixMapping(prefix, mapping.getValue());
091          }
092    
093          //
094          hd.startElement(uri, localName, qName, attributes);
095          hd.endElement(uri, localName, qName);
096    
097          //
098          for (String prefix : mappings.keySet()) {
099            hd.endPrefixMapping(prefix);
100          }
101    
102          //
103          hd.endDocument();
104    
105          //
106          out.close();
107          byte[] content = out.toByteArray();
108          fs.file("crash__content.xml", content.length, new ByteArrayInputStream(content));
109        }
110        catch (Exception e) {
111          throw new SAXException(e);
112        }
113      }
114    
115      @Override
116      public void endElement(String uri, String localName, String qName) throws SAXException {
117        try {
118          String fileName = XML.fileName(qName);
119          fs.endDirectory(fileName);
120        }
121        catch (IOException e) {
122          throw new SAXException(e);
123        }
124      }
125    }