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.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 public class Exporter extends DefaultHandler {
038
039 /** . */
040 private final Map<String, String> mappings;
041
042 /** . */
043 private FileSystem fs;
044
045 public Exporter(FileSystem fs) {
046 this.mappings = new HashMap<String, String>();
047 this.fs = fs;
048 }
049
050 @Override
051 public void startPrefixMapping(String prefix, String uri) throws SAXException {
052 mappings.put(prefix, uri);
053 }
054
055 @Override
056 public void endPrefixMapping(String prefix) throws SAXException {
057 mappings.remove(prefix);
058 }
059
060 @Override
061 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
062 try {
063 String fileName = XML.fileName(qName);
064 fs.beginDirectory(fileName);
065
066 //
067 ByteArrayOutputStream out = new ByteArrayOutputStream();
068 StreamResult streamResult = new StreamResult(out);
069
070 //
071 SAXTransformerFactory tf = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
072
073 //
074 TransformerHandler hd = tf.newTransformerHandler();
075 Transformer serializer = hd.getTransformer();
076 serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
077 serializer.setOutputProperty(OutputKeys.INDENT,"yes");
078 hd.setResult(streamResult);
079
080 //
081 hd.startDocument();
082
083 //
084 for (Map.Entry<String, String> mapping : mappings.entrySet()) {
085 String prefix = mapping.getKey();
086 hd.startPrefixMapping(prefix, mapping.getValue());
087 }
088
089 //
090 hd.startElement(uri, localName, qName, attributes);
091 hd.endElement(uri, localName, qName);
092
093 //
094 for (String prefix : mappings.keySet()) {
095 hd.endPrefixMapping(prefix);
096 }
097
098 //
099 hd.endDocument();
100
101 //
102 out.close();
103 byte[] content = out.toByteArray();
104 fs.file("crash__content.xml", content.length, new ByteArrayInputStream(content));
105 }
106 catch (Exception e) {
107 throw new SAXException(e);
108 }
109 }
110
111 @Override
112 public void endElement(String uri, String localName, String qName) throws SAXException {
113 try {
114 String fileName = XML.fileName(qName);
115 fs.endDirectory(fileName);
116 }
117 catch (IOException e) {
118 throw new SAXException(e);
119 }
120 }
121 }