[sikuli] 193/385: made the Jygments stuff Maven compatible and that it can be used inside Netbeans when running stuff (NB json has a problem with comments so files are renamed to jso)

Gilles Filippini pini at moszumanska.debian.org
Sun Jun 29 19:26:11 UTC 2014


This is an automated email from the git hooks/post-receive script.

pini pushed a commit to tag upstream/1.1.0_beta1
in repository sikuli.

commit aa67455f06bb7fa7ddccee3bece827f33d80cf4a
Author: Raimund Hocke <rmhdevelop at me.com>
Date:   Sat Feb 1 17:37:06 2014 +0100

    made the Jygments stuff Maven compatible and that it can be used inside Netbeans when running stuff (NB json has a problem with comments so files are renamed to  jso)
---
 .../main/java/org/sikuli/syntaxhighlight/Run.java  |  31 ++-
 .../main/java/org/sikuli/syntaxhighlight/Util.java | 267 +++++++++++----------
 .../org/sikuli/syntaxhighlight/grammar/Lexer.java  |  30 ++-
 .../org/sikuli/syntaxhighlight/style/Style.java    |  28 ++-
 .../main/resources/jygments-legal/copyright.txt    |   8 +
 .../src/main/resources/jygments-legal/license.txt  |  25 ++
 6 files changed, 237 insertions(+), 152 deletions(-)

diff --git a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Run.java b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Run.java
index df64d42..c3e137c 100644
--- a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Run.java
+++ b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Run.java
@@ -3,16 +3,33 @@ package org.sikuli.syntaxhighlight;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.Date;
 import org.sikuli.syntaxhighlight.format.Formatter;
 import org.sikuli.syntaxhighlight.grammar.Lexer;
+import org.sikuli.syntaxhighlight.grammar.Token;
 
 public class Run {
+  
+  private static void p(String text, Object... args) {
+    System.out.println(String.format(text, args));
+  } 
 
-	public static void main(String[] args) throws IOException, ResolutionException {
-		String file = System.getProperty("user.dir") + "/org/sikuli/syntaxhighlight/Run.java";
-		Lexer lexer = Lexer.getByName("java");
-		Formatter formatter = Formatter.getByName("html");
-		String code = Util.streamToString(new FileInputStream(file));
-		formatter.format(lexer.getTokens(code), new PrintWriter(System.out));
-	}
+  public static void main(String[] args) throws IOException, ResolutionException {
+    String file = System.getProperty("user.dir") + "/src/main/java/org/sikuli/syntaxhighlight/Util.java";
+    String aLexer = "java";
+    Lexer lexer = Lexer.getByName(aLexer);
+    if (lexer != null) {
+      Formatter formatter = Formatter.getByName("html");
+      String code = Util.streamToString(new FileInputStream(file));
+//      code = "      String code = Util.streamToString(new FileInputStream(file));";
+      long start = new Date().getTime();
+      Iterable<Token> tokens = lexer.getTokens(code);
+      long lexing = new Date().getTime() - start;
+      formatter.format(tokens, new PrintWriter("/Users/rhocke/Desktop/shtest.html"));
+      long formatting = new Date().getTime()- start - lexing;
+      p("%s: processed (%d, %d)", aLexer, lexing, formatting);
+    } else {
+      p("%s: no Lexer found", aLexer);
+    }
+  }
 }
diff --git a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Util.java b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Util.java
index c941bcf..8909cb7 100644
--- a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Util.java
+++ b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/Util.java
@@ -1,12 +1,10 @@
 /**
  * Copyright 2010-2013 Three Crickets LLC.
  * <p>
- * The contents of this file are subject to the terms of a BSD license. See
- * attached license.txt.
+ * The contents of this file are subject to the terms of a BSD license. See attached license.txt.
  * <p>
- * Alternatively, you can obtain a royalty free commercial license with less
- * limitations, transferable or non-transferable, directly from Three Crickets
- * at http://threecrickets.com/
+ * Alternatively, you can obtain a royalty free commercial license with less limitations,
+ * transferable or non-transferable, directly from Three Crickets at http://threecrickets.com/
  */
 package org.sikuli.syntaxhighlight;
 
@@ -19,135 +17,148 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-import org.sikuli.syntaxhighlight.grammar.Lexer;
 
 /**
  * @author Tal Liron
  */
 public class Util {
 
-	public static String literalRegEx(String expression) {
-		return "\\Q" + expression + "\\E";
-	}
-
-	public static String replace(String string, String occurence, String replacement) {
-		return string.replaceAll(literalRegEx(occurence), replacement);
-	}
-
-	public static String streamToString(InputStream stream) throws IOException {
-		StringBuilder builder = new StringBuilder();
-		String line;
-
-		try {
-			BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
-			while ((line = reader.readLine()) != null) {
-				builder.append(line).append("\n");
-			}
-		} finally {
-			stream.close();
-		}
-
-		return builder.toString();
-	}
-
-	public static String rejsonToJson(InputStream stream) throws IOException {
-		String rejson = streamToString(stream);
-		String json = rejsonToJson(rejson, true);
-		json = rejsonToJson(json, false);
-		return json;
-	}
-
-	public static String rejsonToJson(String rejson, boolean doubleQuote) {
-		Matcher matcher = doubleQuote ? DOUBLE_QUOTED_STRING.matcher(rejson) : SINGLE_QUOTED_STRING.matcher(rejson);
-		StringBuilder json = new StringBuilder();
-		int start = 0, end = 0, lastEnd = 0;
-		while (matcher.find()) {
-			lastEnd = end;
-			start = matcher.start();
-			end = matcher.end();
-			if ((start > 0) && (rejson.charAt(start - 1) == 'r')) {
-				// Convert Python-style r"" string to Java-compatible pattern
-				String string = rejson.substring(start + 1, end - 1);
-				json.append(rejson.substring(lastEnd, start - 1));
-				json.append('\"');
-				json.append(pythonRegExToJavaPattern(string, doubleQuote));
-				json.append('\"');
-			} /*
-			 * else if( !doubleQuote ) { // From single quote to double quote
-			 * String string = rejson.substring( start + 1, end - 1 );
-			 * json.append( rejson.substring( lastEnd, start - 1 ) );
-			 * json.append( '\"' ); json.append( string.replaceAll( "\"",
-			 * "\\\\\"" ) ); json.append( '\"' ); }
-			 */ else {
-				// As is
-				json.append(rejson.substring(lastEnd, end));
-			}
-		}
-		json.append(rejson.substring(end));
-		// System.out.println( json );
-		return json.toString();
-	}
-
-	public static String pythonRegExToJavaPattern(String pattern, boolean doubleQuote) {
-		pattern = pattern.replaceAll("\\\\", "\\\\\\\\");
-		pattern = pattern.replaceAll("\\{", "\\\\\\\\{");
-		pattern = pattern.replaceAll("\\}", "\\\\\\\\}");
-		if (!doubleQuote) {
-			pattern = pattern.replaceAll("\"", "\\\\\"");
-		}
-		// System.out.println( pattern );
-		return pattern;
-	}
-
-	public static String escapeHtml(String text) {
-		text = text.replace("&", "&");
-		text = text.replace("<", "<");
-		text = text.replace(">", ">");
-		text = text.replace("\"", """);
-		text = text.replace("'", "'");
-		return text;
-	}
-
-	public static String asHtml(String text) {
-		text = escapeHtml(text);
-		text = text.replace(" ", " ");
-		return text;
-	}
-
-	private static final Pattern DOUBLE_QUOTED_STRING = Pattern.compile("\"(?>\\\\.|.)*?\"");
-
-	private static final Pattern SINGLE_QUOTED_STRING = Pattern.compile("'(?>\\\\.|.)*?'");
-
-	public static String extJSON = ".jso";
-
-	public static InputStream getJsonFile(String filename) {
-		URI jarFileURI = null;
-		File jarFile;
-		InputStream stream = null;
-		filename = filename.replace('.', '/') + extJSON;
-		try {
-			jarFileURI = Jygments.class.getProtectionDomain().getCodeSource().getLocation().toURI();
-		} catch (URISyntaxException ex) {
-			//TODO error message
-		}
-		if (jarFileURI != null) {
-			if (jarFileURI.getScheme().equals("file")) {
-				jarFile = new File(jarFileURI.getPath(), filename);
-				if (jarFile.exists()) {
-					try {
-						stream = new FileInputStream(jarFile);
-					} catch (FileNotFoundException ex) {
-						//TODO error message
-					}
-				}
-			} else {
-				stream = Jygments.class.getClassLoader().getResourceAsStream(filename);
-			}
-		}
-		return stream;
-	}
+  public static String literalRegEx(String expression) {
+    return "\\Q" + expression + "\\E";
+  }
+
+  public static String replace(String string, String occurence, String replacement) {
+    return string.replaceAll(literalRegEx(occurence), replacement);
+  }
+
+  public static String streamToString(InputStream stream) throws IOException {
+    StringBuilder builder = new StringBuilder();
+    String line;
+
+    try {
+      BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
+      while ((line = reader.readLine()) != null) {
+        builder.append(line).append("\n");
+      }
+    } finally {
+      stream.close();
+    }
+
+    return builder.toString();
+  }
+
+  public static String rejsonToJson(InputStream stream) throws IOException {
+    String rejson = streamToString(stream);
+    String json = rejsonToJson(rejson, true);
+    json = rejsonToJson(json, false);
+    return json;
+  }
+
+  public static String rejsonToJson(String rejson, boolean doubleQuote) {
+    Matcher matcher = doubleQuote ? DOUBLE_QUOTED_STRING.matcher(rejson) : SINGLE_QUOTED_STRING.matcher(rejson);
+    StringBuilder json = new StringBuilder();
+    int start = 0, end = 0, lastEnd = 0;
+    while (matcher.find()) {
+      lastEnd = end;
+      start = matcher.start();
+      end = matcher.end();
+      if ((start > 0) && (rejson.charAt(start - 1) == 'r')) {
+        // Convert Python-style r"" string to Java-compatible pattern
+        String string = rejson.substring(start + 1, end - 1);
+        json.append(rejson.substring(lastEnd, start - 1));
+        json.append('\"');
+        json.append(pythonRegExToJavaPattern(string, doubleQuote));
+        json.append('\"');
+      } /*
+       * else if( !doubleQuote ) { // From single quote to double quote
+       * String string = rejson.substring( start + 1, end - 1 );
+       * json.append( rejson.substring( lastEnd, start - 1 ) );
+       * json.append( '\"' ); json.append( string.replaceAll( "\"",
+       * "\\\\\"" ) ); json.append( '\"' ); }
+       */ else {
+        // As is
+        json.append(rejson.substring(lastEnd, end));
+      }
+    }
+    json.append(rejson.substring(end));
+    // System.out.println( json );
+    return json.toString();
+  }
+
+  public static String pythonRegExToJavaPattern(String pattern, boolean doubleQuote) {
+    pattern = pattern.replaceAll("\\\\", "\\\\\\\\");
+    pattern = pattern.replaceAll("\\{", "\\\\\\\\{");
+    pattern = pattern.replaceAll("\\}", "\\\\\\\\}");
+    if (!doubleQuote) {
+      pattern = pattern.replaceAll("\"", "\\\\\"");
+    }
+    // System.out.println( pattern );
+    return pattern;
+  }
+
+  public static String escapeHtml(String text) {
+    text = text.replace("&", "&");
+    text = text.replace("<", "<");
+    text = text.replace(">", ">");
+    text = text.replace("\"", """);
+    text = text.replace("'", "'");
+    return text;
+  }
+
+  public static String asHtml(String text) {
+    text = escapeHtml(text);
+    text = text.replace(" ", " ");
+    return text;
+  }
+  private static final Pattern DOUBLE_QUOTED_STRING = Pattern.compile("\"(?>\\\\.|.)*?\"");
+  private static final Pattern SINGLE_QUOTED_STRING = Pattern.compile("'(?>\\\\.|.)*?'");
+  public static String extJSON = ".jso";
+
+  public static InputStream getJsonFile(String pack, String sub, String name, String fullname) {
+    URI jarFileURI = null;
+    File jarFile = null;
+    InputStream stream = null;
+    String jsonname = name.replace('.', '/') + extJSON;
+    fullname = fullname.replace('.', '/') + extJSON;
+    String filenamePack, filenameRoot;
+    try {
+      jarFileURI = Jygments.class.getProtectionDomain().getCodeSource().getLocation().toURI();
+    } catch (URISyntaxException ex) {
+      //TODO error message
+    }
+    if (jarFileURI != null) {
+      String jarFilePath = jarFileURI.getPath();
+      filenamePack = filenameRoot = jsonname;
+      if (jarFileURI.getScheme().equals("file")) {
+        if (!pack.isEmpty()) {
+          pack = pack.replace(".", "/");
+          if (!sub.isEmpty()) {
+            sub = sub.replace(".", "/");
+            pack = pack + "/" + sub;
+            filenameRoot = sub + "/" + jsonname;
+          }
+          filenamePack = pack + "/" + jsonname;
+        }
+        jarFile = new File(jarFilePath, filenamePack);
+        if (!jarFile.exists()) {
+          jarFile = new File(jarFilePath, filenameRoot);
+          if (!jarFile.exists()) {
+            jarFile = null;
+          }
+        }
+        if (jarFile != null) {
+          try {
+            stream = new FileInputStream(jarFile);
+          } catch (FileNotFoundException ex) {
+            //TODO error message
+          }
+        }
+      } else {
+        stream = Jygments.class.getClassLoader().getResourceAsStream(fullname);
+      }
+    }
+    return stream;
+  }
 }
diff --git a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java
index 18ac312..abd5396 100644
--- a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java
+++ b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/grammar/Lexer.java
@@ -60,29 +60,41 @@ public class Lexer extends Grammar
 		else
 		{
 			// Try contrib package
-			String pack = Jygments.class.getPackage().getName() + ".contrib";
-			lexer = getByFullName( pack + "." + name );
+			String pack = Jygments.class.getPackage().getName();
+			lexer = getByFullName( pack, "contrib", name );
 			if( lexer == null )
 			{
 				// Try this package
 				pack = Lexer.class.getPackage().getName();
-				lexer = getByFullName( pack + "." + name );
+				lexer = getByFullName( pack, "", name );
 			}
 			return lexer;
 		}
 	}
 
+	public static Lexer getByFullName( String name ) throws ResolutionException {
+    return getByFullName("", "", name);
+  }
+          
 	@SuppressWarnings("unchecked")
-	public static Lexer getByFullName( String fullName ) throws ResolutionException
+	public static Lexer getByFullName( String pack, String sub, String name ) throws ResolutionException
 	{
-		// Try cache
-		Lexer lexer = lexers.get( fullName );
+    String fullname = name;
+    if (!pack.isEmpty()) {
+      if (!sub.isEmpty()) {
+        fullname = pack + "." + sub + "." + fullname;
+      } else {
+        fullname = pack + "." + fullname;
+      }
+    }
+    // Try cache
+		Lexer lexer = lexers.get( fullname );
 		if( lexer != null )
 			return lexer;
 
 		try
 		{
-			return (Lexer) Jygments.class.getClassLoader().loadClass( fullName ).newInstance();
+			return (Lexer) Jygments.class.getClassLoader().loadClass( fullname ).newInstance();
 		}
 		catch( InstantiationException x )
 		{
@@ -94,7 +106,7 @@ public class Lexer extends Grammar
 		{
 		}
 
-		InputStream stream = Util.getJsonFile(fullName);
+		InputStream stream = Util.getJsonFile(pack, sub, name, fullname);
 		if( stream != null )
 		{
 			try
@@ -114,7 +126,7 @@ public class Lexer extends Grammar
 				if( lexer != null )
 				{
 					// Cache it
-					Lexer existing = lexers.putIfAbsent( fullName, lexer );
+					Lexer existing = lexers.putIfAbsent( fullname, lexer );
 					if( existing != null )
 						lexer = existing;
 				}
diff --git a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/style/Style.java b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/style/Style.java
index 3ad8ce8..6c99bf3 100644
--- a/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/style/Style.java
+++ b/Jygments4Sikuli/src/main/java/org/sikuli/syntaxhighlight/style/Style.java
@@ -27,6 +27,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import org.sikuli.syntaxhighlight.Jygments;
 import org.sikuli.syntaxhighlight.NestedDef;
 import org.sikuli.syntaxhighlight.ResolutionException;
+import org.sikuli.syntaxhighlight.Util;
 import org.sikuli.syntaxhighlight.grammar.TokenType;
 import org.sikuli.syntaxhighlight.style.def.StyleElementDef;
 
@@ -51,23 +52,34 @@ public class Style extends NestedDef<Style>
 		else
 		{
 			// Try contrib package
-			String pack = Jygments.class.getPackage().getName() + ".contrib";
-			name = pack + "." + name;
-			return getByFullName( name );
+			String pack = Jygments.class.getPackage().getName();
+			return getByFullName( pack, "contrib", name );
 		}
 	}
 
+	public static Style getByFullName( String name ) throws ResolutionException {
+    return getByFullName("", "", name);
+  }
+          
 	@SuppressWarnings("unchecked")
-	public static Style getByFullName( String fullName ) throws ResolutionException
+	public static Style getByFullName( String pack, String sub, String name ) throws ResolutionException
 	{
+    String fullname = name;
+    if (!pack.isEmpty()) {
+      if (!sub.isEmpty()) {
+        fullname = pack + "." + sub + "." + fullname;
+      } else {
+        fullname = pack + "." + fullname;
+      }
+    }
 		// Try cache
-		Style style = styles.get( fullName );
+		Style style = styles.get( fullname );
 		if( style != null )
 			return style;
 
 		try
 		{
-			return (Style) Jygments.class.getClassLoader().loadClass( fullName ).newInstance();
+			return (Style) Jygments.class.getClassLoader().loadClass( fullname ).newInstance();
 		}
 		catch( InstantiationException x )
 		{
@@ -79,7 +91,7 @@ public class Style extends NestedDef<Style>
 		{
 		}
 
-		InputStream stream = Jygments.class.getClassLoader().getResourceAsStream( fullName.replace( '.', '/' ) + extJSON );
+		InputStream stream = Util.getJsonFile(pack, sub, name, fullname);
 		if( stream != null )
 		{
 			ObjectMapper objectMapper = new ObjectMapper();
@@ -92,7 +104,7 @@ public class Style extends NestedDef<Style>
 				style.resolve();
 
 				// Cache it
-				Style existing = styles.putIfAbsent( fullName, style );
+				Style existing = styles.putIfAbsent( fullname, style );
 				if( existing != null )
 					style = existing;
 
diff --git a/Jygments4Sikuli/src/main/resources/jygments-legal/copyright.txt b/Jygments4Sikuli/src/main/resources/jygments-legal/copyright.txt
new file mode 100644
index 0000000..a46caea
--- /dev/null
+++ b/Jygments4Sikuli/src/main/resources/jygments-legal/copyright.txt
@@ -0,0 +1,8 @@
+
+Jygments is Copyright 2010-2013 Three Crickets LLC:
+
+http://threecrickets.com/
+  
+All third-party libraries redistributed with this software remain the property 
+of their respective copyright owners and are subject to separate license 
+agreements.
diff --git a/Jygments4Sikuli/src/main/resources/jygments-legal/license.txt b/Jygments4Sikuli/src/main/resources/jygments-legal/license.txt
new file mode 100644
index 0000000..ec67b93
--- /dev/null
+++ b/Jygments4Sikuli/src/main/resources/jygments-legal/license.txt
@@ -0,0 +1,25 @@
+Copyright (c) 2010 by the respective authors (see copyright.txt file).
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+* Redistributions of source code must retain the above copyright
+  notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above copyright
+  notice, this list of conditions and the following disclaimer in the
+  documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/sikuli.git



More information about the pkg-java-commits mailing list