You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by pi...@apache.org on 2004/11/08 04:41:53 UTC
svn commit: rev 56902 - in cocoon/whiteboard/kernel/sources/blocks/repository-file: . src src/org src/org/apache src/org/apache/cocoon src/org/apache/cocoon/components src/org/apache/cocoon/components/repository src/org/apache/cocoon/components/repository/file
Author: pier
Date: Sun Nov 7 19:41:53 2004
New Revision: 56902
Added:
cocoon/whiteboard/kernel/sources/blocks/repository-file/
cocoon/whiteboard/kernel/sources/blocks/repository-file/cocoon.xml
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileDocument.java
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileIterator.java
cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileRepository.java
Log:
More examples
Added: cocoon/whiteboard/kernel/sources/blocks/repository-file/cocoon.xml
==============================================================================
--- (empty file)
+++ cocoon/whiteboard/kernel/sources/blocks/repository-file/cocoon.xml Sun Nov 7 19:41:53 2004
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<block xmlns="http://apache.org/cocoon/kernel/descriptor/1.0"
+ id="http://cocoon.apache.org/kernel/blocks/repository/file/1.0.0">
+
+ <implementations>
+ <implements contract="http://cocoon.apache.org/kernel/contracts/repository/1.0"/>
+ </implementations>
+
+ <provides
+ component="org.apache.cocoon.components.repository.file.FileRepository"
+ initialize="initialize" destroy="destroy"/>
+
+</block>
Added: cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileDocument.java
==============================================================================
--- (empty file)
+++ cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileDocument.java Sun Nov 7 19:41:53 2004
@@ -0,0 +1,80 @@
+/* =============================================================================== *
+ * Copyright (C) 1999-2004, The Apache Software Foundation. All rights reserved. *
+ * *
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use *
+ * this file except in compliance with the License. You may obtain a copy of the *
+ * License at <http://www.apache.org/licenses/LICENSE-2.0>. *
+ * *
+ * Unless required by applicable law or agreed to in writing, software distributed *
+ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR *
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations under the License. *
+ * =============================================================================== */
+package org.apache.cocoon.components.repository.file;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Date;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.cocoon.components.repository.Document;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>TODO.</p>
+ *
+ * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
+ * @author Copyright © 2000-2004 <a href="http://www.apache.org/">The Apache
+ * Software Foundation</a>. All rights reserved.
+ */
+public class FileDocument implements Document {
+
+ /** <p>The {@link File} associated with this {@link Document}.</p> */
+ private File file = null;
+
+ /**
+ * <p>Create a new {@link FileDocument} instance.</p>
+ */
+ public FileDocument(File file)
+ throws IOException {
+ if (file == null) throw new NullPointerException("Null file");
+ if (! file.isFile()) throw new FileNotFoundException(file.toString());
+ this.file = file.getCanonicalFile();
+ return;
+ }
+
+ public Date getLastModified() {
+ return new Date(this.file.lastModified());
+ }
+
+ public void process(ContentHandler handler)
+ throws IOException, SAXException {
+ try {
+ StreamSource source = new StreamSource(this.file);
+ SAXResult result = new SAXResult(handler);
+ TransformerFactory factory = null;
+ factory = (TransformerFactory) TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer();
+ transformer.transform(source, result);
+ } catch (TransformerConfigurationException exception) {
+ throw new SAXException("Error processing XML document", exception);
+ } catch (TransformerException exception) {
+ Throwable throwable = exception.getException();
+ if (throwable instanceof SAXException) throw (SAXException) throwable;
+ if (throwable instanceof IOException) throw (IOException) throwable;
+ if (throwable instanceof Exception) {
+ Exception nested = (Exception) throwable;
+ throw new SAXException("Error processing XML document", nested);
+ }
+ throw new SAXException("Error processing XML document", exception);
+ }
+ }
+}
Added: cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileIterator.java
==============================================================================
--- (empty file)
+++ cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileIterator.java Sun Nov 7 19:41:53 2004
@@ -0,0 +1,123 @@
+/* =============================================================================== *
+ * Copyright (C) 1999-2004, The Apache Software Foundation. All rights reserved. *
+ * *
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use *
+ * this file except in compliance with the License. You may obtain a copy of the *
+ * License at <http://www.apache.org/licenses/LICENSE-2.0>. *
+ * *
+ * Unless required by applicable law or agreed to in writing, software distributed *
+ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR *
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations under the License. *
+ * =============================================================================== */
+package org.apache.cocoon.components.repository.file;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>TODO.</p>
+ *
+ * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
+ * @author Copyright © 2000-2004 <a href="http://www.apache.org/">The Apache
+ * Software Foundation</a>. All rights reserved.
+ */
+public class FileIterator implements Iterator {
+
+ private File root = null;
+ private String list[] = null;
+ private int position = 0;
+ private String prefix = null;
+ private String current = null;
+ private FileIterator nested = null;
+ private String last = null;
+ private FileRepository repository = null;
+
+ /**
+ * <p>Create a new {@link FileIterator} instance.</p>
+ */
+ public FileIterator(File root, FileRepository repository) {
+ this(root, (String) null);
+ this.repository = repository;
+ }
+
+ private FileIterator(File root, String prefix) {
+ this.root = root;
+ this.prefix = prefix;
+ this.list = root.list();
+ if (this.list == null) this.list = new String[0];
+ this.fetch();
+ }
+
+ private void fetch() {
+ if (this.nested != null) {
+ if (this.nested.hasNext()) {
+ this.current = (String) this.nested.next();
+ return;
+ }
+ }
+
+ if (this.position < this.list.length) {
+ String current = this.list[this.position ++];
+ File file = new File(this.root, current);
+ if (file.isFile()) {
+ String name = file.getName();
+ if ((name.startsWith(FileRepository.TEMP_PREFIX)) &&
+ (name.endsWith(FileRepository.TEMP_SUFFIX))) {
+ this.fetch();
+ } else {
+ this.current = current;
+ }
+ } else if (file.isDirectory()) {
+ this.nested = new FileIterator(file, current);
+ this.fetch();
+ } else {
+ this.fetch();
+ }
+ } else {
+ this.current = null;
+ }
+ }
+
+ public boolean hasNext() {
+ return(this.current != null);
+ }
+
+ public Object next() {
+ if (this.current == null) {
+ this.last = null;
+ throw new NoSuchElementException();
+ }
+
+ this.last = this.current;
+ if (this.prefix != null) {
+ this.last = this.prefix + '/' + this.last;
+ }
+
+ this.fetch();
+
+ return (this.last);
+ }
+
+ public void remove() {
+ if (this.last == null) throw new IllegalStateException();
+ if (this.repository == null) throw new UnsupportedOperationException();
+ try {
+ this.repository.delete(this.last);
+ } catch (IOException e) {
+ Log log = LogFactory.getLog(this.getClass());
+ log.error("I/O error deleting document \"" + this.last + "\"", e);
+ } catch (SAXException e) {
+ Log log = LogFactory.getLog(this.getClass());
+ log.error("XML error deleting document \"" + this.last + "\"", e);
+ } finally {
+ this.last = null;
+ }
+ }
+}
Added: cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileRepository.java
==============================================================================
--- (empty file)
+++ cocoon/whiteboard/kernel/sources/blocks/repository-file/src/org/apache/cocoon/components/repository/file/FileRepository.java Sun Nov 7 19:41:53 2004
@@ -0,0 +1,155 @@
+/* =============================================================================== *
+ * Copyright (C) 1999-2004, The Apache Software Foundation. All rights reserved. *
+ * *
+ * Licensed under the Apache License, Version 2.0 (the "License"). You may not use *
+ * this file except in compliance with the License. You may obtain a copy of the *
+ * License at <http://www.apache.org/licenses/LICENSE-2.0>. *
+ * *
+ * Unless required by applicable law or agreed to in writing, software distributed *
+ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR *
+ * CONDITIONS OF ANY KIND, either express or implied. See the License for the *
+ * specific language governing permissions and limitations under the License. *
+ * =============================================================================== */
+package org.apache.cocoon.components.repository.file;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.StringTokenizer;
+
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.cocoon.components.repository.AbstractRepository;
+import org.apache.cocoon.components.repository.Document;
+import org.apache.cocoon.components.repository.Repository;
+import org.apache.commons.logging.Log;
+import org.xml.sax.SAXException;
+
+/**
+ * <p>TODO.</p>
+ *
+ * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
+ * @author Copyright © 2000-2004 <a href="http://www.apache.org/">The Apache
+ * Software Foundation</a>. All rights reserved.
+ */
+public class FileRepository extends AbstractRepository implements Repository {
+
+ /** <p>The root file of this {@link FileRepository}.</p> */
+ private File root = null;
+ /** <p>The base path of this {@link FileRepository}.</p> */
+ private String path = null;
+ /** <p>The flag indicating if this {@link FileRepository} is open.</p> */
+ private boolean open = false;
+
+ /** <p>The prefix for temporary files.</p> */
+ public static final String TEMP_PREFIX = ".repo.";
+ /** <p>The suffix for temporary files.</p> */
+ public static final String TEMP_SUFFIX = ".tmp";
+
+ /**
+ * <p>Create a new {@link FileRepository} instance.</p>
+ */
+ public FileRepository() {
+ super();
+ }
+
+ public void setRootPath(String path)
+ throws IOException {
+ this.path = path;
+ }
+
+ public void initialize()
+ throws IOException {
+ if (this.path == null) throw new IOException("No root path specified");
+ Log log = this.getLogger();
+
+ try {
+ this.root = new File(this.path).getCanonicalFile();
+ } catch (IOException exception) {
+ String message = "Unable to access root \"" + this.root + "\"";
+ log.fatal(message, exception);
+ throw exception;
+ }
+
+ if (! this.root.isDirectory()) {
+ if (! this.root.mkdir()) {
+ String message = "Unable to create root \"" + this.root + "\"";
+ log.fatal(message);
+ throw new IOException(message);
+ }
+ }
+
+ log.info("FileRepositry at \"" + this.root + "\" initialized");
+ this.open = true;
+ }
+
+ public void destroy() {
+ this.getLogger().info("FileRepositry at \"" + this.root + "\" destroyed");
+ this.open = false;
+ }
+
+
+
+ public Document retrieve(String identifier)
+ throws IOException {
+ if (! this.open) throw new IllegalStateException("Repository not available");
+
+ File document = this.getFile(identifier).getCanonicalFile();
+ if (document.isFile()) return new FileDocument(document);
+ return null;
+ }
+
+ public Iterator documents() {
+ if (! this.open) throw new IllegalStateException("Repository not available");
+
+ return new FileIterator(this.root, this);
+ }
+
+ protected void doStore(String identifier, Document document)
+ throws IOException, SAXException {
+ if (! this.open) throw new IllegalStateException("Repository not available");
+
+ File file = this.getFile(identifier);
+ File temp = file.getParentFile();
+ temp = File.createTempFile(TEMP_PREFIX, TEMP_SUFFIX, temp);
+
+ try {
+ StreamResult result = new StreamResult(temp);
+ SAXTransformerFactory factory = null;
+ factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
+ TransformerHandler handler = factory.newTransformerHandler();
+ document.process(handler);
+ if (!temp.renameTo(file)) {
+ String msg = "Can't rename \"" + temp + "\" into \"" + file + "\"";
+ throw new IOException(msg);
+ }
+ } catch (TransformerConfigurationException exception) {
+ throw new SAXException("Error saving XML document", exception);
+ } finally {
+ temp.delete();
+ }
+ }
+
+ protected void doDelete(String identifier)
+ throws IOException {
+ if (! this.open) throw new IllegalStateException("Repository not available");
+
+ File document = this.getFile(identifier).getCanonicalFile();
+ if (document.isFile()) document.delete();
+ }
+
+ private File getFile(String identifier) {
+ File document = new File(this.root.getAbsolutePath());
+ StringTokenizer tokenizer = new StringTokenizer(identifier, "/");
+ while (tokenizer.hasMoreTokens()) {
+ String token = tokenizer.nextToken();
+ if (token == null) continue;
+ if (token.length() == 0) continue;
+ document = new File(document, tokenizer.nextToken());
+ }
+ return document;
+ }
+}