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

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

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

import com.hfg.util.Executor;
import com.hfg.util.StringUtil;
import com.hfg.xml.parser.SaxyParser;
import com.hfg.xml.parser.AbstractContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Attributes;

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


//------------------------------------------------------------------------------
/**
 * Functions for interfacing with Apple's iPhoto.
 *
 * @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 IPhoto
{
   private static File mIPhotoPlistFile = new File(System.getProperties().getProperty("user.home")
                                          + "/Library/Preferences/com.apple.iPhoto.plist");

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

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

      String cmd = "/usr/bin/plutil -convert xml1 -o - " + mIPhotoPlistFile.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:
              ...
              RootDirectory
              /Photos/iPhoto Library (2006 - 2007)
              ...
      */
      PlistContentHandler handler = new IPhoto().new PlistContentHandler("RootDirectory");
      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