/*
 * This file is part of libbluray
 * Copyright (C) 2010  William Hahne
 *
 * 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, see
 * <http://www.gnu.org/licenses/>.
 */
package org.dvb.dsmcc;

import java.io.FileNotFoundException;
import java.io.InterruptedIOException;
import java.net.URL;
import org.bluray.net.BDLocator;
import org.davic.net.InvalidLocatorException;
import org.davic.net.Locator;
import org.videolan.MountException;
import org.videolan.MountManager;

public class ServiceDomain {
    public ServiceDomain()
    {

    }

    public void attach(Locator dvbService, int carouselId)
            throws ServiceXFRException, InterruptedIOException,
            MPEGDeliveryException
    {
        org.videolan.Logger.unimplemented("ServiceDomain", "attach");
        throw new Error("Not implemented");
    }

    public void attach(Locator locator) throws DSMCCException,
            InterruptedIOException, MPEGDeliveryException
    {   
        BDLocator bdl = checkLocator(locator);
        if (bdl == null)
            throw new DSMCCException("invalid BDLocator");
        
        if (!bdl.isJarFileItem())
            throw new DSMCCException("invalid BDLocator");
       
        try {
            this.mountPoint = new DSMCCObject(MountManager.mount(bdl.getJarFileId()));
            this.locator = locator;
        } catch (MountException e) {
            e.printStackTrace();
            throw new DSMCCException("couldn't mount jar");
        }
    }

    public void attach(byte[] NSAPAddress) throws DSMCCException,
            InterruptedIOException, InvalidAddressException,
            MPEGDeliveryException
    {
        org.videolan.Logger.unimplemented("ServiceDomain", "attach");
        throw new Error("Not implemented");
    }

    public void detach() throws NotLoadedException
    {
        if (mountPoint == null)
            throw new NotLoadedException();
    }

    public byte[] getNSAPAddress() throws NotLoadedException
    {
        org.videolan.Logger.unimplemented("ServiceDomain", "getNSAPAddress");
        throw new Error("Not implemented");
    }

    public static URL getURL(Locator locator)
            throws NotLoadedException, InvalidLocatorException,
            FileNotFoundException
    {
        BDLocator bdl = checkLocator(locator);
        if (bdl == null)
            throw new InvalidLocatorException("invalid BDLocator");
        
        if (bdl.isJarFileItem()) {
            String mountPt = MountManager.getMount(bdl.getJarFileId());
            
            if (mountPt == null)
                throw new NotLoadedException();
            
            DSMCCObject obj = new DSMCCObject(mountPt, bdl.getPathSegments());
            
            if (!obj.exists())
                throw new FileNotFoundException();
            
            return obj.getURL();
        }
        
        throw new InvalidLocatorException();
    }

    public DSMCCObject getMountPoint()
    {
        return mountPoint;
    }

    public boolean isNetworkConnectionAvailable()
    {
        org.videolan.Logger.unimplemented("ServiceDomain", "isNetworkConnectionAvailable");
        return false;
    }

    public boolean isAttached()
    {
        return mountPoint != null;
    }

    public Locator getLocator()
    {
        return locator;
    }
    
    private static BDLocator checkLocator(Locator locator)
    {
        if (!(locator instanceof BDLocator))
            return null;
        
        BDLocator bdl = (BDLocator)locator;
        
        if (bdl.isJarFileItem())
            return bdl;
        else
            return null;
    }
    
    private DSMCCObject mountPoint = null;
    private Locator locator = null;
}

@@ -1527,20 +1527,17 @@
 on%0A    %7B
-   
 %0A
+
         
@@ -1655,33 +1655,25 @@
 DLocator%22);%0A
-        
 %0A
+
         if (
@@ -1753,24 +1753,17 @@
 ator%22);%0A
-       
 %0A
+
         
@@ -2952,25 +2952,17 @@
 ator%22);%0A
-        
 %0A
+
         
@@ -3056,37 +3056,25 @@
 rFileId());%0A
-            
 %0A
+
             
@@ -3138,37 +3138,25 @@
 xception();%0A
-            
 %0A
+
             
@@ -3222,29 +3222,17 @@
 nts());%0A
-            
 %0A
+
         
@@ -3309,21 +3309,9 @@
 ();%0A
-            
 %0A
+
     
@@ -3349,25 +3349,17 @@
       %7D%0A
-        
 %0A
+
         
@@ -3798,37 +3798,33 @@
  locator;%0A    %7D%0A
-    
 %0A
+
     private stat
@@ -3942,25 +3942,17 @@
 n null;%0A
-        
 %0A
+
         
@@ -3991,17 +3991,9 @@
 or;%0A
-        
 %0A
+
     
@@ -4089,20 +4089,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri

Remove trailing whitespace