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.previewer.http.handlers; |
|
20 |
|
21 import java.io.ByteArrayInputStream; |
|
22 import java.io.IOException; |
|
23 import java.io.InputStream; |
|
24 import java.util.Map; |
|
25 import java.util.regex.Matcher; |
|
26 import java.util.regex.Pattern; |
|
27 |
|
28 import org.eclipse.core.resources.IFile; |
|
29 import org.eclipse.core.resources.IProject; |
|
30 import org.eclipse.core.resources.ResourcesPlugin; |
|
31 import org.eclipse.core.runtime.CoreException; |
|
32 import org.eclipse.core.runtime.IPath; |
|
33 import org.json.simple.JSONObject; |
|
34 import org.symbian.tools.wrttools.previewer.PreviewerPlugin; |
|
35 import org.symbian.tools.wrttools.util.CoreUtil; |
|
36 import org.symbian.tools.wrttools.util.ProjectUtils; |
|
37 |
|
38 public class ProjectIndexResourceProvider implements IResourceProvider { |
|
39 public static final String INDEX = "wrt_preview_main.html"; |
|
40 private static final Pattern HEAD_TAG_PATTERN = Pattern.compile("<head(\\s*\\w*=\"(^\")*\")*\\s*>", |
|
41 Pattern.CASE_INSENSITIVE); |
|
42 private static final String SCRIPT = "<script language=\"JavaScript\" type=\"text/javascript\" src=\"preview/script/lib/loader.js\"></script>"; |
|
43 |
|
44 public String[] getPaths() { |
|
45 return new String[] { INDEX }; |
|
46 } |
|
47 |
|
48 public InputStream getResourceStream(IProject project, IPath resource, Map<String, String[]> parameters, |
|
49 String sessionId) throws IOException, CoreException { |
|
50 return getProjectIndexPage(project.getName()); |
|
51 } |
|
52 |
|
53 private InputStream getProjectIndexPage(String projectName) throws IOException, CoreException { |
|
54 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); |
|
55 if (project.isAccessible()) { |
|
56 String indexFileName = CoreUtil.getIndexFile(project); |
|
57 if (indexFileName != null) { |
|
58 final IFile file = CoreUtil.getFile(project, indexFileName); |
|
59 if (!ProjectUtils.isExcluded(file)) { |
|
60 String string = CoreUtil.readFile(project, file); |
|
61 if (string != null) { |
|
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")); |
|
67 } |
|
68 } |
|
69 } |
|
70 PreviewerPlugin.print(String.format("Can not find main page (%s) in project %s.\n", indexFileName, |
|
71 project.getName())); |
|
72 } |
|
73 return null; |
|
74 } |
|
75 |
|
76 public void post(IProject project, IPath resource, Map<String, String[]> parameterMap, JSONObject object, |
|
77 String sessionId) throws IOException, CoreException { |
|
78 // Do nothing |
|
79 } |
|
80 |
|
81 } |
|