Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove guava in wikitext #562

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,49 @@

package org.eclipse.mylyn.wikitext.markdown.internal;

import java.util.Arrays;
import java.util.Locale;

import org.eclipse.mylyn.wikitext.parser.markup.IdGenerationStrategy;

import com.google.common.base.CharMatcher;

public class GfmIdGenerationStrategy extends IdGenerationStrategy {

enum ScanState {
HEAD, WORD, HYPHEN
}
@Override
public String generateId(String headingText) {
String id = headingText.toLowerCase(Locale.getDefault());
id = id.replaceAll("[^a-z0-9_-]", "-"); //$NON-NLS-1$//$NON-NLS-2$
CharMatcher hyphenMatcher = CharMatcher.is('-');
id = hyphenMatcher.trimFrom(hyphenMatcher.collapseFrom(id, '-'));
ScanState state = ScanState.HEAD;
char[] collected = new char[id.length()];
int j = 0;
for (int i = 0; i < id.length(); i++) {
char c = id.charAt(i);
switch (state) {
case HEAD: // skip as many hyphens as we can find
if (c == '-') {
continue;
}
state = ScanState.WORD;
break;
case WORD: // commit chars until the next hyphen
if (c == '-') {
state = ScanState.HYPHEN;
continue; // don't yet commit the hyphen in case it is trailing
}
break;
case HYPHEN:
if (c != '-') {
collected[j++] = '-'; // deferred commit of the hyphen
state = ScanState.WORD;
} else {
continue; // skip additional hyphen
}
}
collected[j++] = c;
}
id = String.valueOf(Arrays.copyOfRange(collected, 0, j));
return id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void getMarkupLanguagesDuplicatedNames() {

@Override
protected Set<MarkupLanguage> loadMarkupLanguages() {
return Set.of(new MockMarkupLanguage("Test"), new MockMarkupLanguage("Test"));
return Set.of(new MockMarkupLanguage("Test"), new MockMarkupLanguage.MockMarkupLanguage2("Test"));
}
};
IllegalStateException ise = assertThrows(IllegalStateException.class, () -> provider.getMarkupLanguages());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public void processContent(MarkupParser parser, String markupContent, boolean as
throw new UnsupportedOperationException();
}

public static class MockMarkupLanguage2 extends MockMarkupLanguage {
public MockMarkupLanguage2(String name) {
super(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ Bundle-Name: Mylyn WikiText
Bundle-Vendor: Eclipse Mylyn
Bundle-RequiredExecutionEnvironment: JavaSE-17
Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=17))"
Import-Package: com.google.common.base;version="[33.0,34.0)",
com.google.common.collect;version="[33.0,34.0)",
com.google.common.escape;version="[33.0,34.0)",
com.google.common.xml;version="[33.0,34.0)",
javax.lang.model,
Import-Package: javax.lang.model,
javax.xml.namespace,
javax.xml.parsers,
javax.xml.stream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@

import java.util.List;

import org.eclipse.mylyn.wikitext.internal.util.Strings;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;

import com.google.common.base.Strings;

/**
* @author David Green
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;

import com.google.common.base.CharMatcher;

/**
* @author David Green
*/
Expand Down Expand Up @@ -119,9 +117,7 @@ private void normalizeEmptySpaceBetweenNodes(Element parent) {
for (Node child : children) {
Node previousSibling = child.previousSibling();
Node nextSibling = child.nextSibling();
if (child instanceof TextNode textNode && previousSibling instanceof Element && nextSibling instanceof Element) {
Element prevElement = (Element) previousSibling;
Element nextElement = (Element) nextSibling;
if (child instanceof TextNode textNode && previousSibling instanceof Element prevElement && nextSibling instanceof Element nextElement) {
normalizeTextBetweenNodes(textNode, prevElement, nextElement);
}
}
Expand Down Expand Up @@ -183,7 +179,7 @@ private Element computeBeforeTarget(Element element) {
private static int lastIndexOfNonWhitespace(String text) {
int i = text.length() - 1;
while (i > -1) {
if (!CharMatcher.whitespace().matches(text.charAt(i))) {
if (!Character.isWhitespace(text.charAt(i))) {
return i;
}
--i;
Expand All @@ -194,7 +190,7 @@ private static int lastIndexOfNonWhitespace(String text) {
private static int firstIndexOfNonWhitespace(String text) {
int i = 0;
while (i < text.length()) {
if (!CharMatcher.whitespace().matches(text.charAt(i))) {
if (!Character.isWhitespace(text.charAt(i))) {
return i;
}
++i;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2024 GK Software SE, and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* See git history
*******************************************************************************/
package org.eclipse.mylyn.wikitext.internal.util;

public class Preconditions {

/**
* @param expression
* this should be {@code true} otherwise an {@link IllegalArgumentException} is thrown.
* @param errorMessage
* optional error message
* @throws IllegalArgumentException
*/
public static void checkArgument(boolean expression, String... errorMessage) {
if (!expression) {
if (errorMessage == null || errorMessage.length == 0) {
throw new IllegalArgumentException();
}
throw new IllegalArgumentException(errorMessage[0]);
}
}

/**
* @param expression
* this should be {@code true} otherwise an {@link IllegalStateException} is thrown.
* @throws IllegalStateException
*/
public static void checkState(boolean expression) {
if (!expression) {
throw new IllegalStateException();
}
}

/**
* @param expression
* this should be {@code true} otherwise an {@link IllegalStateException} is thrown.
* @param errorMessage
* error message which may contain placeholders
* @param messageArguments
* optional message arguments to be interpolated into the message
* @throws IllegalStateException
*/
public static void checkState(boolean expression, String errorMessage, Object... messageArguments) {
if (!expression) {
if (messageArguments == null || messageArguments.length == 0) {
throw new IllegalStateException(errorMessage);
}
throw new IllegalStateException(String.format(errorMessage, messageArguments));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* Copyright (c) 2024 GK Software SE, and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* See git history
*******************************************************************************/
package org.eclipse.mylyn.wikitext.internal.util;

public class Strings {

public static boolean isNullOrEmpty(String str) {
if (str == null) {
return true;
}
return str.isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*******************************************************************************
* Copyright (c) 2024 GK Software SE, and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* See git history
*******************************************************************************/
package org.eclipse.mylyn.wikitext.internal.util;

public class XmlUtil {

public static String getEscapedAttribute(String s) {
StringBuilder result = new StringBuilder(s.length() + 10);
for (int i = 0; i < s.length(); ++i) {
appendEscapedChar(result, s.charAt(i), true);
}
return result.toString();
}

public static String getEscapedContent(String s) {
StringBuilder result = new StringBuilder(s.length() + 10);
for (int i = 0; i < s.length(); ++i) {
appendEscapedChar(result, s.charAt(i), false);
}
return result.toString();
}

private static void appendEscapedChar(StringBuilder buffer, char c, boolean forAttribute) {
String replacement = getReplacement(c, forAttribute);
if (replacement != null) {
buffer.append('&');
buffer.append(replacement);
buffer.append(';');
} else if (c >= 0x00 && c <= 0x1F && c != '\t' && c != '\n' && c != '\r') {
buffer.append("\uFFFD"); //$NON-NLS-1$
} else {
buffer.append(c);
}
}

private static String getReplacement(char c, boolean forAttribute) {
// Encode special XML characters into the equivalent character references.
// These five are defined by default for all XML documents.
switch (c) {
case '<':
return "lt"; //$NON-NLS-1$
case '>':
return "gt"; //$NON-NLS-1$
case '&':
return "amp"; //$NON-NLS-1$
}
if (forAttribute) {
switch (c) {
case '"':
return "quot"; //$NON-NLS-1$
case '\'':
return "apos"; //$NON-NLS-1$
case '\t':
return "#x9"; //$NON-NLS-1$
case '\n':
return "#xA"; //$NON-NLS-1$
case '\r':
return "#xD"; //$NON-NLS-1$
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*******************************************************************************/
package org.eclipse.mylyn.wikitext.parser.builder;

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Map.entry;
import static org.eclipse.mylyn.wikitext.internal.util.Preconditions.checkArgument;

import java.io.BufferedReader;
import java.io.File;
Expand Down Expand Up @@ -48,10 +48,6 @@
import org.eclipse.mylyn.wikitext.util.FormattingXMLStreamWriter;
import org.eclipse.mylyn.wikitext.util.XmlStreamWriter;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableMap;

/**
* A builder that produces XHTML output. The nature of the output is affected by various settings on the builder.
*
Expand Down Expand Up @@ -224,7 +220,9 @@ public void copyConfiguration(HtmlDocumentBuilder other) {
*/
public void addLinkUriProcessor(UriProcessor processor) {
Objects.requireNonNull(processor, "Must provide processor"); //$NON-NLS-1$
linkUriProcessors = ImmutableList.<UriProcessor> builder().addAll(linkUriProcessors).add(processor).build();
List<UriProcessor> processors = new ArrayList<>(linkUriProcessors);
processors.add(processor);
linkUriProcessors = List.copyOf(processors);
}

protected static XmlStreamWriter createFormattingXmlStreamWriter(Writer out) {
Expand All @@ -249,15 +247,15 @@ public void setElementNameOfSpanType(SpanType spanType, String elementName) {
Objects.requireNonNull(spanType, "Must provide spanType"); //$NON-NLS-1$
Objects.requireNonNull(elementName, "Must provide elementName"); //$NON-NLS-1$

ImmutableMap.Builder<SpanType, String> builder = ImmutableMap.builder();
Map<SpanType, String> builder = new HashMap<>();
for (Entry<SpanType, String> entry : spanTypeToElementName.entrySet()) {
if (!entry.getKey().equals(spanType)) {
builder.put(entry);
builder.put(entry.getKey(), entry.getValue());
}
}
builder.put(spanType, elementName);

spanTypeToElementName = builder.build();
spanTypeToElementName = Map.copyOf(builder);
}

/**
Expand Down Expand Up @@ -1433,12 +1431,11 @@ public void setCopyrightNotice(String copyrightNotice) {
}

private void copyLinkProcessors(HtmlDocumentBuilder other) {
List<UriProcessor> defaultProcessors = other.defaultLinkUriProcessors();
Builder<UriProcessor> newProcessors = ImmutableList.<UriProcessor> builder().addAll(defaultProcessors);
List<UriProcessor> defaultProcessors = new ArrayList<>(other.defaultLinkUriProcessors());
if (defaultProcessors.size() < linkUriProcessors.size()) {
newProcessors.addAll(linkUriProcessors.subList(defaultProcessors.size(), linkUriProcessors.size()));
defaultProcessors.addAll(linkUriProcessors.subList(defaultProcessors.size(), linkUriProcessors.size()));
}
other.linkUriProcessors = newProcessors.build();
other.linkUriProcessors = List.copyOf(defaultProcessors);
}

private List<UriProcessor> defaultLinkUriProcessors() {
Expand Down
Loading