plugins/org.symbian.tools.tmw.debug/src/org/symbian/tools/wrttools/debug/internal/launch/WebApplicationSourceLocator.java
changeset 471 06589bf52fa7
parent 470 d4809db37847
child 472 bd9f2d7c64a6
equal deleted inserted replaced
470:d4809db37847 471:06589bf52fa7
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.wrttools.debug.internal.launch;
       
    20 
       
    21 import java.io.BufferedWriter;
       
    22 import java.io.File;
       
    23 import java.io.FileWriter;
       
    24 import java.io.IOException;
       
    25 import java.net.URI;
       
    26 import java.net.URISyntaxException;
       
    27 
       
    28 import org.chromium.debug.core.model.StackFrame;
       
    29 import org.chromium.sdk.Script;
       
    30 import org.eclipse.core.filesystem.EFS;
       
    31 import org.eclipse.core.resources.IFile;
       
    32 import org.eclipse.core.runtime.CoreException;
       
    33 import org.eclipse.core.runtime.IPath;
       
    34 import org.eclipse.core.runtime.IStatus;
       
    35 import org.eclipse.core.runtime.Path;
       
    36 import org.eclipse.core.runtime.Status;
       
    37 import org.eclipse.debug.core.model.ISourceLocator;
       
    38 import org.eclipse.debug.core.model.IStackFrame;
       
    39 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
       
    40 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupParticipant;
       
    41 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
       
    42 import org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant;
       
    43 import org.symbian.tools.tmw.previewer.PreviewerPlugin;
       
    44 import org.symbian.tools.tmw.previewer.http.WebappManager;
       
    45 import org.symbian.tools.wrttools.debug.internal.Activator;
       
    46 import org.symbian.tools.wrttools.debug.internal.model.ResourceManager;
       
    47 
       
    48 public final class WebApplicationSourceLocator extends AbstractSourceLookupDirector implements ISourceLocator,
       
    49         ISourceLookupDirector {
       
    50     public static final class WebApplicationSourceLookupParticipant extends AbstractSourceLookupParticipant {
       
    51         private final ResourceManager resourceManager;
       
    52         private final WebApplicationSourceLocator locator;
       
    53 
       
    54         public WebApplicationSourceLookupParticipant(WebApplicationSourceLocator locator,
       
    55                 ResourceManager resourceManager) {
       
    56             this.locator = locator;
       
    57             this.resourceManager = resourceManager;
       
    58         }
       
    59 
       
    60         public String getSourceName(Object object) throws CoreException {
       
    61             Script script = null;
       
    62             if (object instanceof StackFrame) {
       
    63                 script = ((StackFrame) object).getCallFrame().getScript();
       
    64             } else if (object instanceof Script) {
       
    65                 script = (Script) object;
       
    66             } else {
       
    67                 System.out.println("Source lookup request for " + object.getClass());
       
    68             }
       
    69             if (script != null) {
       
    70                 IFile resource = resourceManager.getResource(script);
       
    71                 if (resource != null) {
       
    72                     return resource.getProjectRelativePath().toString();
       
    73                 } else {
       
    74                     return locator.fileUrl(script);
       
    75                 }
       
    76             }
       
    77             return null;
       
    78         }
       
    79     }
       
    80 
       
    81     private final ResourceManager resourceManager;
       
    82 
       
    83     public WebApplicationSourceLocator(ResourceManager resourceManager) {
       
    84         this.resourceManager = resourceManager;
       
    85     }
       
    86 
       
    87     public String fileUrl(Script script) throws CoreException {
       
    88         String name = script.getName();
       
    89         try {
       
    90             URI uri = new URI(name);
       
    91             if ("http".equals(uri.getScheme()) && WebappManager.getHost().equals(uri.getHost())
       
    92                     && WebappManager.getPort() == uri.getPort()) {
       
    93                 final IPath stateLocation = Activator.getDefault().getStateLocation();
       
    94                 final IPath path = stateLocation.append("systemlibraries")
       
    95                         .append(new Path(uri.getPath()).lastSegment());
       
    96                 final File file = path.toFile();
       
    97                 file.getParentFile().mkdirs();
       
    98                 final String source = script.getSource();
       
    99                 final BufferedWriter writer = new BufferedWriter(new FileWriter(file));
       
   100                 try {
       
   101                     writer.write(source);
       
   102                 } finally {
       
   103                     writer.close();
       
   104                 }
       
   105                 return new Path(file.getParentFile().getName()).append(file.getName()).toString();
       
   106             }
       
   107         } catch (URISyntaxException e) {
       
   108             throw new RuntimeException(e);
       
   109         } catch (IOException e) {
       
   110             throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
       
   111                     "Cannot load web runtime system libraries", e));
       
   112         }
       
   113         return null;
       
   114     }
       
   115 
       
   116     public Object getSourceElement(IStackFrame stackFrame) {
       
   117 		if (stackFrame instanceof StackFrame == false) {
       
   118 			return null;
       
   119 		}
       
   120 		StackFrame jsStackFrame = (StackFrame) stackFrame;
       
   121 
       
   122 		Script script = jsStackFrame.getCallFrame().getScript();
       
   123 		if (script == null) {
       
   124 			return null;
       
   125 		}
       
   126 
       
   127 		IFile resource = resourceManager.getResource(script);
       
   128 		if (resource != null) {
       
   129 			return resource;
       
   130 		} else {
       
   131 			File file = PreviewerPlugin.getDefault().getHttpPreviewer().getLocalFile(script.getName());
       
   132             if (file != null) {
       
   133 				try {
       
   134 					return EFS.getStore(file.toURI());
       
   135 				} catch (CoreException e) {
       
   136 					Activator.log(e);
       
   137 				}
       
   138 			}
       
   139 		}
       
   140 		return null;
       
   141 	}
       
   142 
       
   143     public void initializeParticipants() {
       
   144         addParticipants(new ISourceLookupParticipant[] { new WebApplicationSourceLookupParticipant(this,
       
   145                 resourceManager) });
       
   146     }
       
   147 }