From: <mol...@us...> - 2008-07-01 14:10:22
|
Revision: 876 http://openutils.svn.sourceforge.net/openutils/?rev=876&view=rev Author: molaschi Date: 2008-07-01 07:10:27 -0700 (Tue, 01 Jul 2008) Log Message: ----------- [maven-release-plugin] copy for tag openutils-mgnlbootstrapsync-0.2 Added Paths: ----------- tags/openutils-mgnlbootstrapsync-0.2/ tags/openutils-mgnlbootstrapsync-0.2/pom.xml tags/openutils-mgnlbootstrapsync-0.2/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java Removed Paths: ------------- tags/openutils-mgnlbootstrapsync-0.2/pom.xml tags/openutils-mgnlbootstrapsync-0.2/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java Copied: tags/openutils-mgnlbootstrapsync-0.2 (from rev 873, trunk/openutils-mgnlbootstrapsync) Deleted: tags/openutils-mgnlbootstrapsync-0.2/pom.xml =================================================================== --- trunk/openutils-mgnlbootstrapsync/pom.xml 2008-06-26 17:03:15 UTC (rev 873) +++ tags/openutils-mgnlbootstrapsync-0.2/pom.xml 2008-07-01 14:10:27 UTC (rev 876) @@ -1,76 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - <parent> - <groupId>net.sourceforge.openutils</groupId> - <artifactId>openutils</artifactId> - <version>8</version> - <relativePath>..</relativePath> - </parent> - <modelVersion>4.0.0</modelVersion> - <packaging>jar</packaging> - <artifactId>openutils-mgnlbootstrapsync</artifactId> - <name>openutils-mgnlbootstrapsync</name> - <version>0.2-SNAPSHOT</version> - <licenses> - <license> - <name>GPLv3</name> - <url>http://www.gnu.org/licenses/gpl-3.0.txt</url> - </license> - </licenses> - <build> - <resources> - <resource> - <filtering>false</filtering> - <directory>src/main/resources</directory> - <includes> - <include>**/*</include> - </includes> - </resource> - <resource> - <filtering>true</filtering> - <directory>src/main/resources</directory> - <includes> - <include>META-INF/magnolia/*</include> - </includes> - </resource> - </resources> - </build> - <dependencies> - <dependency> - <groupId>info.magnolia</groupId> - <artifactId>magnolia-core</artifactId> - <version>3.5-rc2</version> - </dependency> - <dependency> - <groupId>info.magnolia</groupId> - <artifactId>magnolia-module-admininterface</artifactId> - <version>3.5.2</version> - </dependency> - <dependency> - <groupId>org.testng</groupId> - <artifactId>testng</artifactId> - <classifier>jdk15</classifier> - <version>5.1</version> - <scope>test</scope> - <exclusions> - <exclusion> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - </exclusion> - </exclusions> - </dependency> - </dependencies> - <repositories> - <repository> - <id>repository.magnolia.info</id> - <name>magnolia repository</name> - <url>http://svn.magnolia.info/maven/m2</url> - <releases> - <enabled>true</enabled> - </releases> - <snapshots> - <enabled>false</enabled> - </snapshots> - </repository> - </repositories> -</project> Copied: tags/openutils-mgnlbootstrapsync-0.2/pom.xml (from rev 875, trunk/openutils-mgnlbootstrapsync/pom.xml) =================================================================== --- tags/openutils-mgnlbootstrapsync-0.2/pom.xml (rev 0) +++ tags/openutils-mgnlbootstrapsync-0.2/pom.xml 2008-07-01 14:10:27 UTC (rev 876) @@ -0,0 +1,82 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <groupId>net.sourceforge.openutils</groupId> + <artifactId>openutils</artifactId> + <version>8</version> + <relativePath>..</relativePath> + </parent> + <modelVersion>4.0.0</modelVersion> + <packaging>jar</packaging> + <artifactId>openutils-mgnlbootstrapsync</artifactId> + <name>openutils-mgnlbootstrapsync</name> + <version>0.2</version> + <licenses> + <license> + <name>GPLv3</name> + <url>http://www.gnu.org/licenses/gpl-3.0.txt</url> + </license> + </licenses> + <build> + <resources> + <resource> + <filtering>false</filtering> + <directory>src/main/resources</directory> + <includes> + <include>**/*</include> + </includes> + </resource> + <resource> + <filtering>true</filtering> + <directory>src/main/resources</directory> + <includes> + <include>META-INF/magnolia/*</include> + </includes> + </resource> + </resources> + </build> + <dependencies> + <dependency> + <groupId>info.magnolia</groupId> + <artifactId>magnolia-core</artifactId> + <version>3.5-rc2</version> + </dependency> + <dependency> + <groupId>info.magnolia</groupId> + <artifactId>magnolia-module-admininterface</artifactId> + <version>3.5.2</version> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <classifier>jdk15</classifier> + <version>5.1</version> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + </exclusion> + </exclusions> + </dependency> + </dependencies> + <repositories> + <repository> + <id>repository.magnolia.info</id> + <name>magnolia repository</name> + <url>http://svn.magnolia.info/maven/m2</url> + <releases> + <enabled>true</enabled> + </releases> + <snapshots> + <enabled>false</enabled> + </snapshots> + </repository> + </repositories> + + <scm> + <connection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-mgnlbootstrapsync-0.2</connection> + <developerConnection>scm:svn:https://openutils.svn.sourceforge.net/svnroot/openutils/tags/openutils-mgnlbootstrapsync-0.2</developerConnection> + <url>http://openutils.svn.sourceforge.net/viewcvs.cgi/openutils/tags/openutils-mgnlbootstrapsync-0.2</url> + </scm> +</project> Deleted: tags/openutils-mgnlbootstrapsync-0.2/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java =================================================================== --- trunk/openutils-mgnlbootstrapsync/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java 2008-06-26 17:03:15 UTC (rev 873) +++ tags/openutils-mgnlbootstrapsync-0.2/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java 2008-07-01 14:10:27 UTC (rev 876) @@ -1,427 +0,0 @@ -package it.openutils.mgnlbootstrapsync.listener; - -import info.magnolia.cms.core.Content; -import info.magnolia.cms.core.HierarchyManager; -import info.magnolia.cms.core.Path; -import info.magnolia.cms.core.ie.DataTransporter; -import info.magnolia.cms.core.ie.filters.VersionFilter; -import info.magnolia.cms.security.AccessDeniedException; -import info.magnolia.cms.util.ContentUtil; -import info.magnolia.context.MgnlContext; -import it.openutils.mgnlbootstrapsync.BootstrapAtomicFilter; -import it.openutils.mgnlbootstrapsync.watch.BootstrapSyncRepositoryWatch; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.util.Collection; -import java.util.Iterator; -import java.util.zip.DeflaterOutputStream; - -import javax.jcr.PathNotFoundException; -import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.observation.EventIterator; -import javax.jcr.observation.EventListener; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.helpers.XMLReaderFactory; - -/** - * - * @author mmolaschi - * @version $Id: $ - */ -public abstract class AbstractBootstrapSyncListener implements EventListener -{ - - private static Logger log = LoggerFactory.getLogger(AbstractBootstrapSyncListener.class); - - private BootstrapSyncRepositoryWatch watch; - - private Object synchronizationObject = new Object(); - - /** - * - * @param watch watch for this listener - */ - public AbstractBootstrapSyncListener(BootstrapSyncRepositoryWatch watch) - { - this.watch = watch; - } - - - /** - * {@inheritDoc} - */ - public abstract void onEvent(EventIterator events); - - public BootstrapSyncRepositoryWatch getWatch() - { - return watch; - } - - public void setWatch(BootstrapSyncRepositoryWatch watch) - { - this.watch = watch; - } - - /** - * Change event on node - * @param nodePath path to node - */ - public void exportNode(String nodePath) - { - synchronized (synchronizationObject) - { - String path = nodePath; - - // get repository manager - HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(watch.getRepository()); - - // check if path can exported - boolean isEnablePath = this.watch.getEnableRoots().isEnable(path); - if (isEnablePath) - { - try - { - // check if it is a property - if (hm.isNodeData(path)) - { - // move to parent node - path = StringUtils.substringBeforeLast(path, "/"); - } - } - catch (AccessDeniedException e) - { - log.error(e.getMessage(), e); - return; - } - - Content exported = null; - - try - { - // get exported content - exported = getRightContent(hm.getContent(path), watch.getNodeType()); - if (exported == null) - { - return; - } - } - catch (RepositoryException e) - { - if (log.isDebugEnabled() && e instanceof PathNotFoundException) - { - log.debug("Path already deleted:" + path); - } - if (log.isErrorEnabled() && !(e instanceof PathNotFoundException)) - { - log.error("Error hierarchy manager for path " + path, e); - } - return; - } - - // get handle - String handle = exported.getHandle(); - - // check if path is under a "compressed" node - String pathToExport = this.watch.getExportRoots().getRootPath(handle); - - if (pathToExport == null) - { - // not "compressed" - exportFileIterative(hm, exported); - } - else - { - exportFile(hm, pathToExport, false); - } - } - } - } - - /** - * Remove event on node - * @param path path to node - */ - public void removeNode(String path) - { - // synchornization - synchronized (synchronizationObject) - { - // get hierarchy manager - HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(watch.getRepository()); - - // check if path can exported - boolean isEnablePath = this.watch.getEnableRoots().isEnable(path); - if (isEnablePath) - { - // check if path is under a "compressed" node - String pathToExport = this.watch.getExportRoots().getRootPath(path); - - if (pathToExport == null || pathToExport.equals(path)) - { - // remove every file that starts with path - cleanFileSystem(path); - - try - { - // get parent node - String parentHandle = StringUtils.substringBeforeLast(path, "/"); - String parentXmlName = watch.getRepository() - + StringUtils.replace(parentHandle, "/", ".") - + ".xml"; - if (parentHandle.length() == 0) - { - parentHandle = "/"; - } - - // check if path is under a "compressed" node - String pathParentToExport = this.watch.getExportRoots().getRootPath(parentHandle); - - if (pathParentToExport == null) - { - // xml reader for filtering 2 levels - BootstrapAtomicFilter xmlReader = new BootstrapAtomicFilter(XMLReaderFactory - .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); - - // export parent node - exportFile(parentXmlName, xmlReader, hm, parentHandle); - } - else - { - exportFile(hm, pathParentToExport, false); - } - } - catch (SAXException ex) - { - throw new RuntimeException(ex); - } - } - else - { - exportFile(hm, pathToExport, false); - } - } - } - } - - /** - * Go up in tree until the current node type equals passed item type - * @param c current node - * @param itemType item type to check - * @return right node or null if not found - * @throws RepositoryException repository exception - */ - @SuppressWarnings("unchecked") - private Content getRightContent(Content c, String itemType) throws RepositoryException - { - if (c.getItemType().getSystemName().equals(itemType)) - { - return c; - } - if (c.getChildren() != null) - { - Iterator it = c.getChildren().iterator(); - while(it.hasNext()) - { - return getRightContent((Content)it.next(), itemType); - } - } - return null; - } - - /** - * Export node and its subnodes - * @param hm hierarchy manager - * @param node node - */ - @SuppressWarnings("unchecked") - protected void exportFileIterative(HierarchyManager hm, Content node) - { - // write itself - exportFile(hm, node.getHandle(), true); - - // cycle on children - Collection<Content> children = node.getChildren(ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER); - if (children != null) - { - for (Content child : children) - { - exportFileIterative(hm, child); - } - } - } - - /** - * Export node to file - * @param hm hierarchy manager - * @param handle path to node - * @param singleNode export single node in file - */ - protected void exportFile(HierarchyManager hm, String handle, boolean singleNode) - { - try - { - if (singleNode) - { - String parentHandle = StringUtils.substringBeforeLast(handle, "/"); - String parentXmlName = watch.getRepository() + StringUtils.replace(parentHandle, "/", ".") + ".xml"; - if (parentHandle.length() == 0) - { - parentHandle = "/"; - } - - BootstrapAtomicFilter xmlReader = new BootstrapAtomicFilter(XMLReaderFactory - .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); - - exportFile(parentXmlName, xmlReader, hm, parentHandle); - } - String xmlName = watch.getRepository() + StringUtils.replace(handle, "/", ".") + ".xml"; - - XMLReader xmlReader = null; - - if (singleNode) - { - xmlReader = new BootstrapAtomicFilter(XMLReaderFactory - .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); - } - exportFile(xmlName, xmlReader, hm, handle); - } - catch (SAXException ex) - { - throw new RuntimeException(ex); - } - } - - /** - * - * @param fileName file name - * @param reader xml filter - * @param hm hierarchy manager - * @param handle path to node - */ - protected void exportFile(String fileName, XMLReader reader, HierarchyManager hm, String handle) - { - // create necessary parent directories - File folder = new File(Path.getAbsoluteFileSystemPath(watch.getExportPath())); - folder.mkdirs(); - - File xmlFile = new File(folder.getAbsoluteFile(), fileName); - FileOutputStream fos = null; - try - { - fos = new FileOutputStream(xmlFile); - } - catch (FileNotFoundException e) - { - log.error(e.getMessage(), e); - return; - } - - try - { - executeExport(fos, reader, hm.getWorkspace().getSession(), handle, watch.getRepository()); - } - catch (IOException e) - { - log.error(e.getMessage(), e); - } - finally - { - IOUtils.closeQuietly(fos); - } - } - - /** - * Clean files for node (path) - * @param path path to node - */ - protected void cleanFileSystem(String path) - { - - String baseName = watch.getRepository() + StringUtils.replace(path, "/", "."); - - // create necessary parent directories - File folder = new File(Path.getAbsoluteFileSystemPath(watch.getExportPath())); - - if (folder.exists()) - { - String[] files = folder.list(); - for (String file : files) - { - File f = new File(folder, file); - if (f.exists() && f.getName().startsWith(baseName)) - { - f.delete(); - log.debug("Removed File: " + f.getName()); - } - } - } - } - - /** - * Execute export - * @param baseOutputStream file output stream - * @param reader xml filter - * @param session jcr session - * @param basepath path to node - * @param repository repository - * @throws IOException exception writing file - */ - protected static void executeExport(OutputStream baseOutputStream, XMLReader reader, Session session, - String basepath, String repository) throws IOException - { - OutputStream outputStream = baseOutputStream; - - try - { - // use XMLSerializer and a SAXFilter in order to rewrite the - // file - - XMLReader xmlReader = reader; - if (reader == null) - { - xmlReader = new VersionFilter(XMLReaderFactory - .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); - } - else - { - xmlReader = new VersionFilter(reader); - } - - DataTransporter.parseAndFormat(outputStream, xmlReader, repository, basepath, session, false); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - catch (SAXException e) - { - throw new RuntimeException(e); - } - catch (RepositoryException e) - { - throw new RuntimeException(e); - } - - // finish the stream properly if zip stream - // this is not done by the IOUtils - if (outputStream instanceof DeflaterOutputStream) - { - ((DeflaterOutputStream) outputStream).finish(); - } - - baseOutputStream.flush(); - IOUtils.closeQuietly(baseOutputStream); - } - -} Copied: tags/openutils-mgnlbootstrapsync-0.2/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java (from rev 874, trunk/openutils-mgnlbootstrapsync/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java) =================================================================== --- tags/openutils-mgnlbootstrapsync-0.2/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java (rev 0) +++ tags/openutils-mgnlbootstrapsync-0.2/src/main/java/it/openutils/mgnlbootstrapsync/listener/AbstractBootstrapSyncListener.java 2008-07-01 14:10:27 UTC (rev 876) @@ -0,0 +1,429 @@ +package it.openutils.mgnlbootstrapsync.listener; + +import info.magnolia.cms.core.Content; +import info.magnolia.cms.core.HierarchyManager; +import info.magnolia.cms.core.Path; +import info.magnolia.cms.core.ie.DataTransporter; +import info.magnolia.cms.core.ie.filters.VersionFilter; +import info.magnolia.cms.security.AccessDeniedException; +import info.magnolia.cms.util.ContentUtil; +import info.magnolia.context.MgnlContext; +import it.openutils.mgnlbootstrapsync.BootstrapAtomicFilter; +import it.openutils.mgnlbootstrapsync.watch.BootstrapSyncRepositoryWatch; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Collection; +import java.util.Iterator; +import java.util.zip.DeflaterOutputStream; + +import javax.jcr.PathNotFoundException; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.observation.EventIterator; +import javax.jcr.observation.EventListener; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.helpers.XMLReaderFactory; + + +/** + * @author mmolaschi + * @version $Id: $ + */ +public abstract class AbstractBootstrapSyncListener implements EventListener +{ + + private static Logger log = LoggerFactory.getLogger(AbstractBootstrapSyncListener.class); + + private BootstrapSyncRepositoryWatch watch; + + private Object synchronizationObject = new Object(); + + /** + * @param watch watch for this listener + */ + public AbstractBootstrapSyncListener(BootstrapSyncRepositoryWatch watch) + { + this.watch = watch; + } + + /** + * {@inheritDoc} + */ + public abstract void onEvent(EventIterator events); + + public BootstrapSyncRepositoryWatch getWatch() + { + return watch; + } + + public void setWatch(BootstrapSyncRepositoryWatch watch) + { + this.watch = watch; + } + + /** + * Change event on node + * @param nodePath path to node + */ + public void exportNode(String nodePath) + { + synchronized (synchronizationObject) + { + String path = nodePath; + + // get repository manager + HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(watch.getRepository()); + + // check if path can exported + boolean isEnablePath = this.watch.getEnableRoots().isEnable(path); + if (isEnablePath) + { + try + { + // check if it is a property + if (hm.isNodeData(path)) + { + // move to parent node + path = StringUtils.substringBeforeLast(path, "/"); + } + } + catch (AccessDeniedException e) + { + log.error(e.getMessage(), e); + return; + } + + Content exported = null; + + try + { + + if (watch.getNodeType() != null) + { + // get exported content + exported = getRightContent(hm.getContent(path), watch.getNodeType()); + if (exported == null) + { + return; + } + + } + else + { + exported = hm.getContent(path); + } + } + catch (RepositoryException e) + { + if (log.isDebugEnabled() && e instanceof PathNotFoundException) + { + log.debug("Path already deleted:" + path); + } + if (log.isErrorEnabled() && !(e instanceof PathNotFoundException)) + { + log.error("Error hierarchy manager for path " + path, e); + } + return; + } + + // get handle + String handle = exported.getHandle(); + + // check if path is under a "compressed" node + String pathToExport = this.watch.getExportRoots().getRootPath(handle); + + if (pathToExport == null) + { + // not "compressed" + exportFileIterative(hm, exported); + } + else + { + exportFile(hm, pathToExport, false); + } + } + } + } + + /** + * Remove event on node + * @param path path to node + */ + public void removeNode(String path) + { + // synchornization + synchronized (synchronizationObject) + { + // get hierarchy manager + HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(watch.getRepository()); + + // check if path can exported + boolean isEnablePath = this.watch.getEnableRoots().isEnable(path); + if (isEnablePath) + { + // check if path is under a "compressed" node + String pathToExport = this.watch.getExportRoots().getRootPath(path); + + if (pathToExport == null || pathToExport.equals(path)) + { + // remove every file that starts with path + cleanFileSystem(path); + + try + { + // get parent node + String parentHandle = StringUtils.substringBeforeLast(path, "/"); + String parentXmlName = watch.getRepository() + + StringUtils.replace(parentHandle, "/", ".") + + ".xml"; + if (parentHandle.length() == 0) + { + parentHandle = "/"; + } + + // check if path is under a "compressed" node + String pathParentToExport = this.watch.getExportRoots().getRootPath(parentHandle); + + if (pathParentToExport == null) + { + // xml reader for filtering 2 levels + BootstrapAtomicFilter xmlReader = new BootstrapAtomicFilter(XMLReaderFactory + .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); + + // export parent node + exportFile(parentXmlName, xmlReader, hm, parentHandle); + } + else + { + exportFile(hm, pathParentToExport, false); + } + } + catch (SAXException ex) + { + throw new RuntimeException(ex); + } + } + else + { + exportFile(hm, pathToExport, false); + } + } + } + } + + /** + * Go up in tree until the current node type equals passed item type + * @param c current node + * @param itemType item type to check + * @return right node or null if not found + * @throws RepositoryException repository exception + */ + @SuppressWarnings("unchecked") + private Content getRightContent(Content c, String itemType) throws RepositoryException + { + if (c.getItemType().getSystemName().equals(itemType)) + { + return c; + } + if (c.getParent() != null) + { + return getRightContent(c.getParent(), itemType); + } + return null; + } + + /** + * Export node and its subnodes + * @param hm hierarchy manager + * @param node node + */ + @SuppressWarnings("unchecked") + protected void exportFileIterative(HierarchyManager hm, Content node) + { + // write itself + exportFile(hm, node.getHandle(), true); + + // cycle on children + Collection<Content> children = node.getChildren(ContentUtil.EXCLUDE_META_DATA_CONTENT_FILTER); + if (children != null) + { + for (Content child : children) + { + exportFileIterative(hm, child); + } + } + } + + /** + * Export node to file + * @param hm hierarchy manager + * @param handle path to node + * @param singleNode export single node in file + */ + protected void exportFile(HierarchyManager hm, String handle, boolean singleNode) + { + try + { + if (singleNode) + { + String parentHandle = StringUtils.substringBeforeLast(handle, "/"); + String parentXmlName = watch.getRepository() + StringUtils.replace(parentHandle, "/", ".") + ".xml"; + if (parentHandle.length() == 0) + { + parentHandle = "/"; + } + + BootstrapAtomicFilter xmlReader = new BootstrapAtomicFilter(XMLReaderFactory + .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); + + exportFile(parentXmlName, xmlReader, hm, parentHandle); + } + String xmlName = watch.getRepository() + StringUtils.replace(handle, "/", ".") + ".xml"; + + XMLReader xmlReader = null; + + if (singleNode) + { + xmlReader = new BootstrapAtomicFilter(XMLReaderFactory + .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); + } + exportFile(xmlName, xmlReader, hm, handle); + } + catch (SAXException ex) + { + throw new RuntimeException(ex); + } + } + + /** + * @param fileName file name + * @param reader xml filter + * @param hm hierarchy manager + * @param handle path to node + */ + protected void exportFile(String fileName, XMLReader reader, HierarchyManager hm, String handle) + { + // create necessary parent directories + File folder = new File(Path.getAbsoluteFileSystemPath(watch.getExportPath())); + folder.mkdirs(); + + File xmlFile = new File(folder.getAbsoluteFile(), fileName); + FileOutputStream fos = null; + try + { + fos = new FileOutputStream(xmlFile); + } + catch (FileNotFoundException e) + { + log.error(e.getMessage(), e); + return; + } + + try + { + executeExport(fos, reader, hm.getWorkspace().getSession(), handle, watch.getRepository()); + } + catch (IOException e) + { + log.error(e.getMessage(), e); + } + finally + { + IOUtils.closeQuietly(fos); + } + } + + /** + * Clean files for node (path) + * @param path path to node + */ + protected void cleanFileSystem(String path) + { + + String baseName = watch.getRepository() + StringUtils.replace(path, "/", "."); + + // create necessary parent directories + File folder = new File(Path.getAbsoluteFileSystemPath(watch.getExportPath())); + + if (folder.exists()) + { + String[] files = folder.list(); + for (String file : files) + { + File f = new File(folder, file); + if (f.exists() && f.getName().startsWith(baseName)) + { + f.delete(); + log.debug("Removed File: " + f.getName()); + } + } + } + } + + /** + * Execute export + * @param baseOutputStream file output stream + * @param reader xml filter + * @param session jcr session + * @param basepath path to node + * @param repository repository + * @throws IOException exception writing file + */ + protected static void executeExport(OutputStream baseOutputStream, XMLReader reader, Session session, + String basepath, String repository) throws IOException + { + OutputStream outputStream = baseOutputStream; + + try + { + // use XMLSerializer and a SAXFilter in order to rewrite the + // file + + XMLReader xmlReader = reader; + if (reader == null) + { + xmlReader = new VersionFilter(XMLReaderFactory + .createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName())); + } + else + { + xmlReader = new VersionFilter(reader); + } + + DataTransporter.parseAndFormat(outputStream, xmlReader, repository, basepath, session, false); + } + catch (IOException e) + { + throw new RuntimeException(e); + } + catch (SAXException e) + { + throw new RuntimeException(e); + } + catch (RepositoryException e) + { + throw new RuntimeException(e); + } + + // finish the stream properly if zip stream + // this is not done by the IOUtils + if (outputStream instanceof DeflaterOutputStream) + { + ((DeflaterOutputStream) outputStream).finish(); + } + + baseOutputStream.flush(); + IOUtils.closeQuietly(baseOutputStream); + } + +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |