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.plugin.builders;
034:
035: import org.eclipse.core.resources.IFile;
036: import org.eclipse.core.resources.IFolder;
037: import org.eclipse.core.resources.IMarker;
038: import org.eclipse.core.resources.IProject;
039: import org.eclipse.core.resources.IResource;
040: import org.eclipse.core.resources.IResourceDelta;
041: import org.eclipse.core.resources.IResourceDeltaVisitor;
042: import org.eclipse.core.resources.IResourceVisitor;
043: import org.eclipse.core.resources.IncrementalProjectBuilder;
044: import org.eclipse.core.runtime.CoreException;
045: import org.eclipse.core.runtime.IProgressMonitor;
046:
047: import org.libresource.so6.core.WsConnection;
048: import org.libresource.so6.core.engine.DBType;
049: import org.libresource.so6.core.tf.TextFileFunctions;
050: import org.libresource.so6.plugin.core.So6Util;
051:
052: /**
053: * @author Guillaume Bort
054: */
055: import java.io.FileInputStream;
056:
057: import java.nio.CharBuffer;
058: import java.nio.MappedByteBuffer;
059: import java.nio.channels.FileChannel;
060: import java.nio.charset.Charset;
061: import java.nio.charset.CharsetDecoder;
062:
063: import java.util.Map;
064: import java.util.regex.Matcher;
065: import java.util.regex.Pattern;
066:
067: /**
068: * DOCUMENT ME!
069: *
070: * @author $author$
071: * @version $Revision: 1.3 $
072: */
073: public class ConflictsFinder extends IncrementalProjectBuilder {
074: /** DOCUMENT ME! */
075: public static final String ID = "org.libresource.so6.plugin.conflictsFinder";
076: private static Charset charset = Charset.forName("UTF-8");
077: private static CharsetDecoder decoder = charset.newDecoder();
078: private static Pattern linePattern = Pattern.compile(".*\r?\n");
079:
080: /**
081: * DOCUMENT ME!
082: *
083: * @param kind DOCUMENT ME!
084: * @param args DOCUMENT ME!
085: * @param monitor DOCUMENT ME!
086: *
087: * @return DOCUMENT ME!
088: *
089: * @throws CoreException DOCUMENT ME!
090: */
091: protected IProject[] build(int kind, Map args,
092: IProgressMonitor monitor) throws CoreException {
093: IResourceDelta delta = ((kind != FULL_BUILD) ? getDelta(getProject())
094: : null);
095:
096: if ((delta == null) || (kind == FULL_BUILD)) {
097: IProject project = getProject();
098:
099: if (So6Util.isSo6Project(project)) {
100: project.accept(new Visitor(monitor));
101: }
102: } else {
103: delta.accept(new DeltaVisitor(monitor));
104: }
105:
106: return null;
107: }
108:
109: /**
110: * DOCUMENT ME!
111: *
112: * @param resource DOCUMENT ME!
113: * @param monitor DOCUMENT ME!
114: */
115: protected void findConflict(IResource resource,
116: IProgressMonitor monitor) {
117: So6Util.deleteProblemMarkersOnResource(resource);
118:
119: try {
120: WsConnection connection = So6Util.getWsConnection(resource
121: .getProject());
122: int fileType = connection.getDBType().getType(
123: resource.getProjectRelativePath().toString());
124:
125: if (resource.getName().indexOf("#") != -1) {
126: So6Util.createProblemMarker(resource,
127: "Conflict on file", IMarker.SEVERITY_ERROR);
128: }
129:
130: WsConnection wsConnection = new WsConnection(
131: So6Util.getAbsoluteSo6PropertiesPath(resource
132: .getProject()));
133:
134: if (fileType == DBType.TYPE_FILE_TXT) {
135: FileInputStream fis = new FileInputStream(resource
136: .getLocation().toFile());
137: FileChannel fc = fis.getChannel();
138: int sz = (int) fc.size();
139: MappedByteBuffer bb = fc.map(
140: FileChannel.MapMode.READ_ONLY, 0, sz);
141: CharBuffer cb = decoder.decode(bb);
142: Matcher lm = linePattern.matcher(cb);
143: Matcher pm = null;
144: int lines = 0;
145:
146: while (lm.find()) {
147: lines++;
148:
149: CharSequence cs = lm.group(); // The current line
150:
151: if (cs.toString().startsWith(
152: TextFileFunctions.CONFLICT_BLOC_START)) {
153: String type = cs.toString().substring(
154: TextFileFunctions.CONFLICT_BLOC_START
155: .length()).trim();
156: So6Util.createProblemMarker((IFile) resource,
157: "Conflict " + type + " in file",
158: IMarker.SEVERITY_ERROR, lines);
159: }
160:
161: if (lm.end() == cb.limit()) {
162: break;
163: }
164: }
165: }
166: } catch (Exception e) {
167: e.printStackTrace();
168: }
169: }
170:
171: private class DeltaVisitor implements IResourceDeltaVisitor {
172: private IProgressMonitor monitor;
173:
174: public DeltaVisitor(IProgressMonitor monitor) {
175: this .monitor = monitor;
176: }
177:
178: public boolean visit(IResourceDelta aDelta) {
179: boolean visitChildren = false;
180:
181: IResource resource = aDelta.getResource();
182:
183: if (resource instanceof IProject) {
184: // Only check projects with So6 nature
185: visitChildren = So6Util
186: .isSo6Project((IProject) resource);
187: } else if (resource instanceof IFile
188: || resource instanceof IFolder) {
189: switch (aDelta.getKind()) {
190: case IResourceDelta.ADDED:
191: findConflict(resource, monitor);
192:
193: break;
194:
195: case IResourceDelta.CHANGED:
196: findConflict(resource, monitor);
197:
198: break;
199:
200: case IResourceDelta.REMOVED:
201: break;
202: }
203: }
204:
205: return visitChildren;
206: }
207: }
208:
209: private class Visitor implements IResourceVisitor {
210: private IProgressMonitor monitor;
211:
212: public Visitor(IProgressMonitor monitor) {
213: this .monitor = monitor;
214: }
215:
216: public boolean visit(IResource resource) {
217: if (resource instanceof IFile
218: || resource instanceof IFolder) {
219: findConflict(resource, monitor);
220: }
221:
222: return true;
223: }
224: }
225: }
|