001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE 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
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixcore.editor2.core.spring;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileNotFoundException;
024: import java.io.FileOutputStream;
025: import java.io.IOException;
026: import java.util.HashMap;
027:
028: import org.w3c.dom.Document;
029: import org.xml.sax.SAXException;
030:
031: import de.schlund.pfixcore.editor2.core.exception.EditorIOException;
032: import de.schlund.pfixxml.util.Xml;
033:
034: /**
035: * Implementation of FileSystemService.
036: *
037: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
038: */
039: public class FileSystemServiceImpl implements FileSystemService {
040: private HashMap<File, Object> locks;
041:
042: public FileSystemServiceImpl() {
043: this .locks = new HashMap<File, Object>();
044: }
045:
046: public Object getLock(File file) {
047: synchronized (this .locks) {
048: if (this .locks.containsKey(file)) {
049: return this .locks.get(file);
050: } else {
051: Object lock = new Object();
052: this .locks.put(file, lock);
053: return lock;
054: }
055: }
056: }
057:
058: public Document readXMLDocumentFromFile(File file)
059: throws FileNotFoundException, SAXException, IOException {
060: /*
061: * DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
062: * dbf.setCoalescing(true); dbf.setNamespaceAware(true); DocumentBuilder
063: * builder = dbf.newDocumentBuilder(); return builder.parse(new
064: * FileInputStream(file));
065: */
066: return Xml.parseMutable(file);
067: }
068:
069: public void storeXMLDocumentToFile(File file, Document document)
070: throws IOException {
071: Xml.serialize(document, file, false, true);
072: }
073:
074: public void makeDirectory(File directory,
075: boolean makeParentDirectories) throws EditorIOException {
076: if (directory.exists()) {
077: throw new EditorIOException(
078: "Cannot create existing directory "
079: + directory.getPath() + "!");
080: }
081: boolean ret;
082: if (makeParentDirectories) {
083: ret = directory.mkdirs();
084: } else {
085: ret = directory.mkdir();
086: }
087: if (!ret) {
088: String msg = "Could not create directory "
089: + directory.getPath() + "!";
090: throw new EditorIOException(msg);
091: }
092: }
093:
094: public void copy(File source, File target) throws EditorIOException {
095: try {
096: if (!target.exists()) {
097: target.createNewFile();
098: }
099: FileInputStream fis = new FileInputStream(source);
100: FileOutputStream fos = new FileOutputStream(target);
101: byte[] buf = new byte[1024];
102: int l;
103: while ((l = fis.read(buf)) != -1) {
104: fos.write(buf, 0, l);
105: }
106: fis.close();
107: fos.close();
108: } catch (IOException e) {
109: String err = "Could not copy file \"" + source.getPath()
110: + "\" to \"" + target.getPath() + "\"!";
111: throw new EditorIOException(err, e);
112: }
113: }
114:
115: }
|