org.sweble.wikitext.lazy.utils.TextUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swc-parser-lazy Show documentation
Show all versions of swc-parser-lazy Show documentation
A parser for MediaWiki's Wikitext.
/**
* Copyright 2011 The Open Source Research Group,
* University of Erlangen-Nürnberg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sweble.wikitext.lazy.utils;
import java.util.ArrayList;
import java.util.Arrays;
import org.sweble.wikitext.lazy.AstNodeTypes;
import org.sweble.wikitext.lazy.parser.RtData;
import de.fau.cs.osr.ptk.common.ast.AstNode;
import de.fau.cs.osr.ptk.common.ast.NodeList;
import de.fau.cs.osr.ptk.common.ast.Text;
import de.fau.cs.osr.utils.StringUtils;
public final class TextUtils
{
public static NodeList trim(NodeList nodes)
{
ArrayList result = new ArrayList(nodes);
trimLeft(result);
trimRight(result);
return new NodeList(result);
}
public static NodeList trimLeft(NodeList nodes)
{
ArrayList result = new ArrayList(nodes);
trimLeft(result);
return new NodeList(result);
}
public static NodeList trimRight(NodeList nodes)
{
ArrayList result = new ArrayList(nodes);
trimRight(result);
return new NodeList(result);
}
public static NodeList trimAndPad(NodeList nodes, int spaces)
{
ArrayList result = new ArrayList(nodes);
trimLeft(result);
trimRight(result);
if (spaces <= 0)
return new NodeList(result);
return pad(result, spaces);
}
public static void trimLeft(ArrayList result)
{
int i = 0;
while (i < result.size())
{
switch (result.get(i).getNodeType())
{
case AstNode.NT_TEXT:
{
Text stringNode = (Text) result.get(i);
String trimmed = trimLeft(stringNode.getContent());
if (trimmed != stringNode.getContent())
{
if (trimmed.isEmpty())
{
result.remove(i);
continue;
}
else
{
result.set(i, new Text(trimmed));
break;
}
}
else
{
break;
}
}
case AstNodeTypes.NT_XML_COMMENT:
++i;
continue;
default:
break;
}
break;
}
}
public static void trimRight(ArrayList result)
{
int i = result.size() - 1;
while (i >= 0)
{
switch (result.get(i).getNodeType())
{
case AstNode.NT_TEXT:
{
Text stringNode = (Text) result.get(i);
String trimmed = trimRight(stringNode.getContent());
if (trimmed != stringNode.getContent())
{
if (trimmed.isEmpty())
{
result.remove(i--);
continue;
}
else
{
result.set(i, new Text(trimmed));
break;
}
}
else
{
break;
}
}
case AstNodeTypes.NT_XML_COMMENT:
--i;
continue;
default:
break;
}
break;
}
}
public static NodeList pad(ArrayList result, int spaces)
{
if (spaces <= 0)
return new NodeList(result);
if (result.isEmpty())
{
result.add(new Text(StringUtils.strrep(' ', spaces * 2)));
}
else
{
String spaced;
String spacesString = StringUtils.strrep(' ', spaces);
// -- before
Text before = null;
if (result.get(0).isNodeType(AstNode.NT_TEXT))
before = (Text) result.remove(0);
spaced = "";
if (before != null)
spaced = before.getContent();
spaced = spacesString + spaced;
result.add(0, new Text(spaced));
// -- after
Text after = null;
int i = result.size() - 1;
if (result.get(i).isNodeType(AstNode.NT_TEXT))
after = (Text) result.remove(i);
spaced = "";
if (after != null)
spaced = after.getContent();
spaced = spaced + spacesString;
result.add(new Text(spaced));
}
return new NodeList(result);
}
public static String trim(String text)
{
int from = 0;
int length = text.length();
while ((from < length) && (Character.isWhitespace(text.charAt(from))))
++from;
while ((from < length) && (Character.isWhitespace(text.charAt(length - 1))))
--length;
if (from > 0 || length < text.length())
{
return text.substring(from, length);
}
else
{
return text;
}
}
public static String trimLeft(String text)
{
int from = 0;
int length = text.length();
while ((from < length) && (Character.isWhitespace(text.charAt(from))))
++from;
if (from > 0)
{
return text.substring(from, length);
}
else
{
return text;
}
}
public static String trimRight(String text)
{
int length = text.length();
while ((0 < length) && (Character.isWhitespace(text.charAt(length - 1))))
--length;
if (length < text.length())
{
return text.substring(0, length);
}
else
{
return text;
}
}
public static RtData addRtData(AstNode yyValue, Object[]... rts)
{
if (rts.length != yyValue.size() + 1)
rts = Arrays.copyOf(rts, yyValue.size() + 1);
RtData data = new RtData(rts);
yyValue.setAttribute("RTD", data);
return data;
}
public static Object[] joinRt(Object... objects)
{
ArrayList