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.core;
034:
035: import org.eclipse.core.resources.IFile;
036: import org.eclipse.core.resources.IMarker;
037: import org.eclipse.core.resources.IProject;
038: import org.eclipse.core.resources.IResource;
039: import org.eclipse.core.resources.IncrementalProjectBuilder;
040: import org.eclipse.core.runtime.CoreException;
041: import org.eclipse.core.runtime.IProgressMonitor;
042: import org.eclipse.core.runtime.QualifiedName;
043:
044: import org.eclipse.jdt.core.IJavaProject;
045:
046: import org.eclipse.jface.wizard.WizardDialog;
047:
048: import org.eclipse.ui.internal.Workbench;
049:
050: import org.libresource.so6.core.WsConnection;
051: import org.libresource.so6.core.engine.OpVector;
052: import org.libresource.so6.core.engine.OpVectorFsImpl;
053: import org.libresource.so6.core.engine.PatchFile;
054: import org.libresource.so6.core.engine.util.FileUtils;
055: import org.libresource.so6.core.engine.util.InfoPatchHandler;
056: import org.libresource.so6.plugin.Plugin;
057: import org.libresource.so6.plugin.compare.Patch;
058: import org.libresource.so6.plugin.properties.So6PropertyPage;
059: import org.libresource.so6.plugin.ui.So6ResourceUILabelDecorator;
060: import org.libresource.so6.plugin.wizards.CommitWizard;
061: import org.libresource.so6.plugin.wizards.UpdateWizard;
062:
063: /**
064: * @author Guillaume Bort
065: * @author Sebastien Jourdain
066: */
067: import java.io.File;
068: import java.io.FileInputStream;
069: import java.io.FileOutputStream;
070: import java.io.InputStream;
071:
072: import java.util.ArrayList;
073: import java.util.Collections;
074: import java.util.Date;
075: import java.util.Vector;
076:
077: import javax.xml.parsers.SAXParser;
078: import javax.xml.parsers.SAXParserFactory;
079:
080: /**
081: * DOCUMENT ME!
082: *
083: * @author $author$
084: * @version $Revision: 1.3 $
085: */
086: public class So6Util {
087: /**
088: * DOCUMENT ME!
089: *
090: * @param resource DOCUMENT ME!
091: *
092: * @throws Exception DOCUMENT ME!
093: */
094: public static void addToIgnoreFile(IResource resource)
095: throws Exception {
096: IProject project = resource.getProject();
097: String content = getIgnoreFileContent(project);
098: String path = resource.getProjectRelativePath().toString();
099: String ignorePattern = ":"
100: + path.replaceAll("\\.", "\\\\.").replaceAll("/", ":")
101: + "$";
102: setIgnoreFileContent(project, content.trim() + "\n"
103: + ignorePattern);
104: }
105:
106: /**
107: * DOCUMENT ME!
108: *
109: * @param project DOCUMENT ME!
110: *
111: * @throws Exception DOCUMENT ME!
112: */
113: public static void checkIfSo6PropertiesExist(IProject project)
114: throws Exception {
115: if (!new File(getAbsoluteSo6PropertiesPath(project)).exists()) {
116: throw new Exception(
117: "The propertie file "
118: + getSo6PropertiesPath(project)
119: + " does not exist. \nPlease configure first the so6.properties file path in the project properties dialog.");
120: }
121: }
122:
123: /**
124: * DOCUMENT ME!
125: *
126: * @param project DOCUMENT ME!
127: *
128: * @throws Exception DOCUMENT ME!
129: */
130: public static void commit(IProject project) throws Exception {
131: checkIfSo6PropertiesExist(project);
132:
133: CommitWizard wizard = new CommitWizard(project);
134: WizardDialog dialog = new WizardDialog(Workbench.getInstance()
135: .getActiveWorkbenchWindow().getShell(), wizard);
136: dialog.open();
137: }
138:
139: /**
140: * DOCUMENT ME!
141: *
142: * @param file DOCUMENT ME!
143: * @param message DOCUMENT ME!
144: * @param severity DOCUMENT ME!
145: * @param line DOCUMENT ME!
146: */
147: public static void createProblemMarker(IFile file, String message,
148: int severity, int line) {
149: if ((file != null) && file.isAccessible()) {
150: try {
151: IMarker marker = file.createMarker(Plugin.ID
152: + ".conflictmarker");
153: marker.setAttribute(IMarker.MESSAGE, message);
154: marker.setAttribute(IMarker.SEVERITY, severity);
155:
156: if (line > 0) {
157: marker.setAttribute(IMarker.LINE_NUMBER, line);
158: }
159: } catch (CoreException e) {
160: e.printStackTrace();
161: }
162: }
163: }
164:
165: /**
166: * DOCUMENT ME!
167: *
168: * @param resource DOCUMENT ME!
169: * @param message DOCUMENT ME!
170: * @param severity DOCUMENT ME!
171: */
172: public static void createProblemMarker(IResource resource,
173: String message, int severity) {
174: if ((resource != null) && resource.isAccessible()) {
175: try {
176: IMarker marker = resource.createMarker(Plugin.ID
177: + ".conflictmarker");
178: marker.setAttribute(IMarker.MESSAGE, message);
179: marker.setAttribute(IMarker.SEVERITY, severity);
180: } catch (CoreException e) {
181: e.printStackTrace();
182: }
183: }
184: }
185:
186: /**
187: * DOCUMENT ME!
188: *
189: * @param project DOCUMENT ME!
190: */
191: public static void deleteProblemMarkers(IProject project) {
192: if ((project != null) && project.isAccessible()) {
193: try {
194: project.deleteMarkers(Plugin.ID + ".conflictmarker",
195: false, IResource.DEPTH_INFINITE);
196: } catch (CoreException e) {
197: e.printStackTrace();
198: }
199: }
200: }
201:
202: /**
203: * DOCUMENT ME!
204: *
205: * @param resource DOCUMENT ME!
206: */
207: public static void deleteProblemMarkersOnResource(IResource resource) {
208: if ((resource != null) && resource.isAccessible()) {
209: try {
210: resource.deleteMarkers(Plugin.ID + ".conflictmarker",
211: false, IResource.DEPTH_ZERO);
212: } catch (CoreException e) {
213: e.printStackTrace();
214: }
215: }
216: }
217:
218: /**
219: * DOCUMENT ME!
220: *
221: * @param file DOCUMENT ME!
222: *
223: * @return DOCUMENT ME!
224: *
225: * @throws Exception DOCUMENT ME!
226: */
227: public static boolean existRefCopy(IResource file) throws Exception {
228: IProject project = file.getProject();
229:
230: if (!isSo6Project(project)) {
231: return false;
232: }
233:
234: try {
235: checkIfSo6PropertiesExist(project);
236: } catch (Exception e) {
237: return false;
238: }
239:
240: WsConnection wsc = getWsConnection(project);
241: String path = file.getProjectRelativePath().toString();
242:
243: return wsc.getRefCopy().exists(path);
244: }
245:
246: /**
247: * DOCUMENT ME!
248: *
249: * @param project DOCUMENT ME!
250: *
251: * @return DOCUMENT ME!
252: */
253: public static String getAbsoluteSo6IgnoreFile(IProject project) {
254: File file = new File(project.getLocation().toFile(),
255: ".ignore.so6");
256:
257: return file.getAbsolutePath();
258: }
259:
260: /**
261: * DOCUMENT ME!
262: *
263: * @param project DOCUMENT ME!
264: *
265: * @return DOCUMENT ME!
266: */
267: public static String getAbsoluteSo6PropertiesPath(IProject project) {
268: File file = new File(project.getLocation().toFile(),
269: getSo6PropertiesPath(project));
270:
271: return file.getAbsolutePath();
272: }
273:
274: /**
275: * DOCUMENT ME!
276: *
277: * @param project DOCUMENT ME!
278: *
279: * @return DOCUMENT ME!
280: *
281: * @throws Exception DOCUMENT ME!
282: */
283: public static String getIgnoreFileContent(IProject project)
284: throws Exception {
285: File ignoreFile = new File(getAbsoluteSo6IgnoreFile(project));
286:
287: if (ignoreFile.exists()) {
288: FileInputStream fis = new FileInputStream(ignoreFile);
289: byte[] data = new byte[(int) ignoreFile.length()];
290: fis.read(data);
291: fis.close();
292:
293: return new String(data);
294: }
295:
296: return "";
297: }
298:
299: /**
300: * DOCUMENT ME!
301: *
302: * @param resource DOCUMENT ME!
303: * @param monitor DOCUMENT ME!
304: *
305: * @return DOCUMENT ME!
306: */
307: public static Vector getPatchs(IResource resource,
308: IProgressMonitor monitor) {
309: try {
310: WsConnection connection = getWsConnection(resource
311: .getProject());
312: File[] files = connection.getAppliedPatch().list();
313: File attachDir = FileUtils.createTmpDir();
314: ArrayList filters = new ArrayList();
315: filters.add(resource.getProjectRelativePath().toString());
316:
317: Vector validPatchs = new Vector();
318: Patch previous = null;
319: monitor.beginTask("Parsing applied patchs", files.length);
320:
321: for (int i = 0; i < files.length; i++) {
322: monitor.worked(i + 1);
323: monitor.subTask("Parsing patch " + files[i].getName());
324:
325: if (monitor.isCanceled()) {
326: return new Vector();
327: }
328:
329: File tmpDir = FileUtils.createTmpDir();
330: OpVector cmds = new OpVectorFsImpl(tmpDir.getPath());
331: PatchFile patchFile = new PatchFile(files[i].getPath());
332: patchFile.buildOpVector(cmds, attachDir.getPath(),
333: filters);
334:
335: if (cmds.size() > 0) {
336: SAXParser parser = SAXParserFactory.newInstance()
337: .newSAXParser();
338: InfoPatchHandler handler = new InfoPatchHandler();
339: parser.parse(files[i].getPath(), handler);
340:
341: Patch patch = new Patch(handler.getToTicket(),
342: handler.getWsName(), new Date(), handler
343: .getComment(), cmds, previous);
344: validPatchs.add(patch);
345: previous = patch;
346: }
347: }
348:
349: Collections.reverse(validPatchs);
350:
351: return validPatchs;
352: } catch (Exception e) {
353: e.printStackTrace();
354:
355: return new Vector();
356: }
357: }
358:
359: /**
360: * DOCUMENT ME!
361: *
362: * @param project DOCUMENT ME!
363: *
364: * @return DOCUMENT ME!
365: */
366: public static IResource getPrivateSo6DirectoryResource(
367: IProject project) {
368: return project.getFolder(".so6");
369: }
370:
371: /**
372: * DOCUMENT ME!
373: *
374: * @param resource DOCUMENT ME!
375: *
376: * @return DOCUMENT ME!
377: *
378: * @throws Exception DOCUMENT ME!
379: */
380: public static File getRefCopy(IResource resource) throws Exception {
381: IProject project = resource.getProject();
382: WsConnection wsc = getWsConnection(project);
383: String path = resource.getProjectRelativePath().toString();
384:
385: return new File(wsc.getRefCopy().getPath(path));
386: }
387:
388: /**
389: * DOCUMENT ME!
390: *
391: * @param file DOCUMENT ME!
392: *
393: * @return DOCUMENT ME!
394: *
395: * @throws Exception DOCUMENT ME!
396: */
397: public static InputStream getRefCopyContent(IFile file)
398: throws Exception {
399: IProject project = file.getProject();
400: WsConnection wsc = getWsConnection(project);
401: String path = file.getProjectRelativePath().toString();
402:
403: return new FileInputStream(wsc.getRefCopy().getPath(path));
404: }
405:
406: /**
407: * DOCUMENT ME!
408: *
409: * @param project DOCUMENT ME!
410: *
411: * @return DOCUMENT ME!
412: */
413: public static IResource getSo6IgnoreFileResource(IProject project) {
414: return project.getFile(".ignore.so6");
415: }
416:
417: /**
418: * DOCUMENT ME!
419: *
420: * @param project DOCUMENT ME!
421: *
422: * @return DOCUMENT ME!
423: */
424: public static String getSo6PropertiesPath(IProject project) {
425: try {
426: return ((IResource) project)
427: .getPersistentProperty(new QualifiedName("",
428: So6PropertyPage.PATH_PROPERTY));
429: } catch (CoreException e) {
430: return So6PropertyPage.DEFAULT_PATH;
431: }
432: }
433:
434: /**
435: * DOCUMENT ME!
436: *
437: * @param project DOCUMENT ME!
438: *
439: * @return DOCUMENT ME!
440: *
441: * @throws Exception DOCUMENT ME!
442: */
443: public static WsConnection getWsConnection(IProject project)
444: throws Exception {
445: return new WsConnection(getAbsoluteSo6PropertiesPath(project));
446: }
447:
448: /**
449: * DOCUMENT ME!
450: *
451: * @param resource DOCUMENT ME!
452: *
453: * @return DOCUMENT ME!
454: *
455: * @throws Exception DOCUMENT ME!
456: */
457: public static boolean isIgnored(IResource resource)
458: throws Exception {
459: IProject project = resource.getProject();
460: String content = getIgnoreFileContent(project);
461: String[] regexps = content.split("\n");
462:
463: for (int i = 0; i < regexps.length; i++) {
464: if (("/" + resource.getProjectRelativePath().toString())
465: .replaceAll("/", ":").matches(regexps[i])) {
466: return true;
467: }
468: }
469:
470: IResource parent = resource.getParent();
471:
472: if (parent instanceof IProject) { // || parent instanceof IJavaProject)
473:
474: return false;
475: }
476:
477: return isIgnored(parent);
478: }
479:
480: /*
481: public static void addConflictFinder(IProject project) throws CoreException {
482: IProjectDescription desc = project.getDescription();
483: ICommand[] builderCommands = desc.getBuildSpec();
484: boolean found = false;
485: for (int i = 0; i < builderCommands.length; i++) {
486: if (builderCommands[i].getBuilderName().equals(ConflictsFinder.ID)) {
487: found = true;
488: break;
489: }
490: }
491: if (!found) {
492: ICommand command = desc.newCommand();
493: command.setBuilderName(ConflictsFinder.ID);
494: ICommand[] newCommands = new ICommand[builderCommands.length + 1];
495: System.arraycopy(builderCommands, 0, newCommands, 1, builderCommands.length);
496: newCommands[0] = command;
497: desc.setBuildSpec(newCommands);
498: project.setDescription(desc, null);
499: }
500: }
501: */
502:
503: /**
504: * DOCUMENT ME!
505: *
506: * @param project DOCUMENT ME!
507: *
508: * @return DOCUMENT ME!
509: */
510: public static boolean isSo6Project(IProject project) {
511: if ((project != null) && project.isAccessible()) {
512: try {
513: return So6TeamProvider.ID
514: .equals(project
515: .getPersistentProperty(new QualifiedName(
516: "org.eclipse.team.core",
517: "repository")));
518: } catch (CoreException e) {
519: e.printStackTrace();
520: }
521: }
522:
523: return false;
524: }
525:
526: /**
527: * DOCUMENT ME!
528: *
529: * @param project DOCUMENT ME!
530: *
531: * @return DOCUMENT ME!
532: *
533: * @throws Exception DOCUMENT ME!
534: */
535: public static boolean isUpToDate(IProject project) throws Exception {
536: checkIfSo6PropertiesExist(project);
537:
538: WsConnection wsc = new WsConnection(
539: getAbsoluteSo6PropertiesPath(project));
540: long server = wsc.getClient().getLastTicket();
541: long local = wsc.getNs();
542:
543: return server == local;
544: }
545:
546: /**
547: * DOCUMENT ME!
548: *
549: * @param project DOCUMENT ME!
550: * @param monitor DOCUMENT ME!
551: *
552: * @throws Exception DOCUMENT ME!
553: */
554: public static void refresh(IProject project,
555: IProgressMonitor monitor) throws Exception {
556: deleteProblemMarkers(project);
557: project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
558: project.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
559: So6ResourceUILabelDecorator.update();
560:
561: // mark project configuration error
562: if (!existRefCopy(project)) {
563: createProblemMarker(project,
564: "So6 configuration error : "
565: + getSo6PropertiesPath(project)
566: + " does not exist", IMarker.SEVERITY_ERROR);
567: }
568: }
569:
570: /**
571: * DOCUMENT ME!
572: *
573: * @param project DOCUMENT ME!
574: * @param content DOCUMENT ME!
575: *
576: * @throws Exception DOCUMENT ME!
577: */
578: public static void setIgnoreFileContent(IProject project,
579: String content) throws Exception {
580: File ignoreFile = new File(getAbsoluteSo6IgnoreFile(project));
581: FileOutputStream fos = new FileOutputStream(ignoreFile);
582: fos.write(content.getBytes());
583: fos.close();
584: }
585:
586: /**
587: * DOCUMENT ME!
588: *
589: * @param project DOCUMENT ME!
590: * @param path DOCUMENT ME!
591: *
592: * @throws Exception DOCUMENT ME!
593: */
594: public static void setSo6PropertiesPath(IProject project,
595: String path) throws Exception {
596: ((IResource) project).setPersistentProperty(new QualifiedName(
597: "", So6PropertyPage.PATH_PROPERTY), path);
598:
599: // refresh
600: refresh(project, null);
601: }
602:
603: /**
604: * DOCUMENT ME!
605: *
606: * @param project DOCUMENT ME!
607: *
608: * @throws Exception DOCUMENT ME!
609: */
610: public static void update(IProject project) throws Exception {
611: checkIfSo6PropertiesExist(project);
612:
613: UpdateWizard wizard = new UpdateWizard(project);
614: WizardDialog dialog = new WizardDialog(Workbench.getInstance()
615: .getActiveWorkbenchWindow().getShell(), wizard);
616: dialog.open();
617: }
618: }
|