001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.so6.core.tool;
034:
035: import org.libresource.so6.core.Workspace;
036: import org.libresource.so6.core.WsConnection;
037: import org.libresource.so6.core.engine.FileParser;
038: import org.libresource.so6.core.engine.util.FileUtils;
039:
040: import java.io.File;
041:
042: /**
043: * @author molli
044: */
045: public class Revert {
046: private WsConnection ws;
047: private FileParser fp;
048:
049: public Revert(WsConnection ws) throws Exception {
050: this .ws = ws;
051: this .fp = new FileParser(ws);
052: }
053:
054: // path is a relative path;
055: public void revertFile(String path) throws Exception {
056: String copyref = ws.getRefCopyPath() + File.separator + path;
057:
058: if (!new File(copyref).exists()) {
059: new File(path).delete();
060:
061: return;
062: }
063:
064: FileUtils.copy(copyref, ws.getPath() + File.separator + path);
065: }
066:
067: public void revertDir(String path) throws Exception {
068: File f = new File(ws.getPath() + File.separator + path);
069:
070: if (!(f.isDirectory())) {
071: return;
072: }
073:
074: // ignore dataFiles !!
075: if (f.getName().equals(Workspace.SO6PREFIX)) {
076: return;
077: }
078:
079: String[] list = f.list();
080:
081: for (int i = 0; i < list.length; i++) {
082: File fl = new File(path + File.separator + list[i]);
083:
084: if (fl.isFile()) {
085: try {
086: revertFile(fl.getPath());
087: } catch (Exception e) {
088: }
089: }
090:
091: if (fl.isDirectory()) {
092: revertDir(fl.getPath());
093: }
094: }
095: }
096:
097: public void revert(String path) throws Exception {
098: File f = new File(ws.getPath(), path);
099: File refFile = new File(ws.getRefCopyPath(), path);
100:
101: if (!refFile.exists()) {
102: f.delete();
103:
104: return;
105: } else {
106: if (!f.exists()) {
107: // Check type
108: if (refFile.isDirectory()) {
109: f.mkdirs();
110: }
111:
112: if (refFile.isFile()) {
113: if (!f.getParentFile().exists()) {
114: f.getParentFile().mkdirs();
115: }
116:
117: f.createNewFile();
118: }
119: }
120:
121: if (f.isFile()) {
122: revertFile(f.getPath());
123: }
124:
125: if (f.isDirectory()) {
126: revertDir(f.getPath());
127: }
128: }
129: }
130:
131: public static void main(String[] args) throws Exception {
132: if (args.length < 1) {
133: throw new Exception(
134: "Usage: <so6 property file> [path1] ... [pathn]");
135: }
136:
137: WsConnection ws = new WsConnection(args[0]);
138: Revert rv = new Revert(ws);
139:
140: if (args.length > 1) {
141: for (int i = 1; i < args.length; i++) {
142: File f = new File(args[i]);
143:
144: if (!(f.exists())) {
145: throw new Exception(args[i] + " not exists");
146: }
147:
148: rv.revert(f.getAbsolutePath());
149: }
150: } else {
151: rv.revert(ws.getPath());
152: }
153: }
154: }
|