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.Safe;
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024 import org.xml.sax.Attributes;
025 import org.xml.sax.ContentHandler;
026 import org.xml.sax.SAXException;
027 import org.xml.sax.helpers.DefaultHandler;
028
029 import javax.xml.parsers.SAXParser;
030 import javax.xml.parsers.SAXParserFactory;
031 import java.io.IOException;
032 import java.io.InputStream;
033 import java.util.ArrayList;
034 import java.util.LinkedList;
035 import java.util.List;
036
037 /**
038 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
039 * @version $Revision$
040 */
041 public class Importer implements FileSystem {
042
043 /** . */
044 private static final Logger log = LoggerFactory.getLogger(Importer.class);
045
046 /** . */
047 private final ContentHandler handler;
048
049 /** . */
050 private final LinkedList<EndElement> stack;
051
052 /** . */
053 private final List<String> prefixes;
054
055 /** . */
056 private final DefaultHandler attributesHandler = new DefaultHandler() {
057
058 @Override
059 public void startPrefixMapping(String prefix, String uri) throws SAXException {
060 if (stack.isEmpty()) {
061 log.debug("Adding prefix mapping " + prefix + " for " + uri);
062 handler.startPrefixMapping(prefix, uri);
063 prefixes.add(prefix);
064 }
065 }
066
067 @Override
068 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
069 log.debug("Creating element " + qName);
070 handler.startElement(uri, localName, qName, attributes);
071 stack.addLast(new EndElement(uri, localName, qName));
072 }
073 };
074
075 public Importer(ContentHandler handler) {
076 this.handler = handler;
077 this.stack = new LinkedList<EndElement>();
078 this.prefixes = new ArrayList<String>();
079 }
080
081 public void startDirectory(String directoryName) throws IOException {
082 }
083
084 public void file(String fileName, int length, InputStream data) throws IOException {
085 try {
086 SAXParserFactory factory = SAXParserFactory.newInstance();
087 factory.setNamespaceAware(true);
088 factory.setValidating(true);
089 SAXParser parser = factory.newSAXParser();
090 parser.parse(data, attributesHandler);
091 }
092 catch (Exception e) {
093 Safe.rethrow(IOException.class, e);
094 }
095 }
096
097 public void endDirectory(String directoryName) throws IOException {
098 try {
099 EndElement end = stack.removeLast();
100 handler.endElement(end.uri, end.localName, end.qName);
101 if (stack.isEmpty()) {
102 for (String prefix : prefixes) {
103 log.debug("Removing prefix mapping " + prefix);
104 handler.endPrefixMapping(prefix);
105 }
106 prefixes.clear();
107 }
108 }
109 catch (Exception e) {
110 Safe.rethrow(IOException.class, e);
111 }
112 }
113
114 private static class EndElement {
115
116 /** . */
117 private final String uri;
118
119 /** . */
120 private final String localName;
121
122 /** . */
123 private final String qName;
124
125 private EndElement(String uri, String localName, String qName) {
126 this.uri = uri;
127 this.localName = localName;
128 this.qName = qName;
129 }
130 }
131 }
132