org.symbian.tools.wrttools.previewer/src/org/symbian/tools/wrttools/previewer/http/handlers/ProjectIndexResourceProvider.java
changeset 345 7723a46fe224
parent 344 0f103d99721b
equal deleted inserted replaced
344:0f103d99721b 345:7723a46fe224
    23 import java.io.InputStream;
    23 import java.io.InputStream;
    24 import java.util.Map;
    24 import java.util.Map;
    25 import java.util.regex.Matcher;
    25 import java.util.regex.Matcher;
    26 import java.util.regex.Pattern;
    26 import java.util.regex.Pattern;
    27 
    27 
       
    28 import org.eclipse.core.resources.IFile;
    28 import org.eclipse.core.resources.IProject;
    29 import org.eclipse.core.resources.IProject;
    29 import org.eclipse.core.resources.ResourcesPlugin;
    30 import org.eclipse.core.resources.ResourcesPlugin;
    30 import org.eclipse.core.runtime.CoreException;
    31 import org.eclipse.core.runtime.CoreException;
    31 import org.eclipse.core.runtime.IPath;
    32 import org.eclipse.core.runtime.IPath;
    32 import org.json.simple.JSONObject;
    33 import org.json.simple.JSONObject;
       
    34 import org.symbian.tools.wrttools.previewer.PreviewerPlugin;
    33 import org.symbian.tools.wrttools.util.CoreUtil;
    35 import org.symbian.tools.wrttools.util.CoreUtil;
       
    36 import org.symbian.tools.wrttools.util.ProjectUtils;
    34 
    37 
    35 public class ProjectIndexResourceProvider implements IResourceProvider {
    38 public class ProjectIndexResourceProvider implements IResourceProvider {
    36     public static final String INDEX = "wrt_preview_main.html";
    39     public static final String INDEX = "wrt_preview_main.html";
    37     private static final Pattern HEAD_TAG_PATTERN = Pattern.compile("<head(\\s*\\w*=\"(^\")*\")*\\s*>",
    40     private static final Pattern HEAD_TAG_PATTERN = Pattern.compile("<head(\\s*\\w*=\"(^\")*\")*\\s*>",
    38             Pattern.CASE_INSENSITIVE);
    41             Pattern.CASE_INSENSITIVE);
    41     public String[] getPaths() {
    44     public String[] getPaths() {
    42         return new String[] { INDEX };
    45         return new String[] { INDEX };
    43     }
    46     }
    44 
    47 
    45     public InputStream getResourceStream(IProject project, IPath resource, Map<String, String[]> parameters,
    48     public InputStream getResourceStream(IProject project, IPath resource, Map<String, String[]> parameters,
    46             String sessionId)
    49             String sessionId) throws IOException, CoreException {
    47             throws IOException, CoreException {
       
    48         return getProjectIndexPage(project.getName());
    50         return getProjectIndexPage(project.getName());
    49     }
    51     }
    50 
    52 
    51     private InputStream getProjectIndexPage(String projectName) throws IOException, CoreException {
    53     private InputStream getProjectIndexPage(String projectName) throws IOException, CoreException {
    52         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    54         IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    53         if (project.isAccessible()) {
    55         if (project.isAccessible()) {
    54             String indexFileName = CoreUtil.getIndexFile(project);
    56             String indexFileName = CoreUtil.getIndexFile(project);
    55             if (indexFileName != null) {
    57             if (indexFileName != null) {
    56                 String string = CoreUtil.readFile(project, CoreUtil.getFile(project, indexFileName));
    58                 final IFile file = CoreUtil.getFile(project, indexFileName);
    57                 if (string != null) {
    59                 if (!ProjectUtils.isExcluded(file)) {
    58                     Matcher matcher = HEAD_TAG_PATTERN.matcher(string);
    60                     String string = CoreUtil.readFile(project, file);
    59                     if (matcher.find()) {
    61                     if (string != null) {
    60                         string = matcher.replaceFirst(matcher.group() + SCRIPT);
    62                         Matcher matcher = HEAD_TAG_PATTERN.matcher(string);
       
    63                         if (matcher.find()) {
       
    64                             string = matcher.replaceFirst(matcher.group() + SCRIPT);
       
    65                         }
       
    66                         return new ByteArrayInputStream(string.getBytes("UTF-8"));
    61                     }
    67                     }
    62                     return new ByteArrayInputStream(string.getBytes("UTF-8"));
       
    63                 }
    68                 }
    64             }
    69             }
       
    70             PreviewerPlugin.print(String.format("Can not find main page (%s) in project %s.\n", indexFileName,
       
    71                     project.getName()));
    65         }
    72         }
    66         return null;
    73         return null;
    67     }
    74     }
    68 
    75 
    69     public void post(IProject project, IPath resource, Map<String, String[]> parameterMap, JSONObject object,
    76     public void post(IProject project, IPath resource, Map<String, String[]> parameterMap, JSONObject object,
    70             String sessionId)
    77             String sessionId) throws IOException, CoreException {
    71             throws IOException, CoreException {
       
    72         // Do nothing
    78         // Do nothing
    73     }
    79     }
    74 
    80 
    75 }
    81 }