-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsAccess.js
243 lines (219 loc) · 7.97 KB
/
wsAccess.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**
* The name of menu item that contains the action.
*/
var menuItemName = "Expand all conrefs/keyrefs";
/**
* Application started.
*/
function applicationStarted(pluginWorkspaceAccess) {
loadDitaTextResolverMethods(pluginWorkspaceAccess);
menuContributor = {
customizeAuthorPopUpMenu: function (popUp, authorAccess) {
try {
/*Create absolute reference*/
mi = new Packages.javax.swing.JMenuItem(menuItemName);
popUp.add(mi);
actionPerfObj = {
actionPerformed: function (e) {
var documentController = authorAccess.getDocumentController();
documentController.beginCompoundEdit();
// Expand all conrefs/keyrefs from the document.
expandConrefs(authorAccess, new Packages.java.util.HashSet());
expandKrefs(authorAccess);
documentController.endCompoundEdit();
}
}
mi.addActionListener(new JavaAdapter(Packages.java.awt.event.ActionListener, actionPerfObj));
}
catch (e1) {
Packages.java.lang.System.err.println(e1);
}
}
}
pluginWorkspaceAccess.addMenusAndToolbarsContributorCustomizer(new Packages.ro.sync.exml.workspace.api.standalone.actions.MenusAndToolbarsContributorCustomizer(menuContributor));
}
/**
* Application closing.
*/
function applicationClosing(pluginWorkspaceAccess) {
}
/**
* Expand conrefs from the current document.
*
* @param authorAccess The access in author page.
* @param Number of invalid conrefs, that can't be expanded.
*/
function expandConrefs(authorAccess, invalidConrefNodes) {
var controller = authorAccess.getDocumentController();
// Find conrefs and conkeyrefs
var allNodes = null;
try {
allNodes = controller.findNodesByXPath("//*[@conref or @conkeyref]", true, true, true);
}
catch (e) {
Packages.java.lang.System.err.println(e);
}
var conrefNodes = excludeInvalidNodes(allNodes, invalidConrefNodes);
var root = controller.getAuthorDocumentNode().getRootElement();
if (conrefNodes != null && !conrefNodes.isEmpty()) {
var conrefNodesSize = conrefNodes.size();
for (var i = 0; i < conrefNodesSize; i++) {
var currentNode = conrefNodes.get(i);
var replaceRoot = (currentNode.getStartOffset() <= root.getStartOffset()) && (currentNode.getEndOffset() >= root.getEndOffset());
try {
var isError = false;
var contentNodes = currentNode.getContentNodes();
if (! contentNodes.isEmpty()) {
for (var j = 0; j < contentNodes.size(); j++) {
var currentContentNode = contentNodes. get (j);
if ("#error".equals(currentContentNode.getName())) {
isError = true;
}
}
// Modify only the valid conrefs.
if (! isError) {
if (replaceRoot) {
var startOffset = contentNodes.get(0).getStartOffset() + 1;
var endOffset = contentNodes.get(0).getEndOffset() - 1;
var fragment = controller.createDocumentFragment(startOffset, endOffset);
controller.replaceRoot(fragment);
} else {
authorAccess.getEditorAccess().setCaretPosition(currentNode.getStartOffset() + 1);
Packages.ro.sync.ecss.dita.DITAAccess.replaceConref(authorAccess);
}
} else {
invalidConrefNodes.add(currentNode);
}
} else {
invalidConrefNodes.add(currentNode);
}
} catch (ex) {
invalidConrefNodes.add(currentNode);
Packages.java.lang.System.err.println(ex);
}
}
expandConrefs(authorAccess, invalidConrefNodes);
}
}
/**
* Expand the keyfs from the current document. The keyrefs from xrefs are changed with hrefs.
*/
function expandKrefs(authorAccess){
var documentController = authorAccess.getDocumentController();
if (resolver != null && activatedMethod != null && resolveReferenceMethod != null && deactivedMethod != null) {
var keyrefNodes = null;
try {
keyrefNodes = documentController.findNodesByXPath("//*[@keyref]", true, true, true);
}
catch (e) {
Packages.java.lang.System.err.println(e);
}
if (keyrefNodes != null && keyrefNodes.length > 0) {
try {
activatedMethod.invoke(resolver, authorAccess);
for (i = 0; i < keyrefNodes.length; i++) {
var currentNode = new JavaAdapter(Packages.ro.sync.ecss.extensions.api.node.AuthorElement, keyrefNodes[i]);
var classStringVal = "";
var classVal = currentNode.getAttribute("class");
if(classVal != null) {
classStringVal = classVal.getValue();
}
if (classStringVal.contains(" topic/link ") || classStringVal.contains(" topic/xref ")) {
// Change 'keyrefs' with 'hrefs' on 'xref' and 'link' elements
var keyrefVal = currentNode.getAttribute("keyref");
var xmlBaseURL = currentNode.getXMLBaseURL();
var keys = Packages.ro.sync.ecss.dita.DITAAccess.getKeys(xmlBaseURL,
Packages.ro.sync.ecss.dita.ContextKeyManager.getDefault());
var value = keyrefVal.getValue();
var keyInfo = keys.get(value);
if(keyInfo != null && keyInfo.getHrefLocation() != null) {
var hrefLocation = keyInfo.getHrefLocation();
var relativeVal = Packages.ro.sync.util.URLUtil.makeRelative(xmlBaseURL, hrefLocation);
documentController.removeAttribute("keyref", currentNode);
documentController.setAttribute("href",
new Packages.ro.sync.ecss.extensions.api.node.AttrValue(relativeVal),
currentNode);
}
} else {
// Insert the text from the reference over the current node.
resolved = resolveReferenceMethod.invoke(resolver, currentNode);
offset = keyrefNodes[i].getStartOffset();
documentController.deleteNode(keyrefNodes[i]);
documentController.insertText(offset, resolved);
}
}
}
catch (e) {
Packages.java.lang.System.err.println(e);
}
finally {
try {
resolver.deactivated(authorAccess);
}
catch (e) {
Packages.java.lang.System.err.println(e);
}
}
}
}
}
/**
* Exclude invalid node from given list
* @param allNodes List with all nodes.
* @param invalidNodes Set with invalid nodes.
*
* @return A list with valid nodes.
*/
function excludeInvalidNodes(allNodes, invalidNodes) {
var toReturn = null;
if(allNodes != null) {
if(invalidNodes != null && !invalidNodes.isEmpty()) {
var lenght = allNodes.length;
toReturn = new Packages.java.util.ArrayList();
for (var i = 0; i < lenght; i++) {
var currentNode = allNodes[i];
if(!invalidNodes.contains(currentNode)) {
toReturn.add(currentNode);
}
}
} else {
toReturn = Packages.java.util.Arrays.asList(allNodes);
}
}
return toReturn;
}
/**
* Load the DitaLinkTextResolver class from the dita.jar
* and store a instance and some methods as global variables.
*/
function loadDitaTextResolverMethods(pluginWorkspaceAccess) {
var classLoader = null;
try {
// Create the dita jar path.
var frameworksPath = pluginWorkspaceAccess.getUtilAccess().expandEditorVariables("${frameworks}", null);
var ditaJarPath = Packages.ro.sync.util.URLUtil.makeAbsolute(frameworksPath, "dita/dita.jar");
var urls = Packages.java.lang.reflect.Array.newInstance(java.net.URL, 1);
urls[0] = new java.net.URL(ditaJarPath);
// Load classes from dita.jar
classLoader = new Packages.java.net.URLClassLoader(urls, pluginWorkspaceAccess.getClass().getClassLoader());
var ditaLinkTextResolverClass = classLoader.loadClass("ro.sync.ecss.extensions.dita.link.DitaLinkTextResolver");
// Create a instance of DitaLinkTextResolver and get some methods.
resolver = ditaLinkTextResolverClass.newInstance();
activatedMethod = ditaLinkTextResolverClass.getDeclaredMethod("activated", Packages.java.lang.Class.forName("ro.sync.ecss.extensions.api.AuthorAccess"));
deactivedMethod = ditaLinkTextResolverClass.getDeclaredMethod("deactivated", Packages.java.lang.Class.forName("ro.sync.ecss.extensions.api.AuthorAccess"));
resolveReferenceMethod = ditaLinkTextResolverClass.getDeclaredMethod("resolveReference", Packages.java.lang.Class.forName("ro.sync.ecss.extensions.api.node.AuthorNode"));
}
catch (e) {
Packages.java.lang.System.err.println(e);
}
finally {
if (classLoader != null) {
try {
classLoader.close();
}
catch (e) {
//Do nothing
}
}
}
}