Skip to content

Commit

Permalink
Streamline IDocumentTextNode creation
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Jul 6, 2024
1 parent aea0f9c commit da1fec0
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public SimpleCSDocumentFactory(SimpleCSModel model) {
@Override
public IDocumentTextNode createDocumentTextNode(String content,
IDocumentElementNode parent) {
IDocumentTextNode textNode = new SimpleCSDocumentTextNode();
textNode.setEnclosingElement(parent);
IDocumentTextNode textNode = new SimpleCSDocumentTextNode(parent);
parent.addTextNode(textNode);
textNode.setText(content);
return textNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashSet;

import org.eclipse.pde.internal.core.text.DocumentTextNode;
import org.eclipse.pde.internal.core.text.IDocumentElementNode;
import org.eclipse.pde.internal.core.util.PDETextHelper;

public class SimpleCSDocumentTextNode extends DocumentTextNode {
Expand All @@ -30,15 +31,14 @@ public class SimpleCSDocumentTextNode extends DocumentTextNode {
TAG_EXCEPTIONS.add("br/"); //$NON-NLS-1$
}

public SimpleCSDocumentTextNode() {
super();
public SimpleCSDocumentTextNode(IDocumentElementNode enclosingElement) {
super(enclosingElement);
}

@Override
public String write() {
String content = getText().trim();
return PDETextHelper.translateWriteText(content, TAG_EXCEPTIONS,
SUBSTITUTE_CHARS);
return PDETextHelper.translateWriteText(content, TAG_EXCEPTIONS, SUBSTITUTE_CHARS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void writeDelimeter(PrintWriter writer) {
}

@Override
protected IDocumentTextNode createDocumentTextNode() {
return new SimpleCSDocumentTextNode();
protected IDocumentTextNode createDocumentTextNode(IDocumentElementNode enclosingElement) {
return new SimpleCSDocumentTextNode(enclosingElement);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.pde.internal.ua.core.ctxhelp.text;

import org.eclipse.pde.internal.core.text.IDocumentElementNode;
import org.eclipse.pde.internal.core.text.IDocumentTextNode;

/**
Expand All @@ -35,8 +36,8 @@ public CtxHelpDescription(CtxHelpModel model) {
}

@Override
protected IDocumentTextNode createDocumentTextNode() {
return new CtxHelpDescriptionTextNode();
protected IDocumentTextNode createDocumentTextNode(IDocumentElementNode enclosingElement) {
return new CtxHelpDescriptionTextNode(enclosingElement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashSet;

import org.eclipse.pde.internal.core.text.DocumentTextNode;
import org.eclipse.pde.internal.core.text.IDocumentElementNode;
import org.eclipse.pde.internal.core.util.PDETextHelper;

public class CtxHelpDescriptionTextNode extends DocumentTextNode {
Expand All @@ -30,15 +31,14 @@ public class CtxHelpDescriptionTextNode extends DocumentTextNode {
TAG_EXCEPTIONS.add("/b"); //$NON-NLS-1$
}

public CtxHelpDescriptionTextNode() {
super();
public CtxHelpDescriptionTextNode(IDocumentElementNode enclosingElement) {
super(enclosingElement);
}

@Override
public String write() {
String content = getText().trim();
return PDETextHelper.translateWriteText(content, TAG_EXCEPTIONS,
SUBSTITUTE_CHARS);
return PDETextHelper.translateWriteText(content, TAG_EXCEPTIONS, SUBSTITUTE_CHARS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
*******************************************************************************/
package org.eclipse.pde.internal.core.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -42,10 +40,7 @@ public static String truncateAndTrailOffText(String text, int limit) {
}

public static boolean isDefined(String text) {
if ((text == null) || (text.isEmpty())) {
return false;
}
return true;
return text != null && !text.isEmpty();
}

public static boolean isDefinedAfterTrim(String text) {
Expand Down Expand Up @@ -101,11 +96,12 @@ public static String translateReadText(String text) {
return result;
}

public static String translateWriteText(String text, HashMap<Character, String> substituteChars) {
public static String translateWriteText(String text, Map<Character, String> substituteChars) {
return translateWriteText(text, null, substituteChars);
}

public static String translateWriteText(String text, HashSet<String> tagExceptions, HashMap<Character, String> substituteChars) {
public static String translateWriteText(String text, Set<String> tagExceptions,
Map<Character, String> substituteChars) {
// Ensure not null
if (text == null) {
return ""; //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,7 @@ public boolean setXMLContent(String text) {
IDocumentTextNode node = getTextNode();
if (node == null) {
// Text does not exist, create it
node = createDocumentTextNode();
node.setEnclosingElement(this);
node = createDocumentTextNode(this);
addTextNode(node);
}
// Update text on node
Expand Down Expand Up @@ -732,8 +731,8 @@ protected IDocumentAttributeNode createDocumentAttributeNode() {
return new DocumentAttributeNode();
}

protected IDocumentTextNode createDocumentTextNode() {
return new DocumentTextNode();
protected IDocumentTextNode createDocumentTextNode(IDocumentElementNode enclosingElement) {
return new DocumentTextNode(enclosingElement);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public IDocumentAttributeNode createAttribute(String name, String value, IDocume

@Override
public IDocumentTextNode createDocumentTextNode(String content, IDocumentElementNode parent) {
IDocumentTextNode textNode = new DocumentTextNode();
textNode.setEnclosingElement(parent);
IDocumentTextNode textNode = new DocumentTextNode(parent);
parent.addTextNode(textNode);
textNode.setText(content);
return textNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,32 @@
*******************************************************************************/
package org.eclipse.pde.internal.core.text;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.pde.internal.core.util.PDETextHelper;

public class DocumentTextNode extends DocumentXMLNode implements IDocumentTextNode {

private static final long serialVersionUID = 1L;

protected static final HashMap<Character, String> SUBSTITUTE_CHARS = new HashMap<>(5);

static {
SUBSTITUTE_CHARS.put(Character.valueOf('&'), "&amp;"); //$NON-NLS-1$
SUBSTITUTE_CHARS.put(Character.valueOf('<'), "&lt;"); //$NON-NLS-1$
SUBSTITUTE_CHARS.put(Character.valueOf('>'), "&gt;"); //$NON-NLS-1$
SUBSTITUTE_CHARS.put(Character.valueOf('\''), "&apos;"); //$NON-NLS-1$
SUBSTITUTE_CHARS.put(Character.valueOf('\"'), "&quot;"); //$NON-NLS-1$
}
protected static final Map<Character, String> SUBSTITUTE_CHARS = Map.of( //
Character.valueOf('&'), "&amp;", //$NON-NLS-1$
Character.valueOf('<'), "&lt;", //$NON-NLS-1$
Character.valueOf('>'), "&gt;", //$NON-NLS-1$
Character.valueOf('\''), "&apos;", //$NON-NLS-1$
Character.valueOf('\"'), "&quot;" //$NON-NLS-1$
);

private transient int fOffset;
private transient int fLength;
private transient IDocumentElementNode fEnclosingElement;

private String fText;

public DocumentTextNode() {
public DocumentTextNode(IDocumentElementNode enclosingElement) {
fOffset = -1;
fLength = 0;
fEnclosingElement = null;
}

@Override
public void setEnclosingElement(IDocumentElementNode node) {
fEnclosingElement = node;
fEnclosingElement = enclosingElement;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

public interface IDocumentTextNode extends IDocumentRange, Serializable, IDocumentXMLNode {

public static final String F_PROPERTY_CHANGE_TYPE_PCDATA = "type_pcdata"; //$NON-NLS-1$

// Used by text edit operations
void setEnclosingElement(IDocumentElementNode node);
String F_PROPERTY_CHANGE_TYPE_PCDATA = "type_pcdata"; //$NON-NLS-1$

IDocumentElementNode getEnclosingElement();

Expand All @@ -33,9 +30,9 @@ public interface IDocumentTextNode extends IDocumentRange, Serializable, IDocume
void setLength(int length);

// Not used by text edit operations
public void reconnect(IDocumentElementNode parent);
void reconnect(IDocumentElementNode parent);

// Not used by text edit operations
public String write();
String write();

}
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ public IPluginExtensionPoint createExtensionPoint() {

@Override
public IDocumentTextNode createDocumentTextNode(String content, IDocumentElementNode parent) {
DocumentTextNode textNode = new DocumentTextNode();
textNode.setEnclosingElement(parent);
DocumentTextNode textNode = new DocumentTextNode(parent);
parent.addTextNode(textNode);
textNode.setText(content.trim());
return textNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public void setText(String text) throws CoreException {
IDocumentTextNode node = getTextNode();
String oldText = node == null ? null : node.getText();
if (node == null) {
node = new DocumentTextNode();
node.setEnclosingElement(this);
node = new DocumentTextNode(this);
addTextNode(node);
}
node.setText(text.trim());
Expand Down

0 comments on commit da1fec0

Please sign in to comment.