Skip to content

Commit 61c1e63

Browse files
Edit findPrologContext method.
1 parent b5f7503 commit 61c1e63

File tree

4 files changed

+107
-54
lines changed

4 files changed

+107
-54
lines changed

src/main/java/com/oxygenxml/prolog/updater/dita/editor/DitaTopicTextEditor.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import org.apache.log4j.Logger;
44

55
import com.oxygenxml.prolog.updater.prolog.content.PrologContentCreator;
6-
import com.oxygenxml.prolog.updater.utils.TextPageDocumentUtil;
7-
import com.oxygenxml.prolog.updater.utils.XMLFragmentUtils;
86
import com.oxygenxml.prolog.updater.utils.ElementXPathConstants;
97
import com.oxygenxml.prolog.updater.utils.ElementXPathUtils;
8+
import com.oxygenxml.prolog.updater.utils.TextPageDocumentUtil;
9+
import com.oxygenxml.prolog.updater.utils.XMLFragmentUtils;
1010

1111
import ro.sync.exml.editor.xmleditor.operations.context.RelativeInsertPosition;
12-
import ro.sync.exml.workspace.api.editor.page.text.xml.TextDocumentController;
1312
import ro.sync.exml.workspace.api.editor.page.text.xml.TextOperationException;
1413
import ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextEditorPage;
1514
import ro.sync.exml.workspace.api.editor.page.text.xml.WSXMLTextNodeRange;
@@ -32,10 +31,6 @@ public class DitaTopicTextEditor implements DitaEditor {
3231
* Contains all elements from prolog.
3332
*/
3433
private PrologContentCreator prologCreator;
35-
/**
36-
* Text document controller
37-
*/
38-
private TextDocumentController documentController;
3934

4035
/**
4136
* The page from Workspace text editor.
@@ -58,7 +53,6 @@ public class DitaTopicTextEditor implements DitaEditor {
5853
*/
5954
public DitaTopicTextEditor(WSXMLTextEditorPage wsEditorPage, PrologContentCreator prologCreator) {
6055
this.wsTextEditorPage = wsEditorPage;
61-
this.documentController = wsTextEditorPage.getDocumentController();
6256

6357
try {
6458
WSXMLTextNodeRange[] mapRoot = wsTextEditorPage.findElementsByXPath(ElementXPathConstants.ROOT_MAP_XPATH);

src/main/java/com/oxygenxml/prolog/updater/utils/AuthorPageDocumentUtil.java

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,35 +288,61 @@ private static WhatElementsCanGoHereContext findPrologContext(AuthorDocumentCont
288288
List<AuthorNode> childNodes = rootElement.getContentNodes();
289289
int nodesSize = childNodes.size();
290290

291-
loop: for (int i = 0; i < nodesSize; i++) {
291+
for (int i = 0; i < nodesSize; i++) {
292292
int offset = childNodes.get(i).getEndOffset();
293293
WhatElementsCanGoHereContext currentContext = null;
294294
try {
295295
currentContext = schemaManager.createWhatElementsCanGoHereContext(offset);
296296
} catch (BadLocationException e) {
297297
logger.warn(e, e.getCause());
298298
}
299-
if (currentContext != null) {
300-
// Analyze if current context can contain the prolog element.
301-
List<CIElement> possible = schemaManager.whatElementsCanGoHere(currentContext);
302-
if (possible != null) {
303-
// Iterate over possible elements.
304-
int size = possible.size();
305-
for (int j = 0; j < size; j++) {
306-
CIElement ciElement = possible.get(j);
307-
if (ciElement.getName().equals(XmlElementsUtils.getPrologName(documentType))) {
308-
toReturn = currentContext;
309-
if (j == 0) {
310-
// if prolog element is first in possible elements list. STOP.
311-
break loop;
312-
}
313-
}
314-
}
299+
300+
Boolean contextForProlog = analyzeContextForElement(
301+
XmlElementsUtils.getPrologName(documentType),
302+
currentContext,
303+
schemaManager);
304+
if (contextForProlog != null) {
305+
toReturn = currentContext;
306+
if (contextForProlog) {
307+
break;
315308
}
316309
}
317310
}
318311
}
319312
return toReturn;
320313
}
321314

315+
/**
316+
* Analyze if the given context can contains the given element.
317+
* @param element The name of the element to search.
318+
* @param context Context to analyze
319+
* @param schemaManager The schema manager from author mode.
320+
* @return <code>null</code> If the context can't contain the given element,
321+
* <code>false</code> if the context can contain the given element, but it's not the best position.
322+
* <code>true</code> if the context can contains the given element and this is the best position.
323+
*/
324+
private static Boolean analyzeContextForElement(String element, WhatElementsCanGoHereContext context, AuthorSchemaManager schemaManager) {
325+
Boolean toReturn = null;
326+
if (context != null) {
327+
// Analyze if current context can contain the prolog element.
328+
List<CIElement> possible = schemaManager.whatElementsCanGoHere(context);
329+
if (possible != null) {
330+
// Iterate over possible elements.
331+
int size = possible.size();
332+
for (int j = 0; j < size; j++) {
333+
CIElement ciElement = possible.get(j);
334+
if (ciElement.getName().equals(element)) {
335+
// the context can contain the given element.
336+
toReturn = false;
337+
if (j == 0) {
338+
// if prolog element is first in possible elements list(This is the best position).
339+
toReturn = true;
340+
}
341+
}
342+
}
343+
}
344+
345+
}
346+
return toReturn;
347+
}
322348
}

src/main/java/com/oxygenxml/prolog/updater/utils/ElementXPathConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,10 @@ public class ElementXPathConstants {
9696
*/
9797
public static final String ROOT_MAP_CHILD = ROOT_MAP_XPATH + "/*";
9898

99+
/**
100+
* Private constructor.
101+
*/
102+
private ElementXPathConstants() {
103+
throw new IllegalStateException("Utility class");
104+
}
99105
}

src/main/java/com/oxygenxml/prolog/updater/utils/TextPageDocumentUtil.java

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -190,42 +190,69 @@ private static WhatElementsCanGoHereContext findPrologContext(WSXMLTextEditorPag
190190

191191
// Get the XmlSchemaManager.
192192
WSTextXMLSchemaManager schemaManager = page.getXMLSchemaManager();
193+
if (schemaManager != null) {
194+
195+
// Get all child of root topic.
196+
WSXMLTextNodeRange[] topicChild = page.findElementsByXPath(ElementXPathUtils.getRootChildXpath(documentType));
197+
int childNo = topicChild.length;
198+
// Iterate over topic child
199+
for (int j = 0; j < childNo; j++) {
200+
WSXMLTextNodeRange currentNode = topicChild[j];
201+
// Get the offset of next line.
202+
WhatElementsCanGoHereContext currentContext = null;
203+
try {
204+
int offset = page.getOffsetOfLineEnd(currentNode.getEndLine());
205+
currentContext = schemaManager.createWhatElementsCanGoHereContext(offset);
206+
} catch (BadLocationException e) {
207+
logger.debug(e.getMessage(), e);
208+
}
193209

194-
// Get all child of root topic.
195-
WSXMLTextNodeRange[] topicChild = page.findElementsByXPath(ElementXPathUtils.getRootChildXpath(documentType));
196-
197-
int childNo = topicChild.length;
198-
// Iterate over topic child
199-
loop: for (int j = 0; j < childNo; j++) {
200-
WSXMLTextNodeRange currentNode = topicChild[j];
201-
// Get the offset of next line.
202-
WhatElementsCanGoHereContext currentContext = null;
203-
try {
204-
int offset = page.getOffsetOfLineEnd(currentNode.getEndLine());
205-
currentContext = schemaManager.createWhatElementsCanGoHereContext(offset);
206-
} catch (BadLocationException e) {
207-
logger.debug(e.getMessage(), e);
210+
Boolean contextForProlog = analyzeContextForElement(
211+
XmlElementsUtils.getPrologName(documentType),
212+
currentContext,
213+
schemaManager);
214+
if (contextForProlog != null) {
215+
toReturn = currentContext;
216+
if (contextForProlog) {
217+
break;
218+
}
219+
}
208220
}
209-
if (currentContext != null) {
210-
// Analyze if current context can contain the prolog element.
211-
List<CIElement> possible = schemaManager.whatElementsCanGoHere(currentContext);
212-
if (possible != null) {
213-
// Iterate over possible elements.
214-
int size = possible.size();
215-
for (int i = 0; i < size; i++) {
216-
CIElement ciElement = possible.get(i);
217-
if (ciElement.getName().equals(XmlElementsUtils.getPrologName(documentType))) {
218-
toReturn = currentContext;
219-
if (i == 0) {
220-
// if prolog element is first in possible elements list. STOP.
221-
break loop;
222-
}
221+
}
222+
return toReturn;
223+
}
224+
225+
/**
226+
* Analyze if the given context can contains the given element.
227+
* @param element The name of the element to search.
228+
* @param context Context to analyze
229+
* @param schemaManager The schema manager from author mode.
230+
* @return <code>null</code> If the context can't contain the given element,
231+
* <code>false</code> if the context can contain the given element, but it's not the best position.
232+
* <code>true</code> if the context can contains the given element and this is the best position.
233+
*/
234+
private static Boolean analyzeContextForElement(String element, WhatElementsCanGoHereContext context, WSTextXMLSchemaManager schemaManager) {
235+
Boolean toReturn = null;
236+
if (context != null) {
237+
// Analyze if current context can contain the prolog element.
238+
List<CIElement> possible = schemaManager.whatElementsCanGoHere(context);
239+
if (possible != null) {
240+
// Iterate over possible elements.
241+
int size = possible.size();
242+
for (int j = 0; j < size; j++) {
243+
CIElement ciElement = possible.get(j);
244+
if (ciElement.getName().equals(element)) {
245+
// the context can contain the given element.
246+
toReturn = false;
247+
if (j == 0) {
248+
// if prolog element is first in possible elements list(This is the best position).
249+
toReturn = true;
223250
}
224251
}
225252
}
226253
}
254+
227255
}
228256
return toReturn;
229257
}
230-
231258
}

0 commit comments

Comments
 (0)