applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / objects / utils / AppDataReference.java
index 058f9e6..efdf0c4 100644 (file)
-/**
- * 
- */
-package uk.ac.vamsas.objects.utils;
-import java.util.Vector;
-
-
-import uk.ac.vamsas.client.ClientHandle;
-import uk.ac.vamsas.client.UserHandle;
-import uk.ac.vamsas.client.simpleclient.VamsasArchive;
-import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
-import uk.ac.vamsas.objects.core.*;
-/**
- * Form, accessors and validation for ApplicationData references in
- * vamsas document.
- * TODO: LATER:extend XML Schema to properly validate against the same forms required by this class
- * TODO: VAMSAS: URNS for appDatas are supposed to be unique, aren't they ?
- */
-public class AppDataReference {
-  /**
-   * search interface for collecting particular types of AppDatas in a vamsas document
-   * @author jimp
-   *
-   */
-  interface IAppDSearch {
-    /**
-     * process the appData Vobject d
-     * @param d
-     * @return true if appData should be collected
-     */
-    public boolean process(AppData d);
-  }
-  /**
-   * collect all appData reference strings in a vamsas document
-   * @param doc
-   * @return vector of String objects
-   */
-  static public Vector getAppDataReferences(VamsasDocument doc) {
-    if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
-      Vector apdrefs = new Vector();
-      ApplicationData[] appdatas = doc.getApplicationData();
-      for (int q=0; q<appdatas.length; q++) {
-        String refstring=appdatas[q].getDataReference();
-        if (refstring!=null) 
-          apdrefs.add(refstring);
-        User users[] = appdatas[q].getUser();
-        
-        if (users!=null)
-          for (int u=0; u<users.length; u++) {
-            refstring=users[u].getDataReference();
-            if (refstring!=null)
-              apdrefs.add(new String(refstring)); // avoid referencing.
-          }
-      }
-      if (apdrefs.size()>0)
-        return apdrefs;
-    }
-    return null;
-  }
-  /**
-   * General search through the set of AppData objects for a particular profile of Client and User handle.
-   * @param doc
-   * @param test interface implemented by the filter selecting particular AppDatas.
-   * @param cascade if true only User objects for ApplicationData objects that test.process returned true will be tested.
-   * @return set of uk.ac.vamsas.objects.core.AppData objects for which test.process returned true
-   */
-  static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test, boolean cascade) {
-    if ((doc!=null) && (doc.getApplicationDataCount()>0)) {
-      Vector apdrefs = new Vector();
-      ApplicationData[] appdatas = doc.getApplicationData();
-      for (int q=0; q<appdatas.length; q++) {
-        boolean t;
-        if (t=test.process(appdatas[q])) 
-          apdrefs.add(appdatas[q]);
-        if (t || cascade) { 
-          User users[] = appdatas[q].getUser();
-          if (users!=null)
-            for (int u=0; u<users.length; u++) 
-              if (test.process(users[u]))
-                apdrefs.add(users[u]);
-        }
-      }
-      if (apdrefs.size()>0)
-        return apdrefs;
-    }
-    return null;
-  }
-  static public boolean equals(User p, UserHandle u) {
-    if (p.getFullname().equals(u.getFullName())
-        && p.getOrganization().equals(u.getOrganization()))
-      return true;
-    return false;
-  }
-  /**
-   * returns true if Name matches in c and p, and Urn's match (or c.getUrn()==null) and Version's match (or c.getVersion()==null)
-   * @param p
-   * @param c
-   * @return match of p on template c.
-   */
-  static public boolean equals(ApplicationData p, ClientHandle c) {
-    if (
-        //((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))
-        //&&
-        (p.getName().equals(c.getClientName()))
-        &&
-        ((c.getVersion()==null) || (p.getVersion().equals(c.getVersion())))
-        )
-      return true;
-    return false;
-  }
-  /**
-   * Searches document appData structure for particular combinations of client and user data
-   * @param doc the data 
-   * @param user template user data to match against
-   * @see AppDataReference.equals(uk.ac.vamsas.objects.core.User, uk.ac.vamsas.client.UserHandle)
-   * @param app
-   * @see AppDataReference.equals(uk.ac.vamsas.objects.core.ApplicationData, uk.ac.vamsas.client.ClientHandle)
-   * @return set of matching client app datas for this client and user combination
-   */
-  static public Vector getUserandApplicationsData(VamsasDocument doc, UserHandle user, ClientHandle app) {
-    if (doc==null) {
-      return null;
-    }
-    final UserHandle u = user;
-    final ClientHandle c = app;
-    
-    IAppDSearch match = new IAppDSearch() {
-      public boolean process(AppData p) {
-        if (p instanceof User) {
-          if (AppDataReference.equals((User) p, u))
-            return true;
-        } else 
-        if (p instanceof ApplicationData) {
-          if (AppDataReference.equals((ApplicationData) p, c))
-            return true;
-        }
-        return false;
-      }
-    };
-    
-    return searchAppDatas(doc, match, true); // only return AppDatas belonging to appdata app.
-  }
-  /**
-   * safely creates a new appData reference
-   * @param dest destination document Vobject
-   * @param entry base application reference to make unique 
-   */
-  public static String uniqueAppDataReference(VamsasDocument dest,String base) {
-    String urn = base.replace('/','_').replace('\\','_').replace(':', '_').replace('.', '_');
-    int v = 1;
-    for (int i=0, j=dest.getApplicationDataCount(); i<j; i++) {
-      ApplicationData o = dest.getApplicationData()[i];
-      // ensure new urn is really unique
-      while (o.getDataReference()!=null && o.getDataReference().equals(urn)) {
-        urn = base+"_"+v++;      
-      } 
-    }
-    return urn;
-  }
-}
+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ *  Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *  \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package uk.ac.vamsas.objects.utils;\r
+\r
+import java.util.Vector;\r
+\r
+import uk.ac.vamsas.client.ClientHandle;\r
+import uk.ac.vamsas.client.UserHandle;\r
+import uk.ac.vamsas.client.simpleclient.VamsasArchive;\r
+import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;\r
+import uk.ac.vamsas.objects.core.*;\r
+\r
+/**\r
+ * Form, accessors and validation for ApplicationData references in vamsas\r
+ * document. TODO: LATER:extend XML Schema to properly validate against the same\r
+ * forms required by this class TODO: VAMSAS: URNS for appDatas are supposed to\r
+ * be unique, aren't they ?\r
+ */\r
+public class AppDataReference {\r
+  /**\r
+   * search interface for collecting particular types of AppDatas in a vamsas\r
+   * document\r
+   * \r
+   * @author jimp\r
+   * \r
+   */\r
+  interface IAppDSearch {\r
+    /**\r
+     * process the appData Vobject d\r
+     * \r
+     * @param d\r
+     * @return true if appData should be collected\r
+     */\r
+    public boolean process(AppData d);\r
+  }\r
+\r
+  /**\r
+   * collect all appData reference strings in a vamsas document\r
+   * \r
+   * @param doc\r
+   * @return vector of String objects\r
+   */\r
+  static public Vector getAppDataReferences(VamsasDocument doc) {\r
+    if ((doc != null) && (doc.getApplicationDataCount() > 0)) {\r
+      Vector apdrefs = new Vector();\r
+      ApplicationData[] appdatas = doc.getApplicationData();\r
+      for (int q = 0; q < appdatas.length; q++) {\r
+        String refstring = appdatas[q].getDataReference();\r
+        if (refstring != null)\r
+          apdrefs.add(refstring);\r
+        User users[] = appdatas[q].getUser();\r
+\r
+        if (users != null)\r
+          for (int u = 0; u < users.length; u++) {\r
+            refstring = users[u].getDataReference();\r
+            if (refstring != null)\r
+              apdrefs.add(new String(refstring)); // avoid referencing.\r
+          }\r
+      }\r
+      if (apdrefs.size() > 0)\r
+        return apdrefs;\r
+    }\r
+    return null;\r
+  }\r
+\r
+  /**\r
+   * General search through the set of AppData objects for a particular profile\r
+   * of Client and User handle.\r
+   * \r
+   * @param doc\r
+   * @param test\r
+   *          interface implemented by the filter selecting particular AppDatas.\r
+   * @param cascade\r
+   *          if true only User objects for ApplicationData objects that\r
+   *          test.process returned true will be tested.\r
+   * @return set of uk.ac.vamsas.objects.core.AppData objects for which\r
+   *         test.process returned true\r
+   */\r
+  static public Vector searchAppDatas(VamsasDocument doc, IAppDSearch test,\r
+      boolean cascade) {\r
+    if ((doc != null) && (doc.getApplicationDataCount() > 0)) {\r
+      Vector apdrefs = new Vector();\r
+      ApplicationData[] appdatas = doc.getApplicationData();\r
+      for (int q = 0; q < appdatas.length; q++) {\r
+        boolean t;\r
+        if (t = test.process(appdatas[q]))\r
+          apdrefs.add(appdatas[q]);\r
+        if (t || cascade) {\r
+          User users[] = appdatas[q].getUser();\r
+          if (users != null)\r
+            for (int u = 0; u < users.length; u++)\r
+              if (test.process(users[u]))\r
+                apdrefs.add(users[u]);\r
+        }\r
+      }\r
+      if (apdrefs.size() > 0)\r
+        return apdrefs;\r
+    }\r
+    return null;\r
+  }\r
+\r
+  static public boolean equals(User p, UserHandle u) {\r
+    if (p.getFullname().equals(u.getFullName())\r
+        && p.getOrganization().equals(u.getOrganization()))\r
+      return true;\r
+    return false;\r
+  }\r
+\r
+  /**\r
+   * returns true if Name matches in c and p, and Urn's match (or\r
+   * c.getUrn()==null) and Version's match (or c.getVersion()==null)\r
+   * \r
+   * @param p\r
+   * @param c\r
+   * @return match of p on template c.\r
+   */\r
+  static public boolean equals(ApplicationData p, ClientHandle c) {\r
+    if (\r
+    // ((c.getClientUrn()==null) || p.getUrn().equals(c.getClientUrn()))\r
+    // &&\r
+    (p.getName().equals(c.getClientName()))\r
+        && ((c.getVersion() == null) || (p.getVersion().equals(c.getVersion()))))\r
+      return true;\r
+    return false;\r
+  }\r
+\r
+  /**\r
+   * Searches document appData structure for particular combinations of client\r
+   * and user data\r
+   * \r
+   * @param doc\r
+   *          the data\r
+   * @param user\r
+   *          template user data to match against\r
+   * @see AppDataReference.equals(uk.ac.vamsas.objects.core.User,\r
+   *      uk.ac.vamsas.client.UserHandle)\r
+   * @param app\r
+   * @see AppDataReference.equals(uk.ac.vamsas.objects.core.ApplicationData,\r
+   *      uk.ac.vamsas.client.ClientHandle)\r
+   * @return set of matching client app datas for this client and user\r
+   *         combination\r
+   */\r
+  static public Vector getUserandApplicationsData(VamsasDocument doc,\r
+      UserHandle user, ClientHandle app) {\r
+    if (doc == null) {\r
+      return null;\r
+    }\r
+    final UserHandle u = user;\r
+    final ClientHandle c = app;\r
+\r
+    IAppDSearch match = new IAppDSearch() {\r
+      public boolean process(AppData p) {\r
+        if (p instanceof User) {\r
+          if (AppDataReference.equals((User) p, u))\r
+            return true;\r
+        } else if (p instanceof ApplicationData) {\r
+          if (AppDataReference.equals((ApplicationData) p, c))\r
+            return true;\r
+        }\r
+        return false;\r
+      }\r
+    };\r
+\r
+    return searchAppDatas(doc, match, true); // only return AppDatas belonging\r
+                                             // to appdata app.\r
+  }\r
+\r
+  /**\r
+   * safely creates a new appData reference\r
+   * \r
+   * @param dest\r
+   *          destination document Vobject\r
+   * @param entry\r
+   *          base application reference to make unique\r
+   */\r
+  public static String uniqueAppDataReference(VamsasDocument dest, String base) {\r
+    String urn = base.replace('/', '_').replace('\\', '_').replace(':', '_')\r
+        .replace('.', '_');\r
+    int v = 1;\r
+    for (int i = 0, j = dest.getApplicationDataCount(); i < j; i++) {\r
+      ApplicationData o = dest.getApplicationData()[i];\r
+      // ensure new urn is really unique\r
+      while (o.getDataReference() != null && o.getDataReference().equals(urn)) {\r
+        urn = base + "_" + v++;\r
+      }\r
+    }\r
+    return urn;\r
+  }\r
+}\r