diff --git a/application.py b/application.py index 9d91337..2286a6d 100755 --- a/application.py +++ b/application.py @@ -10,7 +10,7 @@ from point2 import Point import mode2 import mode.mini, mode.search, mode.replace, mode.which import mode.console, mode.consolemini -import mode.c, mode.python, mode.perl, mode.nasm, mode.sh, mode.sql +import mode.c, mode.python, mode.perl, mode.nasm, mode.sh, mode.sql, mode.java import mode.blame, mode.diff, mode.dir import mode.xml, mode.tt, mode.css, mode.javascript, mode.html import mode.text, mode.mutt @@ -106,6 +106,7 @@ class Application(object): 'template': mode.tt.Template, 'bds': mode.bds.BDS, 'rst': mode.rst.RST, + 'java': mode.java.Java, } # these are used in this order to determine which mode to open certain @@ -138,6 +139,7 @@ class Application(object): '.tt': 'template', '.css': 'css', '.rst': 'rst', + '.java': 'java', } self.mode_detection = { 'python': 'python', diff --git a/code_examples/HibernateUtil.java b/code_examples/HibernateUtil.java new file mode 100644 index 0000000..709b55c --- /dev/null +++ b/code_examples/HibernateUtil.java @@ -0,0 +1,108 @@ +///////////////////////////////////////////////////////////////////////// +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.Statement; + +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + + public static final SessionFactory sessionFactory; + + static { + try { + // Create the SessionFactory from hibernate.cfg.xml + sessionFactory = new Configuration().configure().buildSessionFactory(); + } catch (Throwable ex) { + // Make sure you log the exception, as it might be swallowed + System.err.println("Initial SessionFactory creation failed." + ex); + throw new ExceptionInInitializerError(ex); + } + } + + public static final ThreadLocal session = new ThreadLocal(); + + public static Session currentSession() throws HibernateException { + Session s = (Session) session.get(); + // Open a new Session, if this thread has none yet + if (s == null) { + s = sessionFactory.openSession(); + // Store it in the ThreadLocal variable + session.set(s); + } + return s; + } + + public static void closeSession() throws HibernateException { + Session s = (Session) session.get(); + if (s != null) + s.close(); + session.set(null); + } + + static Connection conn; + static Statement st; + public static void setup(String sql) { + try { + // Step 1: Load the JDBC driver. + Class.forName("org.hsqldb.jdbcDriver"); + System.out.println("Driver Loaded."); + // Step 2: Establish the connection to the database. + String url = "jdbc:hsqldb:data/tutorial"; + + conn = DriverManager.getConnection(url, "sa", ""); + System.out.println("Got Connection."); + + st = conn.createStatement(); + st.executeUpdate(sql); + } catch (Exception e) { + System.err.println("Got an exception! "); + e.printStackTrace(); + System.exit(0); + } + } + public static void checkData(String sql) { + try { + HibernateUtil.outputResultSet(st + .executeQuery(sql)); +// conn.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void outputResultSet(ResultSet rs) throws Exception{ + ResultSetMetaData metadata = rs.getMetaData(); + + int numcols = metadata.getColumnCount(); + String[] labels = new String[numcols]; + int[] colwidths = new int[numcols]; + int[] colpos = new int[numcols]; + int linewidth; + + for (int i = 0; i < numcols; i++) { + labels[i] = metadata.getColumnLabel(i + 1); // get its label + System.out.print(labels[i]+" "); + } + System.out.println("------------------------"); + + while (rs.next()) { + for (int i = 0; i < numcols; i++) { + Object value = rs.getObject(i + 1); + if(value == null){ + System.out.print(" "); + }else{ + System.out.print(value.toString().trim()+" "); + } + + } + System.out.println(" "); + } + } +} diff --git a/mode/c.py b/mode/c.py index d80ba79..383b7c6 100644 --- a/mode/c.py +++ b/mode/c.py @@ -30,7 +30,7 @@ class CGrammar(Grammar): RegionRule(r'comment', '/\*', Grammar, '\*/'), PatternRule(r'comment', r'//.*$'), - PatternRule(r'keyword', r"(?:auto|break|case|char|const|continue|default|double|do|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)(?![a-zA-z_])"), + PatternRule(r'keyword', r"(?:auto|break|case|char|const|continue|default|double|do|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)(?![a-zA-Z_])"), PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'), PatternRule(r'structname', r'(?<=struct ) *[a-zA-Z_][a-zA-Z0-9_]*'), PatternRule(r'enumname', r'(?<=enum ) *[a-zA-Z_][a-zA-Z0-9_]*'), diff --git a/mode/java.py b/mode/java.py new file mode 100644 index 0000000..fff8c19 --- /dev/null +++ b/mode/java.py @@ -0,0 +1,64 @@ +import color, mode2, tab2 +from lex3 import Grammar, PatternRule, RegionRule +from mode.python import StringGrammar +from mode.c import CTabber + +class JavaGrammar(Grammar): + rules = [ + PatternRule(r'package', r'(?<=import ) *[a-zA-Z0-9_.*]+'), + + RegionRule(r'comment', '/\*', Grammar, '\*/'), + PatternRule(r'comment', r'//.*$'), + + PatternRule(r'keyword', r"(?:abstract|assert|boolean|break|byte|case|catch|char|class|continue|default|double|do|else|extends|finally|final|float|for|if|implements|import|instanceof|int|long|new|private|protected|public|return|short|static|switch|super|synchronized|throws|throw|try|void|while)(?![a-zA-Z_])"), + PatternRule(r'label', r'[a-zA-Z_][a-zA-Z0-9_]*(?=:)'), + + PatternRule(r'builtin', r"(?:null|true|false|this)"), + PatternRule(r'identifier', r"[a-zA-Z_][a-zA-Z0-9_]*"), + PatternRule(r"unop", r"\+=|-=|\*=|/=|//=|%=|&=\|\^=|>>=|<<=|\*\*="), + PatternRule(r'binop', r"\+|<>|<<|<=|<|-|>>|>=|>|\*\*|&|\*|\||/|\^|==|//|~|!=|%"), + + PatternRule(r"delimiter", r"->|\.|\(|\)|\[|\]|{|}|@|,|:|`|;|=|\?"), + + PatternRule(r"integer", r"(?:0(?![x0-9])|[1-9][0-9]*|0[0-7]+|0[xX][0-9a-fA-F]+)[lL]?"), + PatternRule(r"float", r"[0-9]+\.[0-9]*|\.[0-9]+|(?:[0-9]|[0-9]+\.[0-9]*|\.[0-9]+)[eE][\+-]?[0-9]+"), + RegionRule(r'string', '"', StringGrammar, '"'), + PatternRule(r'char', r"'.'|'\\.'|'\\[0-7]{3}'"), + PatternRule(r"eol", r"\n$"), + ] + +class Java(mode2.Fundamental): + #tabbercls = CTabber + grammar = JavaGrammar + opentokens = ('delimiter',) + opentags = {'(': ')', '[': ']', '{': '}'} + closetokens = ('delimiter',) + closetags = {')': '(', ']': '[', '}': '{'} + colors = { + 'comment': ('red', 'default'), + 'comment.start': ('red', 'default'), + 'comment.end': ('red', 'default'), + 'comment.null': ('red', 'default'), + + 'package': ('blue', 'default'), + 'label': ('magenta', 'default'), + 'keyword': ('cyan', 'default'), + 'function': ('blue', 'default'), + 'builtin': ('magenta', 'default'), + + 'char': ('green', 'default'), + 'string.start': ('green', 'default'), + 'string.octal': ('green', 'default'), + 'string.escaped': ('green', 'default'), + 'string.null': ('green', 'default'), + 'string.end': ('green', 'default'), + 'integer': ('green', 'default'), + 'float': ('green', 'default'), + } + def __init__(self, w): + mode2.Fundamental.__init__(self, w) + self.add_bindings('close-paren', (')',)) + self.add_bindings('close-brace', ('}',)) + self.add_bindings('close-bracket', (']',)) + def name(self): + return "Java"