summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openslx/util')
-rw-r--r--src/main/java/org/openslx/util/LevenshteinDistance.java85
-rw-r--r--src/main/java/org/openslx/util/ThriftUtil.java4
-rw-r--r--src/main/java/org/openslx/util/XmlHelper.java48
3 files changed, 134 insertions, 3 deletions
diff --git a/src/main/java/org/openslx/util/LevenshteinDistance.java b/src/main/java/org/openslx/util/LevenshteinDistance.java
new file mode 100644
index 0000000..0f33167
--- /dev/null
+++ b/src/main/java/org/openslx/util/LevenshteinDistance.java
@@ -0,0 +1,85 @@
+package org.openslx.util;
+
+public final class LevenshteinDistance
+{
+ private final int insertionCost;
+ private final int deletionCost;
+ private final int substitutionCost;
+
+ public LevenshteinDistance()
+ {
+ this( 1, 1, 1 );
+ }
+
+ public LevenshteinDistance( int insertionCost, int deletionCost, int substitutionCost )
+ {
+ this.validateCostArgument( insertionCost >= 0, "Insertion cost must be greater than or equal to 0" );
+ this.validateCostArgument( deletionCost >= 0, "Deletion cost must be greater than or equal to 0" );
+ this.validateCostArgument( substitutionCost >= 0, "Substitution cost must be greater than or equal to 0" );
+
+ this.insertionCost = insertionCost;
+ this.deletionCost = deletionCost;
+ this.substitutionCost = substitutionCost;
+ }
+
+ private void validateCostArgument( boolean condition, String errorMsg )
+ {
+ if ( !condition ) {
+ throw new IllegalArgumentException( errorMsg );
+ }
+ }
+
+ public int calculateDistance( CharSequence source, CharSequence target )
+ {
+ if ( source == null || target == null ) {
+ throw new IllegalArgumentException( "Source or target cannot be null" );
+ }
+
+ int sourceLength = source.length();
+ int targetLength = target.length();
+
+ int[][] matrix = new int[ sourceLength + 1 ][ targetLength + 1 ];
+ matrix[0][0] = 0;
+
+ for ( int row = 1; row <= sourceLength; ++row ) {
+ matrix[row][0] = row;
+ }
+
+ for ( int col = 1; col <= targetLength; ++col ) {
+ matrix[0][col] = col;
+ }
+
+ for ( int row = 1; row <= sourceLength; ++row ) {
+ for ( int col = 1; col <= targetLength; ++col ) {
+ matrix[row][col] = calcMinCost( source, target, matrix, row, col );
+ }
+ }
+
+ return matrix[sourceLength][targetLength];
+ }
+
+ private int calcMinCost( CharSequence source, CharSequence target, int[][] matrix, int row, int col )
+ {
+ return Math.min( calcSubstitutionCost( source, target, matrix, row, col ),
+ Math.min( calcDeletionCost( matrix, row, col ), calcInsertionCost( matrix, row, col ) ) );
+ }
+
+ private int calcInsertionCost( int[][] matrix, int row, int col )
+ {
+ return matrix[row][col - 1] + insertionCost;
+ }
+
+ private int calcDeletionCost( int[][] matrix, int row, int col )
+ {
+ return matrix[row - 1][col] + deletionCost;
+ }
+
+ private int calcSubstitutionCost( CharSequence source, CharSequence target, int[][] matrix, int row, int col )
+ {
+ int cost = 0;
+ if ( source.charAt( row - 1 ) != target.charAt( col - 1 ) ) {
+ cost = substitutionCost;
+ }
+ return matrix[row - 1][col - 1] + cost;
+ }
+}
diff --git a/src/main/java/org/openslx/util/ThriftUtil.java b/src/main/java/org/openslx/util/ThriftUtil.java
index 3c2c9ea..a9035c1 100644
--- a/src/main/java/org/openslx/util/ThriftUtil.java
+++ b/src/main/java/org/openslx/util/ThriftUtil.java
@@ -7,7 +7,7 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
-import org.openslx.vm.VmwareConfig;
+import org.openslx.virtualization.configuration.VirtualizationConfigurationVmwareFileFormat;
public class ThriftUtil {
@@ -41,7 +41,7 @@ public class ThriftUtil {
BufferedReader reader;
StringBuffer content = new StringBuffer("");
try {
- reader = VmwareConfig.getVmxReader(bytes, bytes.length);
+ reader = VirtualizationConfigurationVmwareFileFormat.getVmxReader(bytes, bytes.length);
String line="";
while ((line=reader.readLine()) != null) {
content.append(line + "\n");
diff --git a/src/main/java/org/openslx/util/XmlHelper.java b/src/main/java/org/openslx/util/XmlHelper.java
index 70c5be8..4e814a0 100644
--- a/src/main/java/org/openslx/util/XmlHelper.java
+++ b/src/main/java/org/openslx/util/XmlHelper.java
@@ -3,6 +3,8 @@ package org.openslx.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -14,6 +16,7 @@ import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
@@ -28,10 +31,11 @@ public class XmlHelper
private final static Logger LOGGER = Logger.getLogger( XmlHelper.class );
// TODO check thread-safety
- public static final XPath XPath = XPathFactory.newInstance().newXPath();
+ private static final XPath XPath = XPathFactory.newInstance().newXPath();
private static DocumentBuilder dBuilder;
static {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
+ dbFactory.setNamespaceAware( true );
dbFactory.setIgnoringComments( true );
try {
dBuilder = dbFactory.newDocumentBuilder();
@@ -40,6 +44,48 @@ public class XmlHelper
}
}
+ public static String globalXPathToLocalXPath( String xPath )
+ {
+ final StringBuilder exprBuilder = new StringBuilder();
+ final String[] elements = xPath.split( "/" );
+
+ for ( final String element : elements ) {
+ if ( !element.isEmpty() ) {
+ final Pattern arraySpecifierRegex = Pattern.compile( "^(.*)\\[(.*)\\]$" );
+ final Matcher arraySpecifierMatcher = arraySpecifierRegex.matcher( element );
+ final String elementName;
+ final String elementSpecifier;
+
+ if ( arraySpecifierMatcher.find() ) {
+ elementName = arraySpecifierMatcher.group( 1 );
+ elementSpecifier = arraySpecifierMatcher.group( 2 );
+ } else {
+ elementName = element;
+ elementSpecifier = null;
+ }
+
+ if ( !elementName.startsWith( "@" ) && !elementName.equals( "*" ) ) {
+ exprBuilder.append( "/*[local-name()='" + elementName + "']" );
+
+ } else {
+ exprBuilder.append( "/" + elementName );
+ }
+
+ if ( elementSpecifier != null && !elementSpecifier.isEmpty() ) {
+ exprBuilder.append( "[" + elementSpecifier + "]" );
+ }
+ }
+ }
+
+ return exprBuilder.toString();
+ }
+
+ public static XPathExpression compileXPath( String xPath ) throws XPathExpressionException
+ {
+ final String localXPath = XmlHelper.globalXPathToLocalXPath( xPath );
+ return XPath.compile( localXPath );
+ }
+
public static Document parseDocumentFromStream( InputStream is )
{
Document doc = null;