author | Eugene Ostroukhov <eugeneo@symbian.org> |
Tue, 31 Aug 2010 11:58:53 -0700 | |
changeset 479 | 518afa7c6d2f |
parent 470 | d4809db37847 |
permissions | -rw-r--r-- |
310
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
1 |
/** |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
2 |
* Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies). |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
3 |
* All rights reserved. |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
4 |
* This component and the accompanying materials are made available |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
6 |
* which accompanies this distribution, and is available |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
8 |
* |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
9 |
* Initial Contributors: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
10 |
* Symbian Foundation - initial contribution. |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
11 |
* Contributors: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
12 |
* Description: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
13 |
* Overview: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
14 |
* Details: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
15 |
* Platforms/Drives/Compatibility: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
16 |
* Assumptions/Requirement/Pre-requisites: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
17 |
* Failures and causes: |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
18 |
*/ |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
19 |
package org.symbian.tools.wrttools.core.libraries; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
20 |
|
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
21 |
import java.io.IOException; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
22 |
|
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
23 |
import org.eclipse.core.resources.IFile; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
24 |
import org.eclipse.core.resources.IProject; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
25 |
import org.eclipse.core.runtime.CoreException; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
26 |
import org.eclipse.wst.sse.core.StructuredModelManager; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
27 |
import org.eclipse.wst.sse.core.internal.provisional.IModelManager; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
28 |
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
29 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
30 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
31 |
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
32 |
import org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
33 |
import org.symbian.tools.wrttools.util.CoreUtil; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
34 |
import org.w3c.dom.Element; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
35 |
import org.w3c.dom.NodeList; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
36 |
|
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
37 |
@SuppressWarnings("restriction") |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
38 |
public class LibrariesUtils { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
39 |
public static void addJSToHtml(IProject project, String label, String[] js, String[] css) throws CoreException, |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
40 |
IOException { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
41 |
if (js == null) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
42 |
js = new String[0]; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
43 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
44 |
if (css == null) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
45 |
css = new String[0]; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
46 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
47 |
if (js.length == 0 && css.length == 0) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
48 |
return; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
49 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
50 |
IModelManager modelManager = StructuredModelManager.getModelManager(); |
479
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
51 |
IFile file = CoreUtil.getIndexFile(project); |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
52 |
if (file != null & file.exists()) { |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
53 |
IStructuredModel model = modelManager.getModelForEdit(file); |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
54 |
try { |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
55 |
if (model instanceof IDOMModel) { |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
56 |
((IDOMModel) model).beginRecording(js, label); |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
57 |
boolean needsSave = false; |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
58 |
for (String jsLib : js) { |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
59 |
needsSave |= change(((IDOMModel) model).getDocument(), jsLib); |
310
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
60 |
} |
479
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
61 |
model.endRecording(js); |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
62 |
if (needsSave) { |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
63 |
model.save(); |
310
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
64 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
65 |
} |
479
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
66 |
} finally { |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
67 |
if (model != null) { |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
68 |
model.releaseFromEdit(); |
518afa7c6d2f
Minor refactoring to make API more clear
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
470
diff
changeset
|
69 |
} |
310
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
70 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
71 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
72 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
73 |
|
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
74 |
private static boolean change(IDOMDocument document, String jsPath) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
75 |
NodeList head = document.getElementsByTagName("head"); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
76 |
if (head.getLength() == 1) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
77 |
IDOMElement headNode = ((IDOMElement) head.item(0)); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
78 |
NodeList elements = headNode.getElementsByTagName("script"); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
79 |
boolean needToAdd = true; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
80 |
IDOMElement last = null; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
81 |
for (int i = 0; i < elements.getLength(); i++) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
82 |
last = (IDOMElement) elements.item(i); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
83 |
String attribute = last.getAttribute("src"); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
84 |
if (jsPath.equalsIgnoreCase(attribute)) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
85 |
needToAdd = false; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
86 |
break; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
87 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
88 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
89 |
if (needToAdd) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
90 |
Element element = document.createElement("script"); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
91 |
element.setAttribute("language", "javascript"); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
92 |
element.setAttribute("type", "text/javascript"); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
93 |
element.setAttribute("src", jsPath); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
94 |
if (last != null && last.getNextSibling() != null) { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
95 |
headNode.insertBefore(element, last.getNextSibling()); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
96 |
} else { |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
97 |
headNode.appendChild(element); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
98 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
99 |
FormatProcessorXML formatter = new FormatProcessorXML(); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
100 |
formatter.formatNode(headNode); |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
101 |
return true; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
102 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
103 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
104 |
return false; |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
105 |
} |
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
106 |
|
e9484be98cfe
UI to add libraries was introduced
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
diff
changeset
|
107 |
} |