All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.image.Aperture Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.image;

import java.io.File;
import java.io.StringReader;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;

import com.hfg.util.Executor;
import com.hfg.util.StringUtil;
import com.hfg.xml.parser.AbstractContentHandler;
import com.hfg.xml.parser.SaxyParser;

//------------------------------------------------------------------------------
/**
 * Functions for interfacing with Apple's Aperture.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class Aperture
{
   private static File mAperturePlistFile = new File(System.getProperties().getProperty("user.home")
                                                     + "/Library/Preferences/com.apple.Aperture.plist");

   //###########################################################################
   // PUBLIC FUNCTIONS
   //###########################################################################

   //--------------------------------------------------------------------------
   /**
    This function retrieves the current default Aperture library path.
    */
   public static File getLibraryPath()
         throws Exception
   {
      if (! mAperturePlistFile.exists())
      {
         throw new RuntimeException("It does not appear that Aperture is installed on this system.");
      }

      String cmd = "/usr/bin/plutil -convert xml1 -o - " + mAperturePlistFile.getPath();
      Executor executor = new Executor(cmd);
      int retVal = executor.exec();
      if (retVal != 0)
      {
         throw new RuntimeException("Error executing " + StringUtil.singleQuote(cmd) + ": " + executor.getSTDERR());
      }

      /*
      Looking for:
              ...
              LibraryPath
              /Photos/Aperture Library.aplibrary
              ...
      */
      PlistContentHandler handler = new Aperture().new PlistContentHandler("LibraryPath");
      SaxyParser parser = new SaxyParser();
      parser.setContentHandler(handler);
      parser.parse(new InputSource(new StringReader(executor.getSTDOUT())));

      return new File(handler.getTargetPropertyValue());
   }

   //###########################################################################
   // INNER CLASS
   //###########################################################################

   private class PlistContentHandler extends AbstractContentHandler
   {
      private String mTargetPropertyKey;
      private String mTargetPropertyValue;
      private String mCurrentTagName;
      private boolean mTargetPropertyFound = false;

      //-----------------------------------------------------------------------
      public PlistContentHandler(String inTargetPropertyKey)
      {
         mTargetPropertyKey = inTargetPropertyKey;
      }

      //-----------------------------------------------------------------------
      public String getTargetPropertyValue()
      {
         return mTargetPropertyValue;
      }

      //-----------------------------------------------------------------------
      public void startElement(String inURI, String inLocalName, String inName, Attributes inAttributes)
      {
         mCurrentTagName = inLocalName;
      }

      //-----------------------------------------------------------------------
      public void characters(char[] inChars, int offset, int length)
      {
         if (mCurrentTagName.equals("key"))
         {
            String key = new String(inChars, offset, length);
            if (key.equals(mTargetPropertyKey))
            {
               mTargetPropertyFound = true;
            }
         }
         else if (mCurrentTagName.equals("string")
                  && mTargetPropertyFound)
         {
            mTargetPropertyValue = new String(inChars, offset, length);
            mTargetPropertyFound = false;
         }
      }
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy