├── .github └── workflows │ ├── codeql-analysis.yml │ └── maven.yml ├── .gitignore ├── COPYING ├── LICENSE_CDDL.txt ├── LICENSE_LGPL.txt ├── README.md ├── pom.xml └── src ├── main ├── java │ ├── com │ │ └── uwyn │ │ │ └── jhighlight │ │ │ └── renderer │ │ │ ├── Renderer.java │ │ │ └── XhtmlRendererFactory.java │ └── org │ │ └── codelibs │ │ └── jhighlight │ │ ├── JHighlight.java │ │ ├── JHighlightVersion.java │ │ ├── JHighlightVersionSingleton.java │ │ ├── fastutil │ │ ├── Arrays.java │ │ ├── BidirectionalIterator.java │ │ ├── Function.java │ │ ├── Hash.java │ │ ├── HashCommon.java │ │ ├── Stack.java │ │ ├── Swapper.java │ │ ├── booleans │ │ │ ├── BooleanArrays.java │ │ │ └── BooleanComparator.java │ │ ├── chars │ │ │ ├── AbstractChar2ObjectFunction.java │ │ │ ├── AbstractChar2ObjectMap.java │ │ │ ├── AbstractCharBidirectionalIterator.java │ │ │ ├── AbstractCharCollection.java │ │ │ ├── AbstractCharIterator.java │ │ │ ├── AbstractCharList.java │ │ │ ├── AbstractCharListIterator.java │ │ │ ├── AbstractCharSet.java │ │ │ ├── Char2ObjectFunction.java │ │ │ ├── Char2ObjectFunctions.java │ │ │ ├── Char2ObjectMap.java │ │ │ ├── Char2ObjectOpenHashMap.java │ │ │ ├── CharArrayList.java │ │ │ ├── CharArrays.java │ │ │ ├── CharBidirectionalIterator.java │ │ │ ├── CharCollection.java │ │ │ ├── CharComparator.java │ │ │ ├── CharIterable.java │ │ │ ├── CharIterator.java │ │ │ ├── CharIterators.java │ │ │ ├── CharList.java │ │ │ ├── CharListIterator.java │ │ │ ├── CharSet.java │ │ │ └── CharStack.java │ │ ├── ints │ │ │ ├── IntArrays.java │ │ │ └── IntComparator.java │ │ └── objects │ │ │ ├── AbstractObjectBidirectionalIterator.java │ │ │ ├── AbstractObjectCollection.java │ │ │ ├── AbstractObjectIterator.java │ │ │ ├── AbstractObjectList.java │ │ │ ├── AbstractObjectListIterator.java │ │ │ ├── AbstractObjectSet.java │ │ │ ├── ObjectArrayList.java │ │ │ ├── ObjectArrays.java │ │ │ ├── ObjectBidirectionalIterator.java │ │ │ ├── ObjectCollection.java │ │ │ ├── ObjectIterable.java │ │ │ ├── ObjectIterator.java │ │ │ ├── ObjectIterators.java │ │ │ ├── ObjectList.java │ │ │ ├── ObjectListIterator.java │ │ │ └── ObjectSet.java │ │ ├── highlighter │ │ ├── CppHighlighter.flex │ │ ├── CppHighlighter.java │ │ ├── ExplicitStateHighlighter.java │ │ ├── GroovyHighlighter.flex │ │ ├── GroovyHighlighter.java │ │ ├── JavaHighlighter.flex │ │ ├── JavaHighlighter.java │ │ ├── JavaScriptHighlighter.flex │ │ ├── JavaScriptHighlighter.java │ │ ├── XmlHighlighter.flex │ │ └── XmlHighlighter.java │ │ ├── renderer │ │ ├── CppXhtmlRenderer.java │ │ ├── GroovyXhtmlRenderer.java │ │ ├── JavaScriptXhtmlRenderer.java │ │ ├── JavaXhtmlRenderer.java │ │ ├── Renderer.java │ │ ├── XhtmlRenderer.java │ │ ├── XhtmlRendererFactory.java │ │ └── XmlXhtmlRenderer.java │ │ ├── servlet │ │ └── HighlightFilter.java │ │ └── tools │ │ ├── ExceptionUtils.java │ │ ├── FileUtils.java │ │ ├── StringUtils.java │ │ └── exceptions │ │ └── FileUtilsErrorException.java └── resources │ ├── JHIGHLIGHT_VERSION │ └── jhighlight.properties └── test └── java ├── com └── uwyn │ └── jhighlight │ └── renderer │ └── XhtmlRendererFactoryTest.java └── org └── codelibs └── jhighlight ├── renderer └── XhtmlRendererFactoryTest.java └── tools └── StringUtilsTest.java /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | name: "CodeQL" 7 | 8 | on: 9 | push: 10 | branches: [master] 11 | pull_request: 12 | # The branches below must be a subset of the branches above 13 | branches: [master] 14 | schedule: 15 | - cron: '0 8 * * 3' 16 | 17 | jobs: 18 | analyze: 19 | name: Analyze 20 | runs-on: ubuntu-latest 21 | 22 | strategy: 23 | fail-fast: false 24 | matrix: 25 | # Override automatic language detection by changing the below list 26 | # Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python'] 27 | language: ['java'] 28 | # Learn more... 29 | # https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection 30 | 31 | steps: 32 | - name: Checkout repository 33 | uses: actions/checkout@v2 34 | with: 35 | # We must fetch at least the immediate parents so that if this is 36 | # a pull request then we can checkout the head. 37 | fetch-depth: 2 38 | 39 | # If this run was triggered by a pull request event, then checkout 40 | # the head of the pull request instead of the merge commit. 41 | - run: git checkout HEAD^2 42 | if: ${{ github.event_name == 'pull_request' }} 43 | 44 | # Initializes the CodeQL tools for scanning. 45 | - name: Initialize CodeQL 46 | uses: github/codeql-action/init@v1 47 | with: 48 | languages: ${{ matrix.language }} 49 | # If you wish to specify custom queries, you can do so here or in a config file. 50 | # By default, queries listed here will override any specified in a config file. 51 | # Prefix the list here with "+" to use these queries and those in the config file. 52 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 53 | 54 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 55 | # If this step fails, then you should remove it and run the build manually (see below) 56 | - name: Autobuild 57 | uses: github/codeql-action/autobuild@v1 58 | 59 | # ℹ️ Command-line programs to run using the OS shell. 60 | # 📚 https://git.io/JvXDl 61 | 62 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 63 | # and modify them (or add more) to build your code if your project 64 | # uses a compiled language 65 | 66 | #- run: | 67 | # make bootstrap 68 | # make release 69 | 70 | - name: Perform CodeQL Analysis 71 | uses: github/codeql-action/analyze@v1 72 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: Java CI with Maven 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 11 20 | uses: actions/setup-java@v2 21 | with: 22 | java-version: '11' 23 | distribution: 'adopt' 24 | - name: Build with Maven 25 | run: mvn -B package --file pom.xml 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /bin 3 | /work 4 | .settings 5 | .classpath 6 | .project 7 | .idea 8 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | JHighlight is Copyright (C) 2000-2006 2 | * Omnicore Software, Hans Kratz & Dennis Strein GbR, 3 | * Geert Bevin 4 | * Arnout Engelen 5 | 6 | It is distributed under the terms of either: 7 | - the common development and distribution license (CDDL), v1.0; or 8 | - the GNU Lesser General Public License, v2.1 or later 9 | -------------------------------------------------------------------------------- /LICENSE_CDDL.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codelibs/jhighlight/7758dfe9f117622a577ae078f81842954ed90e06/LICENSE_CDDL.txt -------------------------------------------------------------------------------- /LICENSE_LGPL.txt: -------------------------------------------------------------------------------- 1 | RIFE, 2 | 3 | Copyright 2001-2006 Geert Bevin , 4 | 5 | Distributed under the GNU Lesser General Public License v2.1. 6 | 7 | This library is free software; you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation; either 10 | version 2.1 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library; if not, write to the Free Software 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 | Distributed under the GNU General Public License v2 or later. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JHighlight 2 | [![Java CI with Maven](https://github.com/codelibs/jhighlight/actions/workflows/maven.yml/badge.svg)](https://github.com/codelibs/jhighlight/actions/workflows/maven.yml) 3 | ================== 4 | 5 | ## Overview 6 | 7 | JHighlight is an embeddable pure Java syntax highlighting library that supports Java, HTML, XHTML, XML and LZX languages and outputs to XHTML. 8 | It also supports RIFE templates tags and highlights them clearly so that you can easily identify the difference between your RIFE markup and the actual marked up source. 9 | 10 | This project is forked from https://jhighlight.dev.java.net/ to fix several bugs. 11 | 12 | ## Reference 13 | 14 | The original jhighlight handles multi-byte characters as garbled one. To solve this problem, replace with CodeLibs jhighlight. 15 | 16 | ### Tika 17 | 18 | Tika uses jhighlight to parse source code files, such as .java. 19 | If a source code file has a multi-byte chracter, it becomes a garbled one. 20 | To avoid this problem, change your pom.xml to: 21 | 22 | 23 | org.apache.tika 24 | tika-parsers 25 | 1.6 26 | 27 | 28 | com.uwyn 29 | jhighlight 30 | 31 | 32 | 33 | 34 | org.codelibs 35 | jhighlight 36 | 1.0.2 37 | 38 | 39 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | org.codelibs 5 | jhighlight 6 | 1.1.1-SNAPSHOT 7 | jar 8 | JHighlight 9 | 10 | JHighlight is an embeddable pure Java syntax highlighting 11 | library that supports Java, HTML, XHTML, XML and LZX 12 | languages and outputs to XHTML. 13 | 14 | It also supports RIFE templates tags and highlights them 15 | clearly so that you can easily identify the difference 16 | between your RIFE markup and the actual marked up source. 17 | 18 | 2011 19 | https://github.com/codelibs/jhighlight 20 | 21 | 22 | CDDL, v1.0 23 | http://www.opensource.org/licenses/cddl1.php 24 | repo 25 | 26 | 27 | LGPL, v2.1 or later 28 | http://www.opensource.org/licenses/lgpl-license.php 29 | repo 30 | 31 | 32 | 33 | CodeLibs Project 34 | https://www.codelibs.org/ 35 | 36 | 37 | 38 | shinsuke 39 | Shinsuke Sugaya 40 | shinsuke.sugaya@codelibs.co 41 | CodeLibs Inc. 42 | https://codelibs.co 43 | 44 | 45 | 46 | scm:git:git@github.com:codelibs/jhighlight.git 47 | scm:git:git@github.com:codelibs/jhighlight.git 48 | git@github.com:codelibs/jhighlight.git 49 | 50 | 51 | UTF-8 52 | 53 | 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-compiler-plugin 58 | 3.14.0 59 | 60 | 1.8 61 | 1.8 62 | 63 | 64 | 65 | org.apache.maven.plugins 66 | maven-surefire-plugin 67 | 3.5.0 68 | 69 | 70 | **/*Test.java 71 | 72 | false 73 | 74 | 75 | 76 | org.jacoco 77 | jacoco-maven-plugin 78 | 0.8.13 79 | 80 | 81 | 82 | prepare-agent 83 | 84 | 85 | 86 | report 87 | prepare-package 88 | 89 | report 90 | 91 | 92 | 93 | 94 | 95 | org.apache.maven.plugins 96 | maven-javadoc-plugin 97 | 3.11.2 98 | 99 | UTF-8 100 | UTF-8 101 | UTF-8 102 | 8 103 | none 104 | 105 | 106 | 107 | maven-jar-plugin 108 | 3.4.2 109 | 110 | 111 | 112 | org.codelibs.jhighlight 113 | 114 | 115 | 116 | 117 | 118 | org.apache.maven.plugins 119 | maven-gpg-plugin 120 | 3.2.7 121 | 122 | 123 | sign-artifacts 124 | verify 125 | 126 | sign 127 | 128 | 129 | 130 | 131 | 132 | org.sonatype.central 133 | central-publishing-maven-plugin 134 | 0.7.0 135 | true 136 | 137 | central 138 | 139 | 140 | 141 | 142 | 143 | 144 | commons-io 145 | commons-io 146 | 2.19.0 147 | 148 | 149 | javax.servlet 150 | servlet-api 151 | 2.3 152 | provided 153 | 154 | 155 | junit 156 | junit 157 | 4.13.2 158 | test 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /src/main/java/com/uwyn/jhighlight/renderer/Renderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed under the terms of either: 3 | * - the common development and distribution license (CDDL), v1.0; or 4 | * - the GNU Lesser General Public License, v2.1 or later 5 | */ 6 | package com.uwyn.jhighlight.renderer; 7 | 8 | import java.io.IOException; 9 | import java.io.InputStream; 10 | import java.io.OutputStream; 11 | 12 | @Deprecated // for backward compatibility 13 | public interface Renderer { 14 | void highlight(String name, InputStream in, OutputStream out, 15 | String encoding, boolean fragment) throws IOException; 16 | 17 | String highlight(String name, String in, String encoding, boolean fragment) 18 | throws IOException; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/uwyn/jhighlight/renderer/XhtmlRendererFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Distributed under the terms of either: 3 | * - the common development and distribution license (CDDL), v1.0; or 4 | * - the GNU Lesser General Public License, v2.1 or later 5 | */ 6 | package com.uwyn.jhighlight.renderer; 7 | 8 | @Deprecated // for backward compatibility 9 | public class XhtmlRendererFactory { 10 | 11 | public static final String GROOVY = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.GROOVY; 12 | 13 | public static final String JAVA = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.JAVA; 14 | 15 | public static final String BEANSHELL = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.BEANSHELL; 16 | 17 | public static final String BSH = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.BSH; 18 | 19 | public static final String XML = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.XML; 20 | 21 | public static final String XHTML = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.XHTML; 22 | 23 | public static final String LZX = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.LZX; 24 | 25 | public static final String HTML = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.HTML; 26 | 27 | public static final String CPP = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.CPP; 28 | 29 | public static final String CXX = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.CXX; 30 | 31 | public static final String CPLUSPLUS = org.codelibs.jhighlight.renderer.XhtmlRendererFactory.CPP; 32 | 33 | public static Renderer getRenderer(String type) { 34 | return org.codelibs.jhighlight.renderer.XhtmlRendererFactory 35 | .getRenderer(type); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/JHighlight.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: JHighlight.java 3429 2006-08-02 02:38:39Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight; 9 | 10 | import java.io.File; 11 | import java.io.FileOutputStream; 12 | import java.io.IOException; 13 | import java.util.ArrayList; 14 | import java.util.Iterator; 15 | import java.util.Set; 16 | import java.util.regex.Pattern; 17 | 18 | import org.codelibs.jhighlight.renderer.XhtmlRendererFactory; 19 | import org.codelibs.jhighlight.tools.FileUtils; 20 | 21 | /** 22 | * Provides console access to the source code syntax highlighting for Java, 23 | * HTML, XHTML, XML and LZX files. The rendering will be done in HTML. 24 | *

The following file extensions will be processed: .java, 25 | * .html, .htm, .xhtml, 26 | * .xml and .lzx. 27 | *

Execute the highlighting with the following syntax: 28 | *

java org.codelibs.jhighlight.JHighlight [--verbose] [--fragment] [-d destdir] [-e encoding] file|dir ...
29 | *

or 30 | *

java -jar jhighlight.jar [--verbose] [--fragment] [-d destdir] [-e encoding] file|dir ...
31 | * 32 | * 33 | * 34 | * 35 | * 36 | * 37 | * 38 | * 39 | * 40 | * 41 | * 42 | * 43 | * 44 | * 45 | * 46 | * 47 | * 48 | *
--verboseOutput messages about what the parser is doing.
--fragmentOutput fragments instead of complete documents.
-dSpecify the destination directory
-eSpecify the encoding of the files
49 | *

RIFE template tags are also 50 | * supported and will be clearly highlighted. 51 | * 52 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 53 | * @version $Revision: 3429 $ 54 | * @since 1.0 55 | */ 56 | public class JHighlight 57 | { 58 | public static void main(String[] arguments) throws Throwable 59 | { 60 | String destdir_name = null; 61 | boolean verbose = false; 62 | String encoding = null; 63 | boolean fragment = false; 64 | ArrayList names = new ArrayList(); 65 | 66 | boolean valid_arguments = true; 67 | if (arguments.length < 1) 68 | { 69 | valid_arguments = false; 70 | } 71 | else 72 | { 73 | boolean next_is_destdir = false; 74 | boolean next_is_encoding = false; 75 | String argument; 76 | for (int i = 0; i < arguments.length; i++) 77 | { 78 | argument = arguments[i]; 79 | if (next_is_destdir) 80 | { 81 | destdir_name = argument; 82 | next_is_destdir = false; 83 | continue; 84 | } 85 | 86 | if (next_is_encoding) 87 | { 88 | encoding = argument; 89 | next_is_encoding = false; 90 | continue; 91 | } 92 | 93 | if (argument.equals("-d")) 94 | { 95 | next_is_destdir = true; 96 | continue; 97 | } 98 | 99 | if (argument.equals("-e")) 100 | { 101 | next_is_encoding = true; 102 | continue; 103 | } 104 | 105 | if (argument.equals("--verbose")) 106 | { 107 | verbose = true; 108 | continue; 109 | } 110 | 111 | if (argument.equals("--fragment")) 112 | { 113 | fragment = true; 114 | continue; 115 | } 116 | 117 | names.add(argument); 118 | } 119 | } 120 | 121 | if (0 == names.size()) 122 | { 123 | valid_arguments = false; 124 | } 125 | 126 | if (!valid_arguments) 127 | { 128 | System.err.println("Usage :"); 129 | System.err.println(" java " + JHighlight.class.getName() + " [--verbose] [--fragment] [-d destdir] [-e encoding] file|dir ..."); 130 | System.err.println("or"); 131 | System.err.println(" java -jar jhighlight-" + JHighlightVersion.getVersion() + ".jar [--verbose] [--fragment] [-d destdir] [-e encoding] file|dir ..."); 132 | System.err.println("Generates highlighted XHTML files from all Java and XML source files"); 133 | System.err.println("in the specified directories."); 134 | System.err.println(" --verbose Output messages about what the parser is doing"); 135 | System.err.println(" --fragment Output fragments instead of complete documents"); 136 | System.err.println(" -d Specify the destination directory"); 137 | System.err.println(" -e Specify the encoding of the files"); 138 | System.exit(1); 139 | } 140 | 141 | File destdir = null; 142 | if (destdir_name != null) 143 | { 144 | destdir = new File(destdir_name); 145 | if (!destdir.exists()) 146 | { 147 | throw new IOException("The destination directory '" + destdir_name + "' doesn't exist."); 148 | } 149 | if (!destdir.canWrite()) 150 | { 151 | throw new IOException("The destination directory '" + destdir_name + "' is not writable."); 152 | } 153 | if (!destdir.isDirectory()) 154 | { 155 | throw new IOException("The destination directory '" + destdir_name + "' is not a directory."); 156 | } 157 | } 158 | 159 | Iterator names_it = names.iterator(); 160 | String name; 161 | while (names_it.hasNext()) 162 | { 163 | name = (String)names_it.next(); 164 | 165 | File location = new File(name); 166 | if (!location.exists()) 167 | { 168 | throw new IOException("The source location '" + name + "' doesn't exist."); 169 | } 170 | if (!location.canRead()) 171 | { 172 | throw new IOException("The source location '" + name + "' is not readable."); 173 | } 174 | 175 | if (!location.isDirectory()) 176 | { 177 | File out = null; 178 | if (null == destdir) 179 | { 180 | out = new File(location.getAbsolutePath() + ".html"); 181 | } 182 | else 183 | { 184 | out = new File(destdir, location.getName() + ".html"); 185 | } 186 | 187 | highlightFile(location.getName(), location, out, encoding, fragment, verbose); 188 | } 189 | else 190 | { 191 | Set supported_types = XhtmlRendererFactory.getSupportedTypes(); 192 | Pattern[] included = new Pattern[supported_types.size()]; 193 | Pattern[] excluded = new Pattern[supported_types.size()+5]; 194 | excluded[0] = Pattern.compile(".*SCCS.*"); 195 | excluded[0] = Pattern.compile(".*svn.*"); 196 | excluded[0] = Pattern.compile(".*CVS.*"); 197 | excluded[0] = Pattern.compile(".*jetty.*"); 198 | excluded[0] = Pattern.compile(".*tomcat.*"); 199 | 200 | Iterator types_it = supported_types.iterator(); 201 | String type; 202 | int counter = 0; 203 | while (types_it.hasNext()) 204 | { 205 | type = (String)types_it.next(); 206 | included[counter] = Pattern.compile(".*\\."+type+"$"); 207 | excluded[counter+5] = Pattern.compile(".*\\."+type+"\\.html\\.*"); 208 | 209 | counter++; 210 | } 211 | 212 | ArrayList file_names = FileUtils.getFileList(location, included, excluded); 213 | 214 | Iterator file_names_it = file_names.iterator(); 215 | String file_name; 216 | while (file_names_it.hasNext()) 217 | { 218 | file_name = (String)file_names_it.next(); 219 | 220 | File in = new File(location.getAbsolutePath() + File.separator + file_name); 221 | File out = null; 222 | if (null == destdir) 223 | { 224 | out = new File(location.getAbsolutePath() + File.separator + file_name + ".html"); 225 | } 226 | else 227 | { 228 | out = new File(destdir, location.getName() + File.separator + file_name + ".html"); 229 | } 230 | 231 | highlightFile(location.getName() + File.separator + file_name, in, out, encoding, fragment, verbose); 232 | } 233 | } 234 | } 235 | } 236 | 237 | private static void highlightFile(String name, File in, File out, String encoding, boolean fragment, boolean verbose) 238 | throws IOException 239 | { 240 | out.getParentFile().mkdirs(); 241 | 242 | if (verbose) 243 | { 244 | System.out.print(name + " ... "); 245 | } 246 | 247 | XhtmlRendererFactory.getRenderer(FileUtils.getExtension(name)) 248 | .highlight(name, 249 | in.toURL().openStream(), 250 | new FileOutputStream(out), 251 | encoding, 252 | fragment); 253 | 254 | if (verbose) 255 | { 256 | System.out.println("done."); 257 | } 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/JHighlightVersion.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: JHighlightVersion.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.net.URL; 13 | import java.net.URLConnection; 14 | 15 | import org.apache.commons.io.output.ByteArrayOutputStream; 16 | 17 | /** 18 | * Provides acces to the version number of this JHighlight release. 19 | * 20 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 21 | * @version $Revision: 3108 $ 22 | * @since 1.0 23 | */ 24 | public class JHighlightVersion 25 | { 26 | private String mVersion = null; 27 | 28 | JHighlightVersion() 29 | { 30 | URL version_url = getClass().getClassLoader().getResource("JHIGHLIGHT_VERSION"); 31 | if (version_url != null) 32 | { 33 | try 34 | { 35 | URLConnection connection = version_url.openConnection(); 36 | connection.setUseCaches(false); 37 | InputStream inputStream = connection.getInputStream(); 38 | 39 | byte[] buffer = new byte[64]; 40 | int return_value = -1; 41 | ByteArrayOutputStream output_stream = new ByteArrayOutputStream(buffer.length); 42 | 43 | try 44 | { 45 | return_value = inputStream.read(buffer); 46 | 47 | while (-1 != return_value) 48 | { 49 | output_stream.write(buffer, 0, return_value); 50 | return_value = inputStream.read(buffer); 51 | } 52 | } 53 | finally 54 | { 55 | output_stream.close(); 56 | inputStream.close(); 57 | } 58 | 59 | mVersion = output_stream.toString("UTF-8"); 60 | } 61 | catch (IOException e) 62 | { 63 | mVersion = null; 64 | } 65 | } 66 | 67 | if (mVersion != null) 68 | { 69 | mVersion = mVersion.trim(); 70 | } 71 | if (null == mVersion) 72 | { 73 | mVersion = "(unknown version)"; 74 | } 75 | } 76 | 77 | private String getVersionString() 78 | { 79 | return mVersion; 80 | } 81 | 82 | /** 83 | * Returns the version number of this JHighlight release. 84 | * 85 | * @return the version number 86 | * @since 1.0 87 | */ 88 | public static String getVersion() 89 | { 90 | return JHighlightVersionSingleton.INSTANCE.getVersionString(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/JHighlightVersionSingleton.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: JHighlightVersionSingleton.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight; 9 | 10 | /** 11 | * Helper class to avoid Double Check Locking 12 | * and still have a thread-safe singleton pattern 13 | */ 14 | class JHighlightVersionSingleton 15 | { 16 | static final JHighlightVersion INSTANCE = new JHighlightVersion(); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/BidirectionalIterator.java: -------------------------------------------------------------------------------- 1 | package org.codelibs.jhighlight.fastutil; 2 | 3 | /* 4 | * Copyright (C) 2002-2014 Sebastiano Vigna 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | 20 | import java.util.Iterator; 21 | import java.util.ListIterator; 22 | 23 | /** A bidirectional {@link Iterator}. 24 | * 25 | *

This kind of iterator is essentially a {@link ListIterator} that 26 | * does not support {@link ListIterator#previousIndex()} and {@link 27 | * ListIterator#nextIndex()}. It is useful for those maps that can easily 28 | * provide bidirectional iteration, but provide no index. 29 | * 30 | *

Note that iterators returned by fastutil classes are more 31 | * specific, and support skipping. This class serves the purpose of organising 32 | * in a cleaner way the relationships between various iterators. 33 | * 34 | * @see Iterator 35 | * @see ListIterator 36 | */ 37 | 38 | public interface BidirectionalIterator extends Iterator { 39 | 40 | /** Returns the previous element from the collection. 41 | * 42 | * @return the previous element from the collection. 43 | * @see java.util.ListIterator#previous() 44 | */ 45 | 46 | K previous(); 47 | 48 | /** Returns whether there is a previous element. 49 | * 50 | * @return whether there is a previous element. 51 | * @see java.util.ListIterator#hasPrevious() 52 | */ 53 | 54 | boolean hasPrevious(); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/Function.java: -------------------------------------------------------------------------------- 1 | package org.codelibs.jhighlight.fastutil; 2 | 3 | /* 4 | * Copyright (C) 2002-2014 Sebastiano Vigna 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | 20 | /** A function mapping keys into values. 21 | * 22 | *

Instances of this class represent functions: the main difference with {@link java.util.Map} 23 | * is that functions do not in principle allow enumeration of their domain or range. The need for 24 | * this interface lies in the existence of several highly optimized implementations of 25 | * functions (e.g., minimal perfect hashes) which do not actually store their domain or range explicitly. 26 | * In case the domain is known, {@link #containsKey(Object)} can be used to perform membership queries. 27 | * 28 | *

The choice of naming all methods exactly as in {@link java.util.Map} makes it possible 29 | * for all type-specific maps to extend type-specific functions (e.g., {@link org.codelibs.jhighlight.fastutil.ints.Int2IntMap} 30 | * extends {@link org.codelibs.jhighlight.fastutil.ints.Int2IntFunction}). However, {@link #size()} is allowed to return -1 to denote 31 | * that the number of keys is not available (e.g., in the case of a string hash function). 32 | * 33 | *

Note that there is an {@link org.codelibs.jhighlight.fastutil.objects.Object2ObjectFunction} that 34 | * can also set its default return value. 35 | * 36 | *

Warning: Equality of functions is not specified 37 | * by contract, and it will usually be by reference, as there is no way to enumerate the keys 38 | * and establish whether two functions represent the same mathematical entity. 39 | * 40 | * @see java.util.Map 41 | */ 42 | 43 | public interface Function { 44 | 45 | /** Associates the specified value with the specified key in this function (optional operation). 46 | * 47 | * @param key the key. 48 | * @param value the value. 49 | * @return the old value, or null if no value was present for the given key. 50 | * @see java.util.Map#put(Object,Object) 51 | */ 52 | 53 | V put( K key, V value ); 54 | 55 | /** Returns the value associated by this function to the specified key. 56 | * 57 | * @param key the key. 58 | * @return the corresponding value, or null if no value was present for the given key. 59 | * @see java.util.Map#get(Object) 60 | */ 61 | 62 | V get( Object key ); 63 | 64 | /** Returns true if this function contains a mapping for the specified key. 65 | * 66 | *

Note that for some kind of functions (e.g., hashes) this method 67 | * will always return true. 68 | * 69 | * @param key the key. 70 | * @return true if this function associates a value to key. 71 | * @see java.util.Map#containsKey(Object) 72 | */ 73 | 74 | boolean containsKey( Object key ); 75 | 76 | /** Removes this key and the associated value from this function if it is present (optional operation). 77 | * 78 | * @param key 79 | * @return the old value, or null if no value was present for the given key. 80 | * @see java.util.Map#remove(Object) 81 | */ 82 | 83 | V remove( Object key ); 84 | 85 | /** Returns the intended number of keys in this function, or -1 if no such number exists. 86 | * 87 | *

Most function implementations will have some knowledge of the intended number of keys 88 | * in their domain. In some cases, however, this might not be possible. 89 | * 90 | * @return the intended number of keys in this function, or -1 if that number is not available. 91 | */ 92 | int size(); 93 | 94 | /** Removes all associations from this function (optional operation). 95 | * 96 | * @see java.util.Map#clear() 97 | */ 98 | 99 | void clear(); 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/HashCommon.java: -------------------------------------------------------------------------------- 1 | package org.codelibs.jhighlight.fastutil; 2 | 3 | /* 4 | * Copyright (C) 2002-2014 Sebastiano Vigna 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | 20 | /** Common code for all hash-based classes. */ 21 | 22 | public class HashCommon { 23 | 24 | protected HashCommon() {}; 25 | 26 | /** This reference is used to fill keys and values of removed entries (if 27 | they are objects). null cannot be used as it would confuse the 28 | search algorithm in the presence of an actual null key. */ 29 | public static final Object REMOVED = new Object(); 30 | 31 | /** Avalanches the bits of an integer by applying the finalisation step of MurmurHash3. 32 | * 33 | *

This method implements the finalisation step of Austin Appleby's MurmurHash3. 34 | * Its purpose is to avalanche the bits of the argument to within 0.25% bias. It is used, among other things, to scramble quickly (but deeply) the hash 35 | * values returned by {@link Object#hashCode()}. 36 | * 37 | * @param x an integer. 38 | * @return a hash value with good avalanching properties. 39 | */ 40 | public final static int murmurHash3( int x ) { 41 | x ^= x >>> 16; 42 | x *= 0x85ebca6b; 43 | x ^= x >>> 13; 44 | x *= 0xc2b2ae35; 45 | x ^= x >>> 16; 46 | return x; 47 | } 48 | 49 | /** Avalanches the bits of a long integer by applying the finalisation step of MurmurHash3. 50 | * 51 | *

This method implements the finalisation step of Austin Appleby's MurmurHash3. 52 | * Its purpose is to avalanche the bits of the argument to within 0.25% bias. It is used, among other things, to scramble quickly (but deeply) the hash 53 | * values returned by {@link Object#hashCode()}. 54 | * 55 | *

Incidentally, iterating this method starting from a nonzero value will generate a sequence of nonzero 56 | * values that passes strongest statistical tests. 57 | * 58 | * @param x a long integer. 59 | * @return a hash value with good avalanching properties. 60 | */ 61 | public final static long murmurHash3( long x ) { 62 | x ^= x >>> 33; 63 | x *= 0xff51afd7ed558ccdL; 64 | x ^= x >>> 33; 65 | x *= 0xc4ceb9fe1a85ec53L; 66 | x ^= x >>> 33; 67 | 68 | return x; 69 | } 70 | 71 | /** Returns the hash code that would be returned by {@link Float#hashCode()}. 72 | * 73 | * @return the same code as {@link Float#hashCode() new Float(f).hashCode()}. 74 | */ 75 | 76 | final public static int float2int( final float f ) { 77 | return Float.floatToRawIntBits( f ); 78 | } 79 | 80 | /** Returns the hash code that would be returned by {@link Double#hashCode()}. 81 | * 82 | * @return the same code as {@link Double#hashCode() new Double(f).hashCode()}. 83 | */ 84 | 85 | final public static int double2int( final double d ) { 86 | final long l = Double.doubleToRawLongBits( d ); 87 | return (int)( l ^ ( l >>> 32 ) ); 88 | } 89 | 90 | /** Returns the hash code that would be returned by {@link Long#hashCode()}. 91 | * 92 | * @return the same code as {@link Long#hashCode() new Long(f).hashCode()}. 93 | */ 94 | final public static int long2int( final long l ) { 95 | return (int)( l ^ ( l >>> 32 ) ); 96 | } 97 | 98 | /** Return the least power of two greater than or equal to the specified value. 99 | * 100 | *

Note that this function will return 1 when the argument is 0. 101 | * 102 | * @param x an integer smaller than or equal to 230. 103 | * @return the least power of two greater than or equal to the specified value. 104 | */ 105 | public static int nextPowerOfTwo( int x ) { 106 | if ( x == 0 ) return 1; 107 | x--; 108 | x |= x >> 1; 109 | x |= x >> 2; 110 | x |= x >> 4; 111 | x |= x >> 8; 112 | return ( x | x >> 16 ) + 1; 113 | } 114 | 115 | /** Return the least power of two greater than or equal to the specified value. 116 | * 117 | *

Note that this function will return 1 when the argument is 0. 118 | * 119 | * @param x a long integer smaller than or equal to 262. 120 | * @return the least power of two greater than or equal to the specified value. 121 | */ 122 | public static long nextPowerOfTwo( long x ) { 123 | if ( x == 0 ) return 1; 124 | x--; 125 | x |= x >> 1; 126 | x |= x >> 2; 127 | x |= x >> 4; 128 | x |= x >> 8; 129 | x |= x >> 16; 130 | return ( x | x >> 32 ) + 1; 131 | } 132 | 133 | 134 | /** Returns the maximum number of entries that can be filled before rehashing. 135 | * 136 | * @param n the size of the backing array. 137 | * @param f the load factor. 138 | * @return the maximum number of entries before rehashing. 139 | */ 140 | public static int maxFill( final int n, final float f ) { 141 | /* We must guarantee that there is always at least 142 | * one free entry (even with pathological load factors). */ 143 | return Math.min( (int)Math.ceil( n * f ), n - 1 ); 144 | } 145 | 146 | /** Returns the maximum number of entries that can be filled before rehashing. 147 | * 148 | * @param n the size of the backing array. 149 | * @param f the load factor. 150 | * @return the maximum number of entries before rehashing. 151 | */ 152 | public static long maxFill( final long n, final float f ) { 153 | /* We must guarantee that there is always at least 154 | * one free entry (even with pathological load factors). */ 155 | return Math.min( (long)Math.ceil( n * f ), n - 1 ); 156 | } 157 | 158 | /** Returns the least power of two smaller than or equal to 230 and larger than or equal to Math.ceil( expected / f ). 159 | * 160 | * @param expected the expected number of elements in a hash table. 161 | * @param f the load factor. 162 | * @return the minimum possible size for a backing array. 163 | * @throws IllegalArgumentException if the necessary size is larger than 230. 164 | */ 165 | public static int arraySize( final int expected, final float f ) { 166 | final long s = Math.max( 2, nextPowerOfTwo( (long)Math.ceil( expected / f ) ) ); 167 | if ( s > (1 << 30) ) throw new IllegalArgumentException( "Too large (" + expected + " expected elements with load factor " + f + ")" ); 168 | return (int)s; 169 | } 170 | 171 | /** Returns the least power of two larger than or equal to Math.ceil( expected / f ). 172 | * 173 | * @param expected the expected number of elements in a hash table. 174 | * @param f the load factor. 175 | * @return the minimum possible size for a backing big array. 176 | */ 177 | public static long bigArraySize( final long expected, final float f ) { 178 | return nextPowerOfTwo( (long)Math.ceil( expected / f ) ); 179 | } 180 | } -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/Stack.java: -------------------------------------------------------------------------------- 1 | package org.codelibs.jhighlight.fastutil; 2 | 3 | /* 4 | * Copyright (C) 2002-2014 Sebastiano Vigna 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | 20 | import java.util.NoSuchElementException; 21 | 22 | /** A stack. 23 | * 24 | *

A stack must provide the classical {@link #push(Object)} and 25 | * {@link #pop()} operations, but may be also peekable 26 | * to some extent: it may provide just the {@link #top()} function, 27 | * or even a more powerful {@link #peek(int)} method that provides 28 | * access to all elements on the stack (indexed from the top, which 29 | * has index 0). 30 | */ 31 | 32 | public interface Stack { 33 | 34 | /** Pushes the given object on the stack. 35 | * 36 | * @param o the object that will become the new top of the stack. 37 | */ 38 | 39 | void push( K o ); 40 | 41 | /** Pops the top off the stack. 42 | * 43 | * @return the top of the stack. 44 | * @throws NoSuchElementException if the stack is empty. 45 | */ 46 | 47 | K pop(); 48 | 49 | /** Checks whether the stack is empty. 50 | * 51 | * @return true if the stack is empty. 52 | */ 53 | 54 | boolean isEmpty(); 55 | 56 | /** Peeks at the top of the stack (optional operation). 57 | * 58 | * @return the top of the stack. 59 | * @throws NoSuchElementException if the stack is empty. 60 | */ 61 | 62 | K top(); 63 | 64 | /** Peeks at an element on the stack (optional operation). 65 | * 66 | * @return the i-th element on the stack; 0 represents the top. 67 | * @throws IndexOutOfBoundsException if the designated element does not exist.. 68 | */ 69 | 70 | K peek( int i ); 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/Swapper.java: -------------------------------------------------------------------------------- 1 | package org.codelibs.jhighlight.fastutil; 2 | 3 | /* 4 | * Copyright (C) 2010-2014 Sebastiano Vigna 5 | * 6 | * Licensed under the Apache License, Version 2.0 (the "License"); 7 | * you may not use this file except in compliance with the License. 8 | * You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, software 13 | * distributed under the License is distributed on an "AS IS" BASIS, 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | * See the License for the specific language governing permissions and 16 | * limitations under the License. 17 | */ 18 | 19 | /** An object that can swap elements whose position is specified by integers 20 | * 21 | * @see Arrays#quickSort(int, int, org.codelibs.jhighlight.fastutil.ints.IntComparator, Swapper) 22 | */ 23 | 24 | public interface Swapper { 25 | /** Swaps the data at the given positions. 26 | * 27 | * @param a the first position to swap. 28 | * @param b the second position to swap. 29 | */ 30 | void swap( int a, int b ); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/booleans/BooleanComparator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.booleans; 39 | import java.util.Comparator; 40 | /** A type-specific {@link Comparator}; provides methods to compare two primitive types both as objects 41 | * and as primitive types. 42 | * 43 | *

Note that fastutil provides a corresponding abstract class that 44 | * can be used to implement this interface just by specifying the type-specific 45 | * comparator. 46 | * 47 | * @see Comparator 48 | */ 49 | public interface BooleanComparator extends Comparator { 50 | /** Compares the given primitive types. 51 | * 52 | * @see java.util.Comparator 53 | * @return A positive integer, zero, or a negative integer if the first 54 | * argument is greater than, equal to, or smaller than, respectively, the 55 | * second one. 56 | */ 57 | public int compare( boolean k1, boolean k2 ); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/AbstractChar2ObjectFunction.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | /** An abstract class providing basic methods for functions implementing a type-specific interface. 40 | * 41 | *

Optional operations just throw an {@link 42 | * UnsupportedOperationException}. Generic versions of accessors delegate to 43 | * the corresponding type-specific counterparts following the interface rules 44 | * (they take care of returning null on a missing key). 45 | * 46 | *

This class handles directly a default return 47 | * value (including {@linkplain #defaultReturnValue() methods to access 48 | * it}). Instances of classes inheriting from this class have just to return 49 | * defRetValue to denote lack of a key in type-specific methods. The value 50 | * is serialized. 51 | * 52 | *

Implementing subclasses have just to provide type-specific get(), 53 | * type-specific containsKey(), and size() methods. 54 | * 55 | */ 56 | public abstract class AbstractChar2ObjectFunction implements Char2ObjectFunction , java.io.Serializable { 57 | private static final long serialVersionUID = -4940583368468432370L; 58 | protected AbstractChar2ObjectFunction() {} 59 | /** 60 | * The default return value for get(), put() and 61 | * remove(). 62 | */ 63 | protected V defRetValue; 64 | public void defaultReturnValue( final V rv ) { 65 | defRetValue = rv; 66 | } 67 | public V defaultReturnValue() { 68 | return defRetValue; 69 | } 70 | public V put( char key, V value ) { 71 | throw new UnsupportedOperationException(); 72 | } 73 | public V remove( char key ) { 74 | throw new UnsupportedOperationException(); 75 | } 76 | public void clear() { 77 | throw new UnsupportedOperationException(); 78 | } 79 | public boolean containsKey( final Object ok ) { 80 | return containsKey( ((((Character)(ok)).charValue())) ); 81 | } 82 | /** Delegates to the corresponding type-specific method, taking care of returning null on a missing key. 83 | * 84 | *

This method must check whether the provided key is in the map using containsKey(). Thus, 85 | * it probes the map twice. Implementors of subclasses should override it with a more efficient method. 86 | */ 87 | public V get( final Object ok ) { 88 | final char k = ((((Character)(ok)).charValue())); 89 | return containsKey( k ) ? (get( k )) : null; 90 | } 91 | /** Delegates to the corresponding type-specific method, taking care of returning null on a missing key. 92 | * 93 | *

This method must check whether the provided key is in the map using containsKey(). Thus, 94 | * it probes the map twice. Implementors of subclasses should override it with a more efficient method. 95 | */ 96 | public V put( final Character ok, final V ov ) { 97 | final char k = ((ok).charValue()); 98 | final boolean containsKey = containsKey( k ); 99 | final V v = put( k, (ov) ); 100 | return containsKey ? (v) : null; 101 | } 102 | /** Delegates to the corresponding type-specific method, taking care of returning null on a missing key. 103 | * 104 | *

This method must check whether the provided key is in the map using containsKey(). Thus, 105 | * it probes the map twice. Implementors of subclasses should override it with a more efficient method. 106 | */ 107 | public V remove( final Object ok ) { 108 | final char k = ((((Character)(ok)).charValue())); 109 | final boolean containsKey = containsKey( k ); 110 | final V v = remove( k ); 111 | return containsKey ? (v) : null; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/AbstractCharBidirectionalIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | /** An abstract class facilitating the creation of type-specific {@linkplain org.codelibs.jhighlight.fastutil.BidirectionalIterator bidirectional iterators}. 40 | * 41 | *

To create a type-specific bidirectional iterator, besides what is needed 42 | * for an iterator you need both a method returning the previous element as 43 | * primitive type and a method returning the previous element as an 44 | * object. However, if you inherit from this class you need just one (anyone). 45 | * 46 | *

This class implements also a trivial version of {@link #back(int)} that 47 | * uses type-specific methods. 48 | */ 49 | public abstract class AbstractCharBidirectionalIterator extends AbstractCharIterator implements CharBidirectionalIterator { 50 | protected AbstractCharBidirectionalIterator() {} 51 | /** Delegates to the corresponding generic method. */ 52 | public char previousChar() { return previous().charValue(); } 53 | /** Delegates to the corresponding type-specific method. */ 54 | public Character previous() { return Character.valueOf( previousChar() ); } 55 | /** This method just iterates the type-specific version of {@link #previous()} for 56 | * at most n times, stopping if {@link 57 | * #hasPrevious()} becomes false. */ 58 | public int back( final int n ) { 59 | int i = n; 60 | while( i-- != 0 && hasPrevious() ) previousChar(); 61 | return n - i - 1; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/AbstractCharCollection.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.AbstractCollection; 40 | import java.util.Collection; 41 | import java.util.Iterator; 42 | /** An abstract class providing basic methods for collections implementing a type-specific interface. 43 | * 44 | *

In particular, this class provide {@link #iterator()}, add(), {@link #remove(Object)} and 45 | * {@link #contains(Object)} methods that just call the type-specific counterpart. 46 | */ 47 | public abstract class AbstractCharCollection extends AbstractCollection implements CharCollection { 48 | protected AbstractCharCollection() {} 49 | public char[] toArray( char a[] ) { 50 | return toCharArray( a ); 51 | } 52 | public char[] toCharArray() { 53 | return toCharArray( null ); 54 | } 55 | public char[] toCharArray( char a[] ) { 56 | if ( a == null || a.length < size() ) a = new char[ size() ]; 57 | CharIterators.unwrap( iterator(), a ); 58 | return a; 59 | } 60 | /** Adds all elements of the given type-specific collection to this collection. 61 | * 62 | * @param c a type-specific collection. 63 | * @return true if this collection changed as a result of the call. 64 | */ 65 | public boolean addAll( CharCollection c ) { 66 | boolean retVal = false; 67 | final CharIterator i = c.iterator(); 68 | int n = c.size(); 69 | while( n-- != 0 ) if ( add( i.nextChar() ) ) retVal = true; 70 | return retVal; 71 | } 72 | /** Checks whether this collection contains all elements from the given type-specific collection. 73 | * 74 | * @param c a type-specific collection. 75 | * @return true if this collection contains all elements of the argument. 76 | */ 77 | public boolean containsAll( CharCollection c ) { 78 | final CharIterator i = c.iterator(); 79 | int n = c.size(); 80 | while( n-- != 0 ) if ( ! contains( i.nextChar() ) ) return false; 81 | return true; 82 | } 83 | /** Retains in this collection only elements from the given type-specific collection. 84 | * 85 | * @param c a type-specific collection. 86 | * @return true if this collection changed as a result of the call. 87 | */ 88 | public boolean retainAll( CharCollection c ) { 89 | boolean retVal = false; 90 | int n = size(); 91 | final CharIterator i = iterator(); 92 | while( n-- != 0 ) { 93 | if ( ! c.contains( i.nextChar() ) ) { 94 | i.remove(); 95 | retVal = true; 96 | } 97 | } 98 | return retVal; 99 | } 100 | /** Remove from this collection all elements in the given type-specific collection. 101 | * 102 | * @param c a type-specific collection. 103 | * @return true if this collection changed as a result of the call. 104 | */ 105 | public boolean removeAll( CharCollection c ) { 106 | boolean retVal = false; 107 | int n = c.size(); 108 | final CharIterator i = c.iterator(); 109 | while( n-- != 0 ) if ( rem( i.nextChar() ) ) retVal = true; 110 | return retVal; 111 | } 112 | public Object[] toArray() { 113 | final Object[] a = new Object[ size() ]; 114 | org.codelibs.jhighlight.fastutil.objects.ObjectIterators.unwrap( iterator(), a ); 115 | return a; 116 | } 117 | @SuppressWarnings("unchecked") 118 | public T[] toArray( T[] a ) { 119 | if ( a.length < size() ) a = (T[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size() ); 120 | org.codelibs.jhighlight.fastutil.objects.ObjectIterators.unwrap( iterator(), a ); 121 | return a; 122 | } 123 | /** Adds all elements of the given collection to this collection. 124 | * 125 | * @param c a collection. 126 | * @return true if this collection changed as a result of the call. 127 | */ 128 | public boolean addAll( Collection c ) { 129 | boolean retVal = false; 130 | final Iterator i = c.iterator(); 131 | int n = c.size(); 132 | while( n-- != 0 ) if ( add( i.next() ) ) retVal = true; 133 | return retVal; 134 | } 135 | public boolean add( char k ) { 136 | throw new UnsupportedOperationException(); 137 | } 138 | /** Delegates to the new covariantly stronger generic method. */ 139 | @Deprecated 140 | public CharIterator charIterator() { 141 | return iterator(); 142 | } 143 | public abstract CharIterator iterator(); 144 | /** Delegates to the type-specific rem() method. */ 145 | public boolean remove( Object ok ) { 146 | return rem( ((((Character)(ok)).charValue())) ); 147 | } 148 | /** Delegates to the corresponding type-specific method. */ 149 | public boolean add( final Character o ) { 150 | return add( o.charValue() ); 151 | } 152 | /** Delegates to the corresponding type-specific method. */ 153 | public boolean rem( final Object o ) { 154 | return rem( ((((Character)(o)).charValue())) ); 155 | } 156 | /** Delegates to the corresponding type-specific method. */ 157 | public boolean contains( final Object o ) { 158 | return contains( ((((Character)(o)).charValue())) ); 159 | } 160 | public boolean contains( final char k ) { 161 | final CharIterator iterator = iterator(); 162 | while ( iterator.hasNext() ) if ( k == iterator.nextChar() ) return true; 163 | return false; 164 | } 165 | public boolean rem( final char k ) { 166 | final CharIterator iterator = iterator(); 167 | while ( iterator.hasNext() ) 168 | if ( k == iterator.nextChar() ) { 169 | iterator.remove(); 170 | return true; 171 | } 172 | return false; 173 | } 174 | /** Checks whether this collection contains all elements from the given collection. 175 | * 176 | * @param c a collection. 177 | * @return true if this collection contains all elements of the argument. 178 | */ 179 | public boolean containsAll( Collection c ) { 180 | int n = c.size(); 181 | final Iterator i = c.iterator(); 182 | while( n-- != 0 ) if ( ! contains( i.next() ) ) return false; 183 | return true; 184 | } 185 | /** Retains in this collection only elements from the given collection. 186 | * 187 | * @param c a collection. 188 | * @return true if this collection changed as a result of the call. 189 | */ 190 | public boolean retainAll( Collection c ) { 191 | boolean retVal = false; 192 | int n = size(); 193 | final Iterator i = iterator(); 194 | while( n-- != 0 ) { 195 | if ( ! c.contains( i.next() ) ) { 196 | i.remove(); 197 | retVal = true; 198 | } 199 | } 200 | return retVal; 201 | } 202 | /** Remove from this collection all elements in the given collection. 203 | * If the collection is an instance of this class, it uses faster iterators. 204 | * 205 | * @param c a collection. 206 | * @return true if this collection changed as a result of the call. 207 | */ 208 | public boolean removeAll( Collection c ) { 209 | boolean retVal = false; 210 | int n = c.size(); 211 | final Iterator i = c.iterator(); 212 | while( n-- != 0 ) if ( remove( i.next() ) ) retVal = true; 213 | return retVal; 214 | } 215 | public boolean isEmpty() { 216 | return size() == 0; 217 | } 218 | public String toString() { 219 | final StringBuilder s = new StringBuilder(); 220 | final CharIterator i = iterator(); 221 | int n = size(); 222 | char k; 223 | boolean first = true; 224 | s.append("{"); 225 | while(n-- != 0) { 226 | if (first) first = false; 227 | else s.append(", "); 228 | k = i.nextChar(); 229 | s.append(String.valueOf(k)); 230 | } 231 | s.append("}"); 232 | return s.toString(); 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/AbstractCharIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | /** An abstract class facilitating the creation of type-specific iterators. 40 | * 41 | *

To create a type-specific iterator you need both a method returning the 42 | * next element as primitive type and a method returning the next element as an 43 | * object. However, if you inherit from this class you need just one (anyone). 44 | * 45 | *

This class implements also a trivial version of {@link #skip(int)} that uses 46 | * type-specific methods; moreover, {@link #remove()} will throw an {@link 47 | * UnsupportedOperationException}. 48 | * 49 | * @see java.util.Iterator 50 | */ 51 | public abstract class AbstractCharIterator implements CharIterator { 52 | protected AbstractCharIterator() {} 53 | /** Delegates to the corresponding generic method. */ 54 | public char nextChar() { return next().charValue(); } 55 | /** Delegates to the corresponding type-specific method. */ 56 | public Character next() { return Character.valueOf( nextChar() ); } 57 | /** This method just throws an {@link UnsupportedOperationException}. */ 58 | public void remove() { throw new UnsupportedOperationException(); } 59 | /** This method just iterates the type-specific version of {@link #next()} for at most 60 | * n times, stopping if {@link #hasNext()} becomes false.*/ 61 | public int skip( final int n ) { 62 | int i = n; 63 | while( i-- != 0 && hasNext() ) nextChar(); 64 | return n - i - 1; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/AbstractCharListIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | /** An abstract class facilitating the creation of type-specific {@linkplain java.util.ListIterator list iterators}. 40 | * 41 | *

This class provides trivial type-specific implementations of {@link 42 | * java.util.ListIterator#set(Object) set()} and {@link java.util.ListIterator#add(Object) add()} which 43 | * throw an {@link UnsupportedOperationException}. For primitive types, it also 44 | * provides a trivial implementation of {@link java.util.ListIterator#set(Object) set()} and {@link 45 | * java.util.ListIterator#add(Object) add()} that just invokes the type-specific one. 46 | * 47 | * 48 | * @see java.util.ListIterator 49 | */ 50 | public abstract class AbstractCharListIterator extends AbstractCharBidirectionalIterator implements CharListIterator { 51 | protected AbstractCharListIterator() {} 52 | /** Delegates to the corresponding type-specific method. */ 53 | public void set( Character ok ) { set( ok.charValue() ); } 54 | /** Delegates to the corresponding type-specific method. */ 55 | public void add( Character ok ) { add( ok.charValue() ); } 56 | /** This method just throws an {@link UnsupportedOperationException}. */ 57 | public void set( char k ) { throw new UnsupportedOperationException(); } 58 | /** This method just throws an {@link UnsupportedOperationException}. */ 59 | public void add( char k ) { throw new UnsupportedOperationException(); } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/AbstractCharSet.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.Set; 40 | /** An abstract class providing basic methods for sets implementing a type-specific interface. */ 41 | public abstract class AbstractCharSet extends AbstractCharCollection implements Cloneable, CharSet { 42 | protected AbstractCharSet() {} 43 | public abstract CharIterator iterator(); 44 | public boolean equals( final Object o ) { 45 | if ( o == this ) return true; 46 | if ( !( o instanceof Set ) ) return false; 47 | Set s = (Set) o; 48 | if ( s.size() != size() ) return false; 49 | return containsAll(s); 50 | } 51 | /** Returns a hash code for this set. 52 | * 53 | * The hash code of a set is computed by summing the hash codes of 54 | * its elements. 55 | * 56 | * @return a hash code for this set. 57 | */ 58 | public int hashCode() { 59 | int h = 0, n = size(); 60 | CharIterator i = iterator(); 61 | char k; 62 | while( n-- != 0 ) { 63 | k = i.nextChar(); // We need k because KEY2JAVAHASH() is a macro with repeated evaluation. 64 | h += (k); 65 | } 66 | return h; 67 | } 68 | public boolean remove( char k ) { 69 | throw new UnsupportedOperationException(); 70 | } 71 | /** Delegates to remove(). 72 | * 73 | * @param k the element to be removed. 74 | * @return true if the set was modified. 75 | */ 76 | public boolean rem( char k ) { 77 | return remove( k ); 78 | } 79 | /** Delegates to the corresponding type-specific method. */ 80 | public boolean remove( final Object o ) { 81 | return remove( ((((Character)(o)).charValue())) ); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/Char2ObjectFunction.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* Generic definitions */ 4 | 5 | 6 | 7 | 8 | /* Assertions (useful to generate conditional code) */ 9 | /* Current type and class (and size, if applicable) */ 10 | /* Value methods */ 11 | /* Interfaces (keys) */ 12 | /* Interfaces (values) */ 13 | /* Abstract implementations (keys) */ 14 | /* Abstract implementations (values) */ 15 | /* Static containers (keys) */ 16 | /* Static containers (values) */ 17 | /* Implementations */ 18 | /* Synchronized wrappers */ 19 | /* Unmodifiable wrappers */ 20 | /* Other wrappers */ 21 | /* Methods (keys) */ 22 | /* Methods (values) */ 23 | /* Methods (keys/values) */ 24 | /* Methods that have special names depending on keys (but the special names depend on values) */ 25 | /* Equality */ 26 | /* Object/Reference-only definitions (keys) */ 27 | /* Primitive-type-only definitions (keys) */ 28 | /* Object/Reference-only definitions (values) */ 29 | /* 30 | * Copyright (C) 2002-2010 Sebastiano Vigna 31 | * 32 | * Licensed under the Apache License, Version 2.0 (the "License"); 33 | * you may not use this file except in compliance with the License. 34 | * You may obtain a copy of the License at 35 | * 36 | * http://www.apache.org/licenses/LICENSE-2.0 37 | * 38 | * Unless required by applicable law or agreed to in writing, software 39 | * distributed under the License is distributed on an "AS IS" BASIS, 40 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 41 | * See the License for the specific language governing permissions and 42 | * limitations under the License. 43 | */ 44 | package org.codelibs.jhighlight.fastutil.chars; 45 | import org.codelibs.jhighlight.fastutil.Function; 46 | /** A type-specific {@link Function}; provides some additional methods that use polymorphism to avoid (un)boxing. 47 | * 48 | *

Type-specific versions of get(), put() and 49 | * remove() cannot rely on null to denote absence of 50 | * a key. Rather, they return a {@linkplain #defaultReturnValue() default 51 | * return value}, which is set to 0 cast to the return type (false 52 | * for booleans) at creation, but can be changed using the 53 | * defaultReturnValue() method. 54 | * 55 | *

For uniformity reasons, even maps returning objects implement the default 56 | * return value (of course, in this case the default return value is 57 | * initialized to null). 58 | * 59 | *

Warning: to fall in line as much as possible with the 60 | * {@linkplain java.util.Map standard map interface}, it is strongly suggested 61 | * that standard versions of get(), put() and 62 | * remove() for maps with primitive-type values return 63 | * null to denote missing keys rather than wrap the default 64 | * return value in an object (of course, for maps with object keys and values 65 | * this is not possible, as there is no type-specific version). 66 | * 67 | * @see Function 68 | */ 69 | public interface Char2ObjectFunction extends Function { 70 | /** Adds a pair to the map. 71 | * 72 | * @param key the key. 73 | * @param value the value. 74 | * @return the old value, or the {@linkplain #defaultReturnValue() default return value} if no value was present for the given key. 75 | * @see Function#put(Object,Object) 76 | */ 77 | V put( char key, V value ); 78 | /** Returns the value to which the given key is mapped. 79 | * 80 | * @param key the key. 81 | * @return the corresponding value, or the {@linkplain #defaultReturnValue() default return value} if no value was present for the given key. 82 | * @see Function#get(Object) 83 | */ 84 | V get( char key ); 85 | /** Removes the mapping with the given key. 86 | * @param key 87 | * @return the old value, or the {@linkplain #defaultReturnValue() default return value} if no value was present for the given key. 88 | * @see Function#remove(Object) 89 | */ 90 | V remove( char key ); 91 | /** 92 | * @see Function#containsKey(Object) 93 | */ 94 | boolean containsKey( char key ); 95 | /** Sets the default return value. 96 | * 97 | * This value must be returned by type-specific versions of 98 | * get(), put() and remove() to 99 | * denote that the map does not contain the specified key. It must be 100 | * 0/false/null by default. 101 | * 102 | * @param rv the new default return value. 103 | * @see #defaultReturnValue() 104 | */ 105 | void defaultReturnValue( V rv ); 106 | /** Gets the default return value. 107 | * 108 | * @return the current default return value. 109 | */ 110 | V defaultReturnValue(); 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/Char2ObjectMap.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.Map; 40 | 41 | import org.codelibs.jhighlight.fastutil.objects.ObjectCollection; 42 | import org.codelibs.jhighlight.fastutil.objects.ObjectIterator; 43 | import org.codelibs.jhighlight.fastutil.objects.ObjectSet; 44 | /** A type-specific {@link Map}; provides some additional methods that use polymorphism to avoid (un)boxing, and handling of a default return value. 45 | * 46 | *

Besides extending the corresponding type-specific {@linkplain org.codelibs.jhighlight.fastutil.Function function}, this interface strengthens {@link #entrySet()}, 47 | * {@link #keySet()} and {@link #values()}. Maps returning entry sets of type {@link FastEntrySet} support also fast iteration. 48 | * 49 | *

A submap or subset may or may not have an 50 | * independent default return value (which however must be initialized to the 51 | * default return value of the originator). 52 | * 53 | * @see Map 54 | */ 55 | public interface Char2ObjectMap extends Char2ObjectFunction , Map { 56 | /** An entry set providing fast iteration. 57 | * 58 | *

In some cases (e.g., hash-based classes) iteration over an entry set requires the creation 59 | * of a large number of {@link java.util.Map.Entry} objects. Some fastutil 60 | * maps might return {@linkplain #entrySet() entry set} objects of type FastEntrySet: in this case, {@link #fastIterator() fastIterator()} 61 | * will return an iterator that is guaranteed not to create a large number of objects, possibly 62 | * by returning always the same entry (of course, mutated). 63 | */ 64 | public interface FastEntrySet extends ObjectSet > { 65 | /** Returns a fast iterator over this entry set; the iterator might return always the same entry object, suitably mutated. 66 | * 67 | * @return a fast iterator over this entry set; the iterator might return always the same {@link java.util.Map.Entry} object, suitably mutated. 68 | */ 69 | public ObjectIterator > fastIterator(); 70 | } 71 | /** Returns a set view of the mappings contained in this map. 72 | *

Note that this specification strengthens the one given in {@link Map#entrySet()}. 73 | * 74 | * @return a set view of the mappings contained in this map. 75 | * @see Map#entrySet() 76 | */ 77 | ObjectSet> entrySet(); 78 | /** Returns a type-specific set view of the mappings contained in this map. 79 | * 80 | *

This method is necessary because there is no inheritance along 81 | * type parameters: it is thus impossible to strengthen {@link #entrySet()} 82 | * so that it returns an {@link org.codelibs.jhighlight.fastutil.objects.ObjectSet} 83 | * of objects of type {@link java.util.Map.Entry} (the latter makes it possible to 84 | * access keys and values with type-specific methods). 85 | * 86 | * @return a type-specific set view of the mappings contained in this map. 87 | * @see #entrySet() 88 | */ 89 | ObjectSet > char2ObjectEntrySet(); 90 | /** Returns a set view of the keys contained in this map. 91 | *

Note that this specification strengthens the one given in {@link Map#keySet()}. 92 | * 93 | * @return a set view of the keys contained in this map. 94 | * @see Map#keySet() 95 | */ 96 | CharSet keySet(); 97 | /** Returns a set view of the values contained in this map. 98 | *

Note that this specification strengthens the one given in {@link Map#values()}. 99 | * 100 | * @return a set view of the values contained in this map. 101 | * @see Map#values() 102 | */ 103 | ObjectCollection values(); 104 | /** A type-specific {@link java.util.Map.Entry}; provides some additional methods 105 | * that use polymorphism to avoid (un)boxing. 106 | * 107 | * @see java.util.Map.Entry 108 | */ 109 | interface Entry extends Map.Entry { 110 | /** 111 | * @see java.util.Map.Entry#getKey() 112 | */ 113 | char getCharKey(); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharBidirectionalIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import org.codelibs.jhighlight.fastutil.BidirectionalIterator; 40 | import org.codelibs.jhighlight.fastutil.objects.ObjectBidirectionalIterator; 41 | /** A type-specific bidirectional iterator; provides an additional method to avoid (un)boxing, 42 | * and the possibility to skip elements backwards. 43 | * 44 | * @see BidirectionalIterator 45 | */ 46 | public interface CharBidirectionalIterator extends CharIterator , ObjectBidirectionalIterator { 47 | /** 48 | * Returns the previous element as a primitive type. 49 | * 50 | * @return the previous element in the iteration. 51 | * @see java.util.ListIterator#previous() 52 | */ 53 | char previousChar(); 54 | /** Moves back for the given number of elements. 55 | * 56 | *

The effect of this call is exactly the same as that of 57 | * calling {@link #previous()} for n times (possibly stopping 58 | * if {@link #hasPrevious()} becomes false). 59 | * 60 | * @param n the number of elements to skip back. 61 | * @return the number of elements actually skipped. 62 | * @see java.util.Iterator#next() 63 | */ 64 | int back( int n ); 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharCollection.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.Collection; 40 | /** A type-specific {@link Collection}; provides some additional methods 41 | * that use polymorphism to avoid (un)boxing. 42 | * 43 | *

Additionally, this class defines strengthens (again) {@link #iterator()} and defines 44 | * a slightly different semantics for {@link #toArray(Object[])}. 45 | * 46 | * @see Collection 47 | */ 48 | public interface CharCollection extends Collection, CharIterable { 49 | /** Returns a type-specific iterator on the elements of this collection. 50 | * 51 | *

Note that this specification strengthens the one given in 52 | * {@link java.lang.Iterable#iterator()}, which was already 53 | * strengthened in the corresponding type-specific class, 54 | * but was weakened by the fact that this interface extends {@link Collection}. 55 | * 56 | * @return a type-specific iterator on the elements of this collection. 57 | */ 58 | CharIterator iterator(); 59 | /** Returns a type-specific iterator on this elements of this collection. 60 | * 61 | * @see #iterator() 62 | * @deprecated As of fastutil 5, replaced by {@link #iterator()}. 63 | */ 64 | @Deprecated 65 | CharIterator charIterator(); 66 | /** Returns an containing the items of this collection; 67 | * the runtime type of the returned array is that of the specified array. 68 | * 69 | *

Warning: Note that, contrarily to {@link Collection#toArray(Object[])}, this 70 | * methods just writes all elements of this collection: no special 71 | * value will be added after the last one. 72 | * 73 | * @param a if this array is big enough, it will be used to store this collection. 74 | * @return a primitive type array containing the items of this collection. 75 | * @see Collection#toArray(Object[]) 76 | */ 77 | T[] toArray(T[] a); 78 | /** 79 | * @see Collection#contains(Object) 80 | */ 81 | boolean contains( char key ); 82 | /** Returns a primitive type array containing the items of this collection. 83 | * @return a primitive type array containing the items of this collection. 84 | * @see Collection#toArray() 85 | */ 86 | char[] toCharArray(); 87 | /** Returns a primitive type array containing the items of this collection. 88 | * 89 | *

Note that, contrarily to {@link Collection#toArray(Object[])}, this 90 | * methods just writes all elements of this collection: no special 91 | * value will be added after the last one. 92 | * 93 | * @param a if this array is big enough, it will be used to store this collection. 94 | * @return a primitive type array containing the items of this collection. 95 | * @see Collection#toArray(Object[]) 96 | */ 97 | char[] toCharArray( char a[] ); 98 | /** Returns a primitive type array containing the items of this collection. 99 | * 100 | *

Note that, contrarily to {@link Collection#toArray(Object[])}, this 101 | * methods just writes all elements of this collection: no special 102 | * value will be added after the last one. 103 | * 104 | * @param a if this array is big enough, it will be used to store this collection. 105 | * @return a primitive type array containing the items of this collection. 106 | * @see Collection#toArray(Object[]) 107 | */ 108 | char[] toArray( char a[] ); 109 | /** 110 | * @see Collection#add(Object) 111 | */ 112 | boolean add( char key ); 113 | /** Note that this method should be called {@link java.util.Collection#remove(Object) remove()}, but the clash 114 | * with the similarly named index-based method in the {@link java.util.List} interface 115 | * forces us to use a distinguished name. For simplicity, the set interfaces reinstates 116 | * remove(). 117 | * 118 | * @see Collection#remove(Object) 119 | */ 120 | boolean rem( char key ); 121 | /** 122 | * @see Collection#addAll(Collection) 123 | */ 124 | boolean addAll( CharCollection c ); 125 | /** 126 | * @see Collection#containsAll(Collection) 127 | */ 128 | boolean containsAll( CharCollection c ); 129 | /** 130 | * @see Collection#removeAll(Collection) 131 | */ 132 | boolean removeAll( CharCollection c ); 133 | /** 134 | * @see Collection#retainAll(Collection) 135 | */ 136 | boolean retainAll( CharCollection c ); 137 | } 138 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharComparator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.Comparator; 40 | /** A type-specific {@link Comparator}; provides methods to compare two primitive types both as objects 41 | * and as primitive types. 42 | * 43 | *

Note that fastutil provides a corresponding abstract class that 44 | * can be used to implement this interface just by specifying the type-specific 45 | * comparator. 46 | * 47 | * @see Comparator 48 | */ 49 | public interface CharComparator extends Comparator { 50 | /** Compares the given primitive types. 51 | * 52 | * @see java.util.Comparator 53 | * @return A positive integer, zero, or a negative integer if the first 54 | * argument is greater than, equal to, or smaller than, respectively, the 55 | * second one. 56 | */ 57 | public int compare( char k1, char k2 ); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharIterable.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | 40 | /** A type-specific {@link Iterable} that strengthens that specification of {@link Iterable#iterator()}. 41 | * 42 | *

Warning: Java will let you write “colon” for statements with primitive-type 43 | * loop variables; however, what is (unfortunately) really happening is that at each iteration an 44 | * unboxing (and, in the case of fastutil type-specific data structures, a boxing) will be performed. Watch out. 45 | * 46 | * @see Iterable 47 | */ 48 | public interface CharIterable extends Iterable { 49 | /** Returns a type-specific iterator. 50 | * 51 | * Note that this specification strengthens the one given in {@link Iterable#iterator()}. 52 | * 53 | * @return a type-specific iterator. 54 | */ 55 | CharIterator iterator(); 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.Iterator; 40 | /** A type-specific {@link Iterator}; provides an additional method to avoid (un)boxing, and 41 | * the possibility to skip elements. 42 | * 43 | * @see Iterator 44 | */ 45 | public interface CharIterator extends Iterator { 46 | /** 47 | * Returns the next element as a primitive type. 48 | * 49 | * @return the next element in the iteration. 50 | * @see Iterator#next() 51 | */ 52 | char nextChar(); 53 | /** Skips the given number of elements. 54 | * 55 | *

The effect of this call is exactly the same as that of 56 | * calling {@link #next()} for n times (possibly stopping 57 | * if {@link #hasNext()} becomes false). 58 | * 59 | * @param n the number of elements to skip. 60 | * @return the number of elements actually skipped. 61 | * @see Iterator#next() 62 | */ 63 | int skip( int n ); 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharList.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.List; 40 | /** A type-specific {@link List}; provides some additional methods that use polymorphism to avoid (un)boxing. 41 | * 42 | *

Note that this type-specific interface extends {@link Comparable}: it is expected that implementing 43 | * classes perform a lexicographical comparison using the standard operator "less then" for primitive types, 44 | * and the usual {@link Comparable#compareTo(Object) compareTo()} method for objects. 45 | * 46 | *

Additionally, this interface strengthens {@link #listIterator()}, 47 | * {@link #listIterator(int)} and {@link #subList(int,int)}. 48 | * 49 | *

Besides polymorphic methods, this interfaces specifies methods to copy into an array or remove contiguous 50 | * sublists. Although the abstract implementation of this interface provides simple, one-by-one implementations 51 | * of these methods, it is expected that concrete implementation override them with optimized versions. 52 | * 53 | * @see List 54 | */ 55 | public interface CharList extends List, Comparable>, CharCollection { 56 | /** Returns a type-specific iterator on the elements of this list (in proper sequence). 57 | * 58 | * Note that this specification strengthens the one given in {@link List#iterator()}. 59 | * It would not be normally necessary, but {@link java.lang.Iterable#iterator()} is bizarrily re-specified 60 | * in {@link List}. 61 | * 62 | * @return an iterator on the elements of this list (in proper sequence). 63 | */ 64 | CharListIterator iterator(); 65 | /** Returns a type-specific list iterator on the list. 66 | * 67 | * @see #listIterator() 68 | * @deprecated As of fastutil 5, replaced by {@link #listIterator()}. 69 | */ 70 | @Deprecated 71 | CharListIterator charListIterator(); 72 | /** Returns a type-specific list iterator on the list starting at a given index. 73 | * 74 | * @see #listIterator(int) 75 | * @deprecated As of fastutil 5, replaced by {@link #listIterator(int)}. 76 | */ 77 | @Deprecated 78 | CharListIterator charListIterator( int index ); 79 | /** Returns a type-specific list iterator on the list. 80 | * 81 | * @see List#listIterator() 82 | */ 83 | CharListIterator listIterator(); 84 | /** Returns a type-specific list iterator on the list starting at a given index. 85 | * 86 | * @see List#listIterator(int) 87 | */ 88 | CharListIterator listIterator( int index ); 89 | /** Returns a type-specific view of the portion of this list from the index from, inclusive, to the index to, exclusive. 90 | * @see List#subList(int,int) 91 | * @deprecated As of fastutil 5, replaced by {@link #subList(int,int)}. 92 | */ 93 | @Deprecated 94 | CharList charSubList( int from, int to ); 95 | /** Returns a type-specific view of the portion of this list from the index from, inclusive, to the index to, exclusive. 96 | * 97 | *

Note that this specification strengthens the one given in {@link List#subList(int,int)}. 98 | * 99 | * @see List#subList(int,int) 100 | */ 101 | CharList subList(int from, int to); 102 | /** Sets the size of this list. 103 | * 104 | *

If the specified size is smaller than the current size, the last elements are 105 | * discarded. Otherwise, they are filled with 0/null/false. 106 | * 107 | * @param size the new size. 108 | */ 109 | void size( int size ); 110 | /** Copies (hopefully quickly) elements of this type-specific list into the given array. 111 | * 112 | * @param from the start index (inclusive). 113 | * @param a the destination array. 114 | * @param offset the offset into the destination array where to store the first element copied. 115 | * @param length the number of elements to be copied. 116 | */ 117 | void getElements( int from, char a[], int offset, int length ); 118 | /** Removes (hopefully quickly) elements of this type-specific list. 119 | * 120 | * @param from the start index (inclusive). 121 | * @param to the end index (exclusive). 122 | */ 123 | void removeElements( int from, int to ); 124 | /** Add (hopefully quickly) elements to this type-specific list. 125 | * 126 | * @param index the index at which to add elements. 127 | * @param a the array containing the elements. 128 | */ 129 | void addElements( int index, char a[] ); 130 | /** Add (hopefully quickly) elements to this type-specific list. 131 | * 132 | * @param index the index at which to add elements. 133 | * @param a the array containing the elements. 134 | * @param offset the offset of the first element to add. 135 | * @param length the number of elements to add. 136 | */ 137 | void addElements( int index, char a[], int offset, int length ); 138 | /** 139 | * @see List#add(Object) 140 | */ 141 | boolean add( char key ); 142 | /** 143 | * @see List#add(int,Object) 144 | */ 145 | void add( int index, char key ); 146 | /** 147 | * @see List#add(int,Object) 148 | */ 149 | boolean addAll( int index, CharCollection c ); 150 | /** 151 | * @see List#add(int,Object) 152 | */ 153 | boolean addAll( int index, CharList c ); 154 | /** 155 | * @see List#add(int,Object) 156 | */ 157 | boolean addAll( CharList c ); 158 | /** 159 | * @see List#get(int) 160 | */ 161 | char getChar( int index ); 162 | /** 163 | * @see List#indexOf(Object) 164 | */ 165 | int indexOf( char k ); 166 | /** 167 | * @see List#lastIndexOf(Object) 168 | */ 169 | int lastIndexOf( char k ); 170 | /** 171 | * @see List#remove(int) 172 | */ 173 | char removeChar( int index ); 174 | /** 175 | * @see List#set(int,Object) 176 | */ 177 | char set( int index, char k ); 178 | } 179 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharListIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.ListIterator; 40 | /** A type-specific bidirectional iterator that is also a {@link ListIterator}. 41 | * 42 | *

This interface merges the methods provided by a {@link ListIterator} and 43 | * a type-specific {@link org.codelibs.jhighlight.fastutil.BidirectionalIterator}. Moreover, it provides 44 | * type-specific versions of {@link java.util.ListIterator#add(Object) add()} 45 | * and {@link java.util.ListIterator#set(Object) set()}. 46 | * 47 | * @see java.util.ListIterator 48 | * @see org.codelibs.jhighlight.fastutil.BidirectionalIterator 49 | */ 50 | public interface CharListIterator extends ListIterator, CharBidirectionalIterator { 51 | void set( char k ); 52 | void add( char k ); 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharSet.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import java.util.Set; 40 | /** A type-specific {@link Set}; provides some additional methods that use polymorphism to avoid (un)boxing. 41 | * 42 | *

Additionally, this interface strengthens (again) {@link #iterator()}. 43 | * 44 | * @see Set 45 | */ 46 | public interface CharSet extends CharCollection , Set { 47 | /** Returns a type-specific iterator on the elements of this set. 48 | * 49 | *

Note that this specification strengthens the one given in {@link java.lang.Iterable#iterator()}, 50 | * which was already strengthened in the corresponding type-specific class, 51 | * but was weakened by the fact that this interface extends {@link Set}. 52 | * 53 | * @return a type-specific iterator on the elements of this set. 54 | */ 55 | CharIterator iterator(); 56 | /** Removes an element from this set. 57 | * 58 | *

Note that the corresponding method of the type-specific collection is rem(). 59 | * This unfortunate situation is caused by the clash 60 | * with the similarly named index-based method in the {@link java.util.List} interface. 61 | * 62 | * @see java.util.Collection#remove(Object) 63 | */ 64 | public boolean remove( char k ); 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/chars/CharStack.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.chars; 39 | import org.codelibs.jhighlight.fastutil.Stack; 40 | /** A type-specific {@link Stack}; provides some additional methods that use polymorphism to avoid (un)boxing. 41 | */ 42 | public interface CharStack extends Stack { 43 | /** 44 | * @see Stack#push(Object) 45 | */ 46 | void push( char k ); 47 | /** 48 | * @see Stack#pop() 49 | */ 50 | char popChar(); 51 | /** 52 | * @see Stack#top() 53 | */ 54 | char topChar(); 55 | /** 56 | * @see Stack#peek(int) 57 | */ 58 | char peekChar( int i ); 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/ints/IntComparator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Primitive-type-only definitions (keys) */ 22 | /* Object/Reference-only definitions (values) */ 23 | /* 24 | * Copyright (C) 2002-2014 Sebastiano Vigna 25 | * 26 | * Licensed under the Apache License, Version 2.0 (the "License"); 27 | * you may not use this file except in compliance with the License. 28 | * You may obtain a copy of the License at 29 | * 30 | * http://www.apache.org/licenses/LICENSE-2.0 31 | * 32 | * Unless required by applicable law or agreed to in writing, software 33 | * distributed under the License is distributed on an "AS IS" BASIS, 34 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 | * See the License for the specific language governing permissions and 36 | * limitations under the License. 37 | */ 38 | package org.codelibs.jhighlight.fastutil.ints; 39 | import java.util.Comparator; 40 | /** A type-specific {@link Comparator}; provides methods to compare two primitive types both as objects 41 | * and as primitive types. 42 | * 43 | *

Note that fastutil provides a corresponding abstract class that 44 | * can be used to implement this interface just by specifying the type-specific 45 | * comparator. 46 | * 47 | * @see Comparator 48 | */ 49 | public interface IntComparator extends Comparator { 50 | /** Compares the given primitive types. 51 | * 52 | * @see java.util.Comparator 53 | * @return A positive integer, zero, or a negative integer if the first 54 | * argument is greater than, equal to, or smaller than, respectively, the 55 | * second one. 56 | */ 57 | public int compare( int k1, int k2 ); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/AbstractObjectBidirectionalIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | /** An abstract class facilitating the creation of type-specific {@linkplain org.codelibs.jhighlight.fastutil.BidirectionalIterator bidirectional iterators}. 39 | * 40 | *

To create a type-specific bidirectional iterator, besides what is needed 41 | * for an iterator you need both a method returning the previous element as 42 | * primitive type and a method returning the previous element as an 43 | * object. However, if you inherit from this class you need just one (anyone). 44 | * 45 | *

This class implements also a trivial version of {@link #back(int)} that 46 | * uses type-specific methods. 47 | */ 48 | public abstract class AbstractObjectBidirectionalIterator extends AbstractObjectIterator implements ObjectBidirectionalIterator { 49 | protected AbstractObjectBidirectionalIterator() {} 50 | /** This method just iterates the type-specific version of {@link #previous()} for 51 | * at most n times, stopping if {@link 52 | * #hasPrevious()} becomes false. */ 53 | public int back( final int n ) { 54 | int i = n; 55 | while( i-- != 0 && hasPrevious() ) previous(); 56 | return n - i - 1; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/AbstractObjectCollection.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import java.util.AbstractCollection; 39 | import java.util.Collection; 40 | import java.util.Iterator; 41 | /** An abstract class providing basic methods for collections implementing a type-specific interface. 42 | * 43 | *

In particular, this class provide {@link #iterator()}, add(), {@link #remove(Object)} and 44 | * {@link #contains(Object)} methods that just call the type-specific counterpart. 45 | */ 46 | public abstract class AbstractObjectCollection extends AbstractCollection implements ObjectCollection { 47 | protected AbstractObjectCollection() {} 48 | public Object[] toArray() { 49 | final Object[] a = new Object[ size() ]; 50 | org.codelibs.jhighlight.fastutil.objects.ObjectIterators.unwrap( iterator(), a ); 51 | return a; 52 | } 53 | @SuppressWarnings("unchecked") 54 | public T[] toArray( T[] a ) { 55 | if ( a.length < size() ) a = (T[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), size() ); 56 | org.codelibs.jhighlight.fastutil.objects.ObjectIterators.unwrap( iterator(), a ); 57 | return a; 58 | } 59 | /** Adds all elements of the given collection to this collection. 60 | * 61 | * @param c a collection. 62 | * @return true if this collection changed as a result of the call. 63 | */ 64 | public boolean addAll( Collection c ) { 65 | boolean retVal = false; 66 | final Iterator i = c.iterator(); 67 | int n = c.size(); 68 | while( n-- != 0 ) if ( add( i.next() ) ) retVal = true; 69 | return retVal; 70 | } 71 | public boolean add( K k ) { 72 | throw new UnsupportedOperationException(); 73 | } 74 | /** Delegates to the new covariantly stronger generic method. */ 75 | @Deprecated 76 | public ObjectIterator objectIterator() { 77 | return iterator(); 78 | } 79 | public abstract ObjectIterator iterator(); 80 | /** Checks whether this collection contains all elements from the given collection. 81 | * 82 | * @param c a collection. 83 | * @return true if this collection contains all elements of the argument. 84 | */ 85 | public boolean containsAll( Collection c ) { 86 | int n = c.size(); 87 | final Iterator i = c.iterator(); 88 | while( n-- != 0 ) if ( ! contains( i.next() ) ) return false; 89 | return true; 90 | } 91 | /** Retains in this collection only elements from the given collection. 92 | * 93 | * @param c a collection. 94 | * @return true if this collection changed as a result of the call. 95 | */ 96 | public boolean retainAll( Collection c ) { 97 | boolean retVal = false; 98 | int n = size(); 99 | final Iterator i = iterator(); 100 | while( n-- != 0 ) { 101 | if ( ! c.contains( i.next() ) ) { 102 | i.remove(); 103 | retVal = true; 104 | } 105 | } 106 | return retVal; 107 | } 108 | /** Remove from this collection all elements in the given collection. 109 | * If the collection is an instance of this class, it uses faster iterators. 110 | * 111 | * @param c a collection. 112 | * @return true if this collection changed as a result of the call. 113 | */ 114 | public boolean removeAll( Collection c ) { 115 | boolean retVal = false; 116 | int n = c.size(); 117 | final Iterator i = c.iterator(); 118 | while( n-- != 0 ) if ( remove( i.next() ) ) retVal = true; 119 | return retVal; 120 | } 121 | public boolean isEmpty() { 122 | return size() == 0; 123 | } 124 | public String toString() { 125 | final StringBuilder s = new StringBuilder(); 126 | final ObjectIterator i = iterator(); 127 | int n = size(); 128 | Object k; 129 | boolean first = true; 130 | s.append("{"); 131 | while(n-- != 0) { 132 | if (first) first = false; 133 | else s.append(", "); 134 | k = i.next(); 135 | if (this == k) s.append("(this collection)"); else 136 | s.append(String.valueOf(k)); 137 | } 138 | s.append("}"); 139 | return s.toString(); 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/AbstractObjectIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | /** An abstract class facilitating the creation of type-specific iterators. 39 | * 40 | *

To create a type-specific iterator you need both a method returning the 41 | * next element as primitive type and a method returning the next element as an 42 | * object. However, if you inherit from this class you need just one (anyone). 43 | * 44 | *

This class implements also a trivial version of {@link #skip(int)} that uses 45 | * type-specific methods; moreover, {@link #remove()} will throw an {@link 46 | * UnsupportedOperationException}. 47 | * 48 | * @see java.util.Iterator 49 | */ 50 | public abstract class AbstractObjectIterator implements ObjectIterator { 51 | protected AbstractObjectIterator() {} 52 | /** This method just throws an {@link UnsupportedOperationException}. */ 53 | public void remove() { throw new UnsupportedOperationException(); } 54 | /** This method just iterates the type-specific version of {@link #next()} for at most 55 | * n times, stopping if {@link #hasNext()} becomes false.*/ 56 | public int skip( final int n ) { 57 | int i = n; 58 | while( i-- != 0 && hasNext() ) next(); 59 | return n - i - 1; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/AbstractObjectListIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | /** An abstract class facilitating the creation of type-specific {@linkplain java.util.ListIterator list iterators}. 39 | * 40 | *

This class provides trivial type-specific implementations of {@link 41 | * java.util.ListIterator#set(Object) set()} and {@link java.util.ListIterator#add(Object) add()} which 42 | * throw an {@link UnsupportedOperationException}. For primitive types, it also 43 | * provides a trivial implementation of {@link java.util.ListIterator#set(Object) set()} and {@link 44 | * java.util.ListIterator#add(Object) add()} that just invokes the type-specific one. 45 | * 46 | * 47 | * @see java.util.ListIterator 48 | */ 49 | public abstract class AbstractObjectListIterator extends AbstractObjectBidirectionalIterator implements ObjectListIterator { 50 | protected AbstractObjectListIterator() {} 51 | /** This method just throws an {@link UnsupportedOperationException}. */ 52 | public void set( K k ) { throw new UnsupportedOperationException(); } 53 | /** This method just throws an {@link UnsupportedOperationException}. */ 54 | public void add( K k ) { throw new UnsupportedOperationException(); } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/AbstractObjectSet.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import java.util.Set; 39 | /** An abstract class providing basic methods for sets implementing a type-specific interface. */ 40 | public abstract class AbstractObjectSet extends AbstractObjectCollection implements Cloneable, ObjectSet { 41 | protected AbstractObjectSet() {} 42 | public abstract ObjectIterator iterator(); 43 | public boolean equals( final Object o ) { 44 | if ( o == this ) return true; 45 | if ( !( o instanceof Set ) ) return false; 46 | Set s = (Set) o; 47 | if ( s.size() != size() ) return false; 48 | return containsAll(s); 49 | } 50 | /** Returns a hash code for this set. 51 | * 52 | * The hash code of a set is computed by summing the hash codes of 53 | * its elements. 54 | * 55 | * @return a hash code for this set. 56 | */ 57 | public int hashCode() { 58 | int h = 0, n = size(); 59 | ObjectIterator i = iterator(); 60 | K k; 61 | while( n-- != 0 ) { 62 | k = i.next(); // We need k because KEY2JAVAHASH() is a macro with repeated evaluation. 63 | h += ( (k) == null ? 0 : (k).hashCode() ); 64 | } 65 | return h; 66 | } 67 | public boolean remove( Object k ) { 68 | throw new UnsupportedOperationException(); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/ObjectBidirectionalIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import org.codelibs.jhighlight.fastutil.BidirectionalIterator; 39 | /** A type-specific bidirectional iterator; provides an additional method to avoid (un)boxing, 40 | * and the possibility to skip elements backwards. 41 | * 42 | * @see BidirectionalIterator 43 | */ 44 | public interface ObjectBidirectionalIterator extends ObjectIterator , BidirectionalIterator { 45 | /** Moves back for the given number of elements. 46 | * 47 | *

The effect of this call is exactly the same as that of 48 | * calling {@link #previous()} for n times (possibly stopping 49 | * if {@link #hasPrevious()} becomes false). 50 | * 51 | * @param n the number of elements to skip back. 52 | * @return the number of elements actually skipped. 53 | * @see java.util.Iterator#next() 54 | */ 55 | int back( int n ); 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/ObjectCollection.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import java.util.Collection; 39 | /** A type-specific {@link Collection}; provides some additional methods 40 | * that use polymorphism to avoid (un)boxing. 41 | * 42 | *

Additionally, this class defines strengthens (again) {@link #iterator()} and defines 43 | * a slightly different semantics for {@link #toArray(Object[])}. 44 | * 45 | * @see Collection 46 | */ 47 | public interface ObjectCollection extends Collection, ObjectIterable { 48 | /** Returns a type-specific iterator on the elements of this collection. 49 | * 50 | *

Note that this specification strengthens the one given in 51 | * {@link java.lang.Iterable#iterator()}, which was already 52 | * strengthened in the corresponding type-specific class, 53 | * but was weakened by the fact that this interface extends {@link Collection}. 54 | * 55 | * @return a type-specific iterator on the elements of this collection. 56 | */ 57 | ObjectIterator iterator(); 58 | /** Returns a type-specific iterator on this elements of this collection. 59 | * 60 | * @see #iterator() 61 | * @deprecated As of fastutil 5, replaced by {@link #iterator()}. 62 | */ 63 | @Deprecated 64 | ObjectIterator objectIterator(); 65 | /** Returns an containing the items of this collection; 66 | * the runtime type of the returned array is that of the specified array. 67 | * 68 | *

Warning: Note that, contrarily to {@link Collection#toArray(Object[])}, this 69 | * methods just writes all elements of this collection: no special 70 | * value will be added after the last one. 71 | * 72 | * @param a if this array is big enough, it will be used to store this collection. 73 | * @return a primitive type array containing the items of this collection. 74 | * @see Collection#toArray(Object[]) 75 | */ 76 | T[] toArray(T[] a); 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/ObjectIterable.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | 39 | /** A type-specific {@link Iterable} that strengthens that specification of {@link Iterable#iterator()}. 40 | * 41 | *

Warning: Java will let you write “colon” for statements with primitive-type 42 | * loop variables; however, what is (unfortunately) really happening is that at each iteration an 43 | * unboxing (and, in the case of fastutil type-specific data structures, a boxing) will be performed. Watch out. 44 | * 45 | * @see Iterable 46 | */ 47 | public interface ObjectIterable extends Iterable { 48 | /** Returns a type-specific iterator. 49 | * 50 | * Note that this specification strengthens the one given in {@link Iterable#iterator()}. 51 | * 52 | * @return a type-specific iterator. 53 | */ 54 | ObjectIterator iterator(); 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/ObjectIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import java.util.Iterator; 39 | /** A type-specific {@link Iterator}; provides an additional method to avoid (un)boxing, and 40 | * the possibility to skip elements. 41 | * 42 | * @see Iterator 43 | */ 44 | public interface ObjectIterator extends Iterator { 45 | /** Skips the given number of elements. 46 | * 47 | *

The effect of this call is exactly the same as that of 48 | * calling {@link #next()} for n times (possibly stopping 49 | * if {@link #hasNext()} becomes false). 50 | * 51 | * @param n the number of elements to skip. 52 | * @return the number of elements actually skipped. 53 | * @see Iterator#next() 54 | */ 55 | int skip( int n ); 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/ObjectList.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import java.util.List; 39 | /** A type-specific {@link List}; provides some additional methods that use polymorphism to avoid (un)boxing. 40 | * 41 | *

Note that this type-specific interface extends {@link Comparable}: it is expected that implementing 42 | * classes perform a lexicographical comparison using the standard operator "less then" for primitive types, 43 | * and the usual {@link Comparable#compareTo(Object) compareTo()} method for objects. 44 | * 45 | *

Additionally, this interface strengthens {@link #listIterator()}, 46 | * {@link #listIterator(int)} and {@link #subList(int,int)}. 47 | * 48 | *

Besides polymorphic methods, this interfaces specifies methods to copy into an array or remove contiguous 49 | * sublists. Although the abstract implementation of this interface provides simple, one-by-one implementations 50 | * of these methods, it is expected that concrete implementation override them with optimized versions. 51 | * 52 | * @see List 53 | */ 54 | public interface ObjectList extends List, Comparable>, ObjectCollection { 55 | /** Returns a type-specific iterator on the elements of this list (in proper sequence). 56 | * 57 | * Note that this specification strengthens the one given in {@link List#iterator()}. 58 | * It would not be normally necessary, but {@link java.lang.Iterable#iterator()} is bizarrily re-specified 59 | * in {@link List}. 60 | * 61 | * @return an iterator on the elements of this list (in proper sequence). 62 | */ 63 | ObjectListIterator iterator(); 64 | /** Returns a type-specific list iterator on the list. 65 | * 66 | * @see #listIterator() 67 | * @deprecated As of fastutil 5, replaced by {@link #listIterator()}. 68 | */ 69 | @Deprecated 70 | ObjectListIterator objectListIterator(); 71 | /** Returns a type-specific list iterator on the list starting at a given index. 72 | * 73 | * @see #listIterator(int) 74 | * @deprecated As of fastutil 5, replaced by {@link #listIterator(int)}. 75 | */ 76 | @Deprecated 77 | ObjectListIterator objectListIterator( int index ); 78 | /** Returns a type-specific list iterator on the list. 79 | * 80 | * @see List#listIterator() 81 | */ 82 | ObjectListIterator listIterator(); 83 | /** Returns a type-specific list iterator on the list starting at a given index. 84 | * 85 | * @see List#listIterator(int) 86 | */ 87 | ObjectListIterator listIterator( int index ); 88 | /** Returns a type-specific view of the portion of this list from the index from, inclusive, to the index to, exclusive. 89 | * @see List#subList(int,int) 90 | * @deprecated As of fastutil 5, replaced by {@link #subList(int,int)}. 91 | */ 92 | @Deprecated 93 | ObjectList objectSubList( int from, int to ); 94 | /** Returns a type-specific view of the portion of this list from the index from, inclusive, to the index to, exclusive. 95 | * 96 | *

Note that this specification strengthens the one given in {@link List#subList(int,int)}. 97 | * 98 | * @see List#subList(int,int) 99 | */ 100 | ObjectList subList(int from, int to); 101 | /** Sets the size of this list. 102 | * 103 | *

If the specified size is smaller than the current size, the last elements are 104 | * discarded. Otherwise, they are filled with 0/null/false. 105 | * 106 | * @param size the new size. 107 | */ 108 | void size( int size ); 109 | /** Copies (hopefully quickly) elements of this type-specific list into the given array. 110 | * 111 | * @param from the start index (inclusive). 112 | * @param a the destination array. 113 | * @param offset the offset into the destination array where to store the first element copied. 114 | * @param length the number of elements to be copied. 115 | */ 116 | void getElements( int from, Object a[], int offset, int length ); 117 | /** Removes (hopefully quickly) elements of this type-specific list. 118 | * 119 | * @param from the start index (inclusive). 120 | * @param to the end index (exclusive). 121 | */ 122 | void removeElements( int from, int to ); 123 | /** Add (hopefully quickly) elements to this type-specific list. 124 | * 125 | * @param index the index at which to add elements. 126 | * @param a the array containing the elements. 127 | */ 128 | void addElements( int index, K a[] ); 129 | /** Add (hopefully quickly) elements to this type-specific list. 130 | * 131 | * @param index the index at which to add elements. 132 | * @param a the array containing the elements. 133 | * @param offset the offset of the first element to add. 134 | * @param length the number of elements to add. 135 | */ 136 | void addElements( int index, K a[], int offset, int length ); 137 | } 138 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/ObjectListIterator.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import java.util.ListIterator; 39 | /** A type-specific bidirectional iterator that is also a {@link ListIterator}. 40 | * 41 | *

This interface merges the methods provided by a {@link ListIterator} and 42 | * a type-specific {@link org.codelibs.jhighlight.fastutil.BidirectionalIterator}. Moreover, it provides 43 | * type-specific versions of {@link java.util.ListIterator#add(Object) add()} 44 | * and {@link java.util.ListIterator#set(Object) set()}. 45 | * 46 | * @see java.util.ListIterator 47 | * @see org.codelibs.jhighlight.fastutil.BidirectionalIterator 48 | */ 49 | public interface ObjectListIterator extends ListIterator, ObjectBidirectionalIterator { 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/fastutil/objects/ObjectSet.java: -------------------------------------------------------------------------------- 1 | /* Generic definitions */ 2 | /* Assertions (useful to generate conditional code) */ 3 | /* Current type and class (and size, if applicable) */ 4 | /* Value methods */ 5 | /* Interfaces (keys) */ 6 | /* Interfaces (values) */ 7 | /* Abstract implementations (keys) */ 8 | /* Abstract implementations (values) */ 9 | /* Static containers (keys) */ 10 | /* Static containers (values) */ 11 | /* Implementations */ 12 | /* Synchronized wrappers */ 13 | /* Unmodifiable wrappers */ 14 | /* Other wrappers */ 15 | /* Methods (keys) */ 16 | /* Methods (values) */ 17 | /* Methods (keys/values) */ 18 | /* Methods that have special names depending on keys (but the special names depend on values) */ 19 | /* Equality */ 20 | /* Object/Reference-only definitions (keys) */ 21 | /* Object/Reference-only definitions (values) */ 22 | /* 23 | * Copyright (C) 2002-2014 Sebastiano Vigna 24 | * 25 | * Licensed under the Apache License, Version 2.0 (the "License"); 26 | * you may not use this file except in compliance with the License. 27 | * You may obtain a copy of the License at 28 | * 29 | * http://www.apache.org/licenses/LICENSE-2.0 30 | * 31 | * Unless required by applicable law or agreed to in writing, software 32 | * distributed under the License is distributed on an "AS IS" BASIS, 33 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 34 | * See the License for the specific language governing permissions and 35 | * limitations under the License. 36 | */ 37 | package org.codelibs.jhighlight.fastutil.objects; 38 | import java.util.Set; 39 | /** A type-specific {@link Set}; provides some additional methods that use polymorphism to avoid (un)boxing. 40 | * 41 | *

Additionally, this interface strengthens (again) {@link #iterator()}. 42 | * 43 | * @see Set 44 | */ 45 | public interface ObjectSet extends ObjectCollection , Set { 46 | /** Returns a type-specific iterator on the elements of this set. 47 | * 48 | *

Note that this specification strengthens the one given in {@link java.lang.Iterable#iterator()}, 49 | * which was already strengthened in the corresponding type-specific class, 50 | * but was weakened by the fact that this interface extends {@link Set}. 51 | * 52 | * @return a type-specific iterator on the elements of this set. 53 | */ 54 | ObjectIterator iterator(); 55 | /** Removes an element from this set. 56 | * 57 | *

Note that the corresponding method of the type-specific collection is rem(). 58 | * This unfortunate situation is caused by the clash 59 | * with the similarly named index-based method in the {@link java.util.List} interface. 60 | * 61 | * @see java.util.Collection#remove(Object) 62 | */ 63 | public boolean remove( Object k ); 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/highlighter/CppHighlighter.flex: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 Arnout Engelen . 3 | * Copyright 2000-2006 Omnicore Software, Hans Kratz & Dennis Strein GbR, 4 | * Geert Bevin . 5 | * Distributed under the terms of either: 6 | * - the common development and distribution license (CDDL), v1.0; or 7 | * - the GNU Lesser General Public License, v2.1 or later 8 | * $Id$ 9 | */ 10 | package org.codelibs.jhighlight.highlighter; 11 | 12 | import java.io.Reader; 13 | import java.io.IOException; 14 | 15 | %% 16 | 17 | %class CppHighlighter 18 | %implements ExplicitStateHighlighter 19 | 20 | %unicode 21 | %pack 22 | 23 | %buffer 128 24 | 25 | %public 26 | 27 | %int 28 | 29 | %{ 30 | /* styles */ 31 | 32 | public static final byte PLAIN_STYLE = 1; 33 | public static final byte KEYWORD_STYLE = 2; 34 | public static final byte TYPE_STYLE = 3; 35 | public static final byte OPERATOR_STYLE = 4; 36 | public static final byte SEPARATOR_STYLE = 5; 37 | public static final byte LITERAL_STYLE = 6; 38 | public static final byte CPP_COMMENT_STYLE = 7; 39 | public static final byte DOXYGEN_COMMENT_STYLE = 8; 40 | public static final byte DOXYGEN_TAG_STYLE = 9; 41 | public static final byte PREPROC_STYLE = 10; 42 | 43 | /* Highlighter implementation */ 44 | 45 | public int getStyleCount() 46 | { 47 | return 10; 48 | } 49 | 50 | public byte getStartState() 51 | { 52 | return YYINITIAL+1; 53 | } 54 | 55 | public byte getCurrentState() 56 | { 57 | return (byte) (yystate()+1); 58 | } 59 | 60 | public void setState(byte newState) 61 | { 62 | yybegin(newState-1); 63 | } 64 | 65 | public byte getNextToken() 66 | throws IOException 67 | { 68 | return (byte) yylex(); 69 | } 70 | 71 | public int getTokenLength() 72 | { 73 | return yylength(); 74 | } 75 | 76 | public void setReader(Reader r) 77 | { 78 | this.zzReader = r; 79 | } 80 | 81 | public CppHighlighter() 82 | { 83 | } 84 | %} 85 | 86 | /* main character classes */ 87 | 88 | WhiteSpace = [ \t\f] 89 | 90 | /* identifiers */ 91 | 92 | ConstantIdentifier = {SimpleConstantIdentifier} 93 | SimpleConstantIdentifier = [#A-Z0-9_]+ 94 | 95 | Identifier = [:jletter:][:jletterdigit:]* 96 | 97 | TypeIdentifier = {SimpleTypeIdentifier} 98 | SimpleTypeIdentifier = [A-Z][:jletterdigit:]* 99 | 100 | /* int literals */ 101 | 102 | DecLiteral = 0 | [1-9][0-9]* {IntegerSuffix} 103 | 104 | HexLiteral = 0 [xX] 0* {HexDigit}* {IntegerSuffix} 105 | HexDigit = [0-9a-fA-F] 106 | 107 | OctLiteral = 0+ {OctDigit}* {IntegerSuffix} 108 | OctDigit = [0-7] 109 | 110 | IntegerSuffix = [uU]? [lL]? [uU]? 111 | 112 | /* float literals */ 113 | 114 | FloatLiteral = ({FLit1}|{FLit2}|{FLit3}|{FLit4}) ([fF]|[dD])? 115 | 116 | FLit1 = [0-9]+ \. [0-9]* {Exponent}? 117 | FLit2 = \. [0-9]+ {Exponent}? 118 | FLit3 = [0-9]+ {Exponent} 119 | FLit4 = [0-9]+ {Exponent}? 120 | 121 | Exponent = [eE] [+\-]? [0-9]+ 122 | 123 | %state IN_COMMENT, IN_DOXYGEN_COMMENT 124 | 125 | %% 126 | 127 | { 128 | 129 | /* keywords */ 130 | "__abstract" | 131 | "abstract" | 132 | "__alignof" | 133 | "array" | 134 | "__asm" | 135 | "__assume" | 136 | "__based" | 137 | "__box" | 138 | "break" | 139 | "case" | 140 | "catch" | 141 | "__cdecl" | 142 | "class" | 143 | "const" | 144 | "const_cast" | 145 | "continue" | 146 | "__declspec" | 147 | "default" | 148 | "__delegate" | 149 | "delegate" | 150 | "delete" | 151 | "deprecated" | 152 | "dllexport" | 153 | "dllimport" | 154 | "do" | 155 | "double" | 156 | "dynamic_cast" | 157 | "else" | 158 | "event" | 159 | "__event" | 160 | "__except" | 161 | "explicit" | 162 | "extern" | 163 | "false" | 164 | "__fastcall" | 165 | "__finally" | 166 | "finally" | 167 | "for" | 168 | "__forceinline" | 169 | "friend" | 170 | "friend_as" | 171 | "__gc" | 172 | "gcnew" | 173 | "generic" | 174 | "goto" | 175 | "enum" | 176 | "__hook" | 177 | "__identifier" | 178 | "if" | 179 | "__if_exists" | 180 | "__if_not_exists" | 181 | "initonly" | 182 | "__inline" | 183 | "inline" | 184 | "__int8" | 185 | "__int16" | 186 | "__int32" | 187 | "__int64" | 188 | "__interface" | 189 | "interface" | 190 | "interface" | 191 | "interior_ptr" | 192 | "__leave" | 193 | "literal" | 194 | "__m64" | 195 | "__m128" | 196 | "__m128d" | 197 | "__m128i" | 198 | "__multiple_inheritance" | 199 | "mutable" | 200 | "naked" | 201 | "namespace" | 202 | "new" | 203 | "__nogc" | 204 | "noinline" | 205 | "__noop" | 206 | "noreturn" | 207 | "nothrow" | 208 | "novtable" | 209 | "nullptr" | 210 | "operator" | 211 | "__pin" | 212 | "private" | 213 | "__property" | 214 | "property" | 215 | "property" | 216 | "protected" | 217 | "public" | 218 | "__raise" | 219 | "register" | 220 | "reinterpret_cast" | 221 | "return" | 222 | "safecast" | 223 | "__sealed" | 224 | "sealed" | 225 | "selectany" | 226 | "signed" | 227 | "__single_inheritance" | 228 | "sizeof" | 229 | "static" | 230 | "static_cast" | 231 | "__stdcall" | 232 | "struct" | 233 | "__super" | 234 | "switch" | 235 | "template" | 236 | "this" | 237 | "thread" | 238 | "throw" | 239 | "true" | 240 | "try" | 241 | "__try" | 242 | "__except" | 243 | "__try_cast" | 244 | "typedef" | 245 | "typeid" | 246 | "typeid" | 247 | "typename" | 248 | "__unaligned" | 249 | "__unhook" | 250 | "union" | 251 | "unsigned" | 252 | "using" | 253 | "uuid" | 254 | "__uuidof" | 255 | "value" | 256 | "__value" | 257 | "virtual" | 258 | "__virtual_inheritance" | 259 | "void" | 260 | "volatile" | 261 | "__w64" | 262 | "__wchar_t," | 263 | "while" 264 | { return KEYWORD_STYLE; } 265 | 266 | "bool" | 267 | "char" | 268 | "double" | 269 | "int" | 270 | "long" | 271 | "float" | 272 | "short" | 273 | "void" { return TYPE_STYLE; } 274 | 275 | /* literals */ 276 | "true" | 277 | "false" | 278 | 279 | (\" ( [^\"\n\\] | \\[^\n] )* (\n | \\\n | \")) | 280 | (\' ( [^\'\n\\] | \\[^\n] )* (\n | \\\n | \')) | 281 | 282 | {DecLiteral} | 283 | {OctLiteral} | 284 | {HexLiteral} | 285 | 286 | {FloatLiteral} 287 | { return LITERAL_STYLE; } 288 | 289 | /* preprocessor symbols */ 290 | "#define" | 291 | "#elif" | 292 | "#else" | 293 | "#endif" | 294 | "#error" | 295 | "#ifdef" | 296 | "#ifndef" | 297 | "#if" | 298 | "#import" | 299 | "#include" | 300 | "#line" | 301 | "#pragma" | 302 | "#undef" | 303 | "#using" 304 | { return PREPROC_STYLE; } 305 | 306 | 307 | /* separators */ 308 | "(" | 309 | ")" | 310 | "{" | 311 | "}" | 312 | "[" | 313 | "]" | 314 | ";" | 315 | "," | 316 | "." { return SEPARATOR_STYLE; } 317 | 318 | /* operators */ 319 | "=" | 320 | ">" | 321 | "<" | 322 | "!" | 323 | "~" | 324 | "?" | 325 | ":" | 326 | "+" | 327 | "-" | 328 | "*" | 329 | "/" | 330 | "&" | 331 | "|" | 332 | "^" | 333 | "%" { return OPERATOR_STYLE; } 334 | 335 | {ConstantIdentifier} { return PLAIN_STYLE; } 336 | 337 | {TypeIdentifier} { return TYPE_STYLE; } 338 | 339 | \n | 340 | {Identifier} | 341 | {WhiteSpace} { return PLAIN_STYLE; } 342 | 343 | 344 | 345 | // single line comment 346 | 347 | "//" [^\n]* \n | 348 | 349 | // short comment 350 | 351 | "/**/" { return CPP_COMMENT_STYLE; } 352 | 353 | // comment start 354 | 355 | "/**" { yybegin(IN_DOXYGEN_COMMENT); return DOXYGEN_COMMENT_STYLE;} 356 | "/*" { yybegin(IN_COMMENT); return CPP_COMMENT_STYLE;} 357 | 358 | } 359 | 360 | 361 | // normal comment mode 362 | 363 | { 364 | 365 | 366 | // comment unterminated 367 | 368 | ([^\n*]|\*+[^\n*/])* (\n | \*+\n) { return CPP_COMMENT_STYLE; } 369 | 370 | // comment terminated 371 | 372 | ([^\n*]|\*+[^\n*/])* \*+ "/" { yybegin(YYINITIAL); return CPP_COMMENT_STYLE; } 373 | 374 | } 375 | 376 | // doc comment mode 377 | 378 | { 379 | 380 | // comment unterminated 381 | 382 | .|\n { return DOXYGEN_COMMENT_STYLE; } 383 | 384 | // comment terminated 385 | 386 | \* "/" { yybegin(YYINITIAL); return DOXYGEN_COMMENT_STYLE; } 387 | 388 | 389 | "@" {Identifier} { return DOXYGEN_TAG_STYLE; } 390 | 391 | } 392 | 393 | /* error fallback */ 394 | 395 | .|\n { return PLAIN_STYLE; } 396 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/highlighter/ExplicitStateHighlighter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2000-2006 Omnicore Software, Hans Kratz & Dennis Strein GbR, 3 | * Geert Bevin . 4 | * Distributed under the terms of either: 5 | * - the common development and distribution license (CDDL), v1.0; or 6 | * - the GNU Lesser General Public License, v2.1 or later 7 | * $Id: ExplicitStateHighlighter.java 3108 2006-03-13 18:03:00Z gbevin $ 8 | */ 9 | package org.codelibs.jhighlight.highlighter; 10 | 11 | import java.io.IOException; 12 | import java.io.Reader; 13 | 14 | /** 15 | * Provides access to the lexical scanning of a highlighted language. 16 | * 17 | * @author Omnicore Software 18 | * @author Hans Kratz & Dennis Strein GbR 19 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 20 | * @version $Revision: 3108 $ 21 | * @since 1.0 22 | */ 23 | public interface ExplicitStateHighlighter 24 | { 25 | /** 26 | * Sets the reader that will be used to receive the text data. 27 | * 28 | * @param reader the Reader that has to be used 29 | */ 30 | void setReader(Reader reader); 31 | 32 | /** 33 | * Obtain the next token from the scanner. 34 | * 35 | * @return one of the tokens that are define in the scanner 36 | * @exception IOException when an error occurred during the parsing of 37 | * the reader 38 | */ 39 | byte getNextToken() throws IOException; 40 | 41 | /** 42 | * Returns the length of the matched text region. 43 | * 44 | * @return the length of the matched text region 45 | */ 46 | int getTokenLength(); 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/highlighter/GroovyHighlighter.flex: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2000-2006 Omnicore Software, Hans Kratz & Dennis Strein GbR, 3 | * Geert Bevin . 4 | * Distributed under the terms of either: 5 | * - the common development and distribution license (CDDL), v1.0; or 6 | * - the GNU Lesser General Public License, v2.1 or later 7 | * $Id$ 8 | */ 9 | package org.codelibs.jhighlight.highlighter; 10 | 11 | import java.io.Reader; 12 | import java.io.IOException; 13 | 14 | %% 15 | 16 | %class GroovyHighlighter 17 | %implements ExplicitStateHighlighter 18 | 19 | %unicode 20 | %pack 21 | 22 | %buffer 128 23 | 24 | %public 25 | 26 | %int 27 | 28 | %{ 29 | /* styles */ 30 | 31 | public static final byte PLAIN_STYLE = 1; 32 | public static final byte KEYWORD_STYLE = 2; 33 | public static final byte TYPE_STYLE = 3; 34 | public static final byte OPERATOR_STYLE = 4; 35 | public static final byte SEPARATOR_STYLE = 5; 36 | public static final byte LITERAL_STYLE = 6; 37 | public static final byte JAVA_COMMENT_STYLE = 7; 38 | public static final byte JAVADOC_COMMENT_STYLE = 8; 39 | public static final byte JAVADOC_TAG_STYLE = 9; 40 | 41 | /* Highlighter implementation */ 42 | 43 | public int getStyleCount() 44 | { 45 | return 9; 46 | } 47 | 48 | public byte getStartState() 49 | { 50 | return YYINITIAL+1; 51 | } 52 | 53 | public byte getCurrentState() 54 | { 55 | return (byte) (yystate()+1); 56 | } 57 | 58 | public void setState(byte newState) 59 | { 60 | yybegin(newState-1); 61 | } 62 | 63 | public byte getNextToken() 64 | throws IOException 65 | { 66 | return (byte) yylex(); 67 | } 68 | 69 | public int getTokenLength() 70 | { 71 | return yylength(); 72 | } 73 | 74 | public void setReader(Reader r) 75 | { 76 | this.zzReader = r; 77 | } 78 | 79 | public GroovyHighlighter() 80 | { 81 | } 82 | %} 83 | 84 | /* main character classes */ 85 | 86 | WhiteSpace = [ \t\f] 87 | 88 | /* identifiers */ 89 | 90 | ConstantIdentifier = {SimpleConstantIdentifier} 91 | SimpleConstantIdentifier = [A-Z0-9_]+ 92 | 93 | Identifier = [:jletter:][:jletterdigit:]* 94 | 95 | TypeIdentifier = {SimpleTypeIdentifier} 96 | SimpleTypeIdentifier = [A-Z][:jletterdigit:]* 97 | 98 | /* int literals */ 99 | 100 | DecLiteral = 0 | [1-9][0-9]* [lL]? 101 | 102 | HexLiteral = 0 [xX] 0* {HexDigit}* [lL]? 103 | HexDigit = [0-9a-fA-F] 104 | 105 | OctLiteral = 0+ {OctDigit}* [lL]? 106 | OctDigit = [0-7] 107 | 108 | /* float literals */ 109 | 110 | FloatLiteral = ({FLit1}|{FLit2}|{FLit3}|{FLit4}) ([fF]|[dD])? 111 | 112 | FLit1 = [0-9]+ \. [0-9]* {Exponent}? 113 | FLit2 = \. [0-9]+ {Exponent}? 114 | FLit3 = [0-9]+ {Exponent} 115 | FLit4 = [0-9]+ {Exponent}? 116 | 117 | Exponent = [eE] [+\-]? [0-9]+ 118 | 119 | %state IN_HEREDOC, IN_COMMENT, IN_JAVA_DOC_COMMENT 120 | 121 | %% 122 | 123 | { 124 | 125 | /* keywords */ 126 | "abstract" | 127 | "as" | 128 | "assert" | 129 | "break" | 130 | "case" | 131 | "catch" | 132 | "class" | 133 | "const" | 134 | "continue" | 135 | "def" | 136 | "do" | 137 | "else" | 138 | "extends" | 139 | "final" | 140 | "finally" | 141 | "for" | 142 | "default" | 143 | "implements" | 144 | "import" | 145 | "in" | 146 | "instanceof" | 147 | "interface" | 148 | "mixin" | 149 | "native" | 150 | "new" | 151 | "goto" | 152 | "if" | 153 | "public" | 154 | "super" | 155 | "switch" | 156 | "synchronized" | 157 | "package" | 158 | "private" | 159 | "protected" | 160 | "transient" | 161 | "return" | 162 | "static" | 163 | "while" | 164 | "this" | 165 | "throw" | 166 | "throws" | 167 | "try" | 168 | "volatile" | 169 | "strictfp" { return KEYWORD_STYLE; } 170 | 171 | "boolean" | 172 | "byte" | 173 | "char" | 174 | "double" | 175 | "int" | 176 | "long" | 177 | "float" | 178 | "short" | 179 | "void" { return TYPE_STYLE; } 180 | 181 | /* literals */ 182 | "true" | 183 | "false" | 184 | "null" | 185 | 186 | (\" ( [^\"\n\\] | \\[^\n] )* (\n | \\\n | \")) | 187 | (\' ( [^\'\n\\] | \\[^\n] )* (\n | \\\n | \')) | 188 | 189 | {DecLiteral} | 190 | {HexLiteral} | 191 | {OctLiteral} | 192 | 193 | {FloatLiteral} 194 | { return LITERAL_STYLE; } 195 | 196 | "\"\"\"" { yybegin(IN_HEREDOC); return LITERAL_STYLE;} 197 | 198 | /* separators */ 199 | "(" | 200 | ")" | 201 | "{" | 202 | "}" | 203 | "[" | 204 | "]" | 205 | ";" | 206 | "," | 207 | "." { return SEPARATOR_STYLE; } 208 | 209 | /* operators */ 210 | "=" | 211 | ">" | 212 | "<" | 213 | "!" | 214 | "~" | 215 | "?" | 216 | ":" | 217 | "+" | 218 | "-" | 219 | "*" | 220 | "/" | 221 | "&" | 222 | "|" | 223 | "^" | 224 | "%" { return OPERATOR_STYLE; } 225 | 226 | {ConstantIdentifier} { return PLAIN_STYLE; } 227 | 228 | {TypeIdentifier} { return TYPE_STYLE; } 229 | 230 | \n | 231 | {Identifier} | 232 | {WhiteSpace} { return PLAIN_STYLE; } 233 | 234 | 235 | 236 | // single line comment 237 | 238 | "//" [^\n]* \n | 239 | 240 | // short comment 241 | 242 | "/**/" { return JAVA_COMMENT_STYLE; } 243 | 244 | // comment start 245 | 246 | "/**" { yybegin(IN_JAVA_DOC_COMMENT); return JAVADOC_COMMENT_STYLE;} 247 | "/*" { yybegin(IN_COMMENT); return JAVA_COMMENT_STYLE;} 248 | 249 | } 250 | 251 | 252 | // normal comment mode 253 | 254 | { 255 | 256 | 257 | // comment unterminated 258 | 259 | .|\n { return LITERAL_STYLE; } 260 | 261 | // comment terminated 262 | 263 | \"\"\" { yybegin(YYINITIAL); return LITERAL_STYLE; } 264 | 265 | } 266 | 267 | // normal comment mode 268 | 269 | { 270 | 271 | 272 | // comment unterminated 273 | 274 | ([^\n*]|\*+[^\n*/])* (\n | \*+\n) { return JAVA_COMMENT_STYLE; } 275 | 276 | // comment terminated 277 | 278 | ([^\n*]|\*+[^\n*/])* \*+ "/" { yybegin(YYINITIAL); return JAVA_COMMENT_STYLE; } 279 | 280 | } 281 | 282 | // doc comment mode 283 | 284 | { 285 | 286 | // comment unterminated 287 | 288 | .|\n { return JAVADOC_COMMENT_STYLE; } 289 | 290 | // comment terminated 291 | 292 | \* "/" { yybegin(YYINITIAL); return JAVADOC_COMMENT_STYLE; } 293 | 294 | 295 | "@" {Identifier} { return JAVADOC_TAG_STYLE; } 296 | 297 | } 298 | 299 | /* error fallback */ 300 | 301 | .|\n { return PLAIN_STYLE; } 302 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/highlighter/JavaHighlighter.flex: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2000-2006 Omnicore Software, Hans Kratz & Dennis Strein GbR, 3 | * Geert Bevin . 4 | * Distributed under the terms of either: 5 | * - the common development and distribution license (CDDL), v1.0; or 6 | * - the GNU Lesser General Public License, v2.1 or later 7 | * $Id$ 8 | */ 9 | package org.codelibs.jhighlight.highlighter; 10 | 11 | import java.io.Reader; 12 | import java.io.IOException; 13 | 14 | %% 15 | 16 | %class JavaHighlighter 17 | %implements ExplicitStateHighlighter 18 | 19 | %unicode 20 | %pack 21 | 22 | %buffer 128 23 | 24 | %public 25 | 26 | %int 27 | 28 | %{ 29 | public static boolean ASSERT_IS_KEYWORD = false; 30 | 31 | /* styles */ 32 | 33 | public static final byte PLAIN_STYLE = 1; 34 | public static final byte KEYWORD_STYLE = 2; 35 | public static final byte TYPE_STYLE = 3; 36 | public static final byte OPERATOR_STYLE = 4; 37 | public static final byte SEPARATOR_STYLE = 5; 38 | public static final byte LITERAL_STYLE = 6; 39 | public static final byte JAVA_COMMENT_STYLE = 7; 40 | public static final byte JAVADOC_COMMENT_STYLE = 8; 41 | public static final byte JAVADOC_TAG_STYLE = 9; 42 | 43 | /* Highlighter implementation */ 44 | 45 | public int getStyleCount() 46 | { 47 | return 9; 48 | } 49 | 50 | public byte getStartState() 51 | { 52 | return YYINITIAL+1; 53 | } 54 | 55 | public byte getCurrentState() 56 | { 57 | return (byte) (yystate()+1); 58 | } 59 | 60 | public void setState(byte newState) 61 | { 62 | yybegin(newState-1); 63 | } 64 | 65 | public byte getNextToken() 66 | throws IOException 67 | { 68 | return (byte) yylex(); 69 | } 70 | 71 | public int getTokenLength() 72 | { 73 | return yylength(); 74 | } 75 | 76 | public void setReader(Reader r) 77 | { 78 | this.zzReader = r; 79 | } 80 | 81 | public JavaHighlighter() 82 | { 83 | } 84 | %} 85 | 86 | /* main character classes */ 87 | 88 | WhiteSpace = [ \t\f] 89 | 90 | /* identifiers */ 91 | 92 | ConstantIdentifier = {SimpleConstantIdentifier} 93 | SimpleConstantIdentifier = [A-Z0-9_]+ 94 | 95 | Identifier = [:jletter:][:jletterdigit:]* 96 | 97 | TypeIdentifier = {SimpleTypeIdentifier} 98 | SimpleTypeIdentifier = [A-Z][:jletterdigit:]* 99 | 100 | /* int literals */ 101 | 102 | DecLiteral = 0 | [1-9][0-9]* [lL]? 103 | 104 | HexLiteral = 0 [xX] 0* {HexDigit}* [lL]? 105 | HexDigit = [0-9a-fA-F] 106 | 107 | OctLiteral = 0+ {OctDigit}* [lL]? 108 | OctDigit = [0-7] 109 | 110 | /* float literals */ 111 | 112 | FloatLiteral = ({FLit1}|{FLit2}|{FLit3}|{FLit4}) ([fF]|[dD])? 113 | 114 | FLit1 = [0-9]+ \. [0-9]* {Exponent}? 115 | FLit2 = \. [0-9]+ {Exponent}? 116 | FLit3 = [0-9]+ {Exponent} 117 | FLit4 = [0-9]+ {Exponent}? 118 | 119 | Exponent = [eE] [+\-]? [0-9]+ 120 | 121 | %state IN_COMMENT, IN_JAVA_DOC_COMMENT 122 | 123 | %% 124 | 125 | { 126 | 127 | /* keywords */ 128 | "abstract" | 129 | "break" | 130 | "case" | 131 | "catch" | 132 | "class" | 133 | "const" | 134 | "continue" | 135 | "do" | 136 | "else" | 137 | "extends" | 138 | "final" | 139 | "finally" | 140 | "for" | 141 | "default" | 142 | "implements" | 143 | "import" | 144 | "instanceof" | 145 | "interface" | 146 | "native" | 147 | "new" | 148 | "goto" | 149 | "if" | 150 | "public" | 151 | "super" | 152 | "switch" | 153 | "synchronized" | 154 | "package" | 155 | "private" | 156 | "protected" | 157 | "transient" | 158 | "return" | 159 | "static" | 160 | "while" | 161 | "this" | 162 | "throw" | 163 | "throws" | 164 | "try" | 165 | "volatile" | 166 | "strictfp" { return KEYWORD_STYLE; } 167 | 168 | "boolean" | 169 | "byte" | 170 | "char" | 171 | "double" | 172 | "int" | 173 | "long" | 174 | "float" | 175 | "short" | 176 | "void" { return TYPE_STYLE; } 177 | 178 | "assert" { return ASSERT_IS_KEYWORD ? KEYWORD_STYLE : PLAIN_STYLE; } 179 | 180 | /* literals */ 181 | "true" | 182 | "false" | 183 | "null" | 184 | 185 | (\" ( [^\"\n\\] | \\[^\n] )* (\n | \\\n | \")) | 186 | (\' ( [^\'\n\\] | \\[^\n] )* (\n | \\\n | \')) | 187 | 188 | {DecLiteral} | 189 | {HexLiteral} | 190 | {OctLiteral} | 191 | 192 | {FloatLiteral} 193 | { return LITERAL_STYLE; } 194 | 195 | /* separators */ 196 | "(" | 197 | ")" | 198 | "{" | 199 | "}" | 200 | "[" | 201 | "]" | 202 | ";" | 203 | "," | 204 | "." { return SEPARATOR_STYLE; } 205 | 206 | /* operators */ 207 | "=" | 208 | ">" | 209 | "<" | 210 | "!" | 211 | "~" | 212 | "?" | 213 | ":" | 214 | "+" | 215 | "-" | 216 | "*" | 217 | "/" | 218 | "&" | 219 | "|" | 220 | "^" | 221 | "%" { return OPERATOR_STYLE; } 222 | 223 | {ConstantIdentifier} { return PLAIN_STYLE; } 224 | 225 | {TypeIdentifier} { return TYPE_STYLE; } 226 | 227 | \n | 228 | {Identifier} | 229 | {WhiteSpace} { return PLAIN_STYLE; } 230 | 231 | 232 | 233 | // single line comment 234 | 235 | "//" [^\n]* \n | 236 | 237 | // short comment 238 | 239 | "/**/" { return JAVA_COMMENT_STYLE; } 240 | 241 | // comment start 242 | 243 | "/**" { yybegin(IN_JAVA_DOC_COMMENT); return JAVADOC_COMMENT_STYLE;} 244 | "/*" { yybegin(IN_COMMENT); return JAVA_COMMENT_STYLE;} 245 | 246 | } 247 | 248 | 249 | // normal comment mode 250 | 251 | { 252 | 253 | 254 | // comment unterminated 255 | 256 | ([^\n*]|\*+[^\n*/])* (\n | \*+\n) { return JAVA_COMMENT_STYLE; } 257 | 258 | // comment terminated 259 | 260 | ([^\n*]|\*+[^\n*/])* \*+ "/" { yybegin(YYINITIAL); return JAVA_COMMENT_STYLE; } 261 | 262 | } 263 | 264 | // doc comment mode 265 | 266 | { 267 | 268 | // comment unterminated 269 | 270 | .|\n { return JAVADOC_COMMENT_STYLE; } 271 | 272 | // comment terminated 273 | 274 | \* "/" { yybegin(YYINITIAL); return JAVADOC_COMMENT_STYLE; } 275 | 276 | 277 | "@" {Identifier} { return JAVADOC_TAG_STYLE; } 278 | 279 | } 280 | 281 | /* error fallback */ 282 | 283 | .|\n { return PLAIN_STYLE; } 284 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/highlighter/JavaScriptHighlighter.flex: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2000-2006 Omnicore Software, Hans Kratz & Dennis Strein GbR, 3 | * Geert Bevin , 4 | * Ulf Dittmer (ulf[remove] at ulfdittmer dot com). 5 | * Distributed under the terms of either: 6 | * - the common development and distribution license (CDDL), v1.0; or 7 | * - the GNU Lesser General Public License, v2.1 or later 8 | * $Id$ 9 | */ 10 | package org.codelibs.jhighlight.highlighter; 11 | 12 | import java.io.Reader; 13 | import java.io.IOException; 14 | 15 | %% 16 | 17 | %class JavaScriptHighlighter 18 | %implements ExplicitStateHighlighter 19 | 20 | %unicode 21 | %pack 22 | 23 | %buffer 128 24 | 25 | %public 26 | 27 | %int 28 | 29 | %{ 30 | /* styles */ 31 | 32 | public static final byte PLAIN_STYLE = 1; 33 | public static final byte KEYWORD_STYLE = 2; 34 | public static final byte OPERATOR_STYLE = 3; 35 | public static final byte SEPARATOR_STYLE = 4; 36 | public static final byte LITERAL_STYLE = 5; 37 | public static final byte JAVA_COMMENT_STYLE = 6; 38 | 39 | /* Highlighter implementation */ 40 | 41 | public int getStyleCount() 42 | { 43 | return 6; 44 | } 45 | 46 | public byte getStartState() 47 | { 48 | return YYINITIAL+1; 49 | } 50 | 51 | public byte getCurrentState() 52 | { 53 | return (byte) (yystate()+1); 54 | } 55 | 56 | public void setState(byte newState) 57 | { 58 | yybegin(newState-1); 59 | } 60 | 61 | public byte getNextToken() 62 | throws IOException 63 | { 64 | return (byte) yylex(); 65 | } 66 | 67 | public int getTokenLength() 68 | { 69 | return yylength(); 70 | } 71 | 72 | public void setReader(Reader r) 73 | { 74 | this.zzReader = r; 75 | } 76 | 77 | public JavaScriptHighlighter() 78 | { 79 | } 80 | %} 81 | 82 | /* main character classes */ 83 | 84 | WhiteSpace = [ \t\f] 85 | 86 | /* identifiers */ 87 | 88 | ConstantIdentifier = {SimpleConstantIdentifier} 89 | SimpleConstantIdentifier = [A-Z0-9_]+ 90 | 91 | Identifier = [:jletter:][:jletterdigit:]* 92 | 93 | /* int literals */ 94 | 95 | DecLiteral = 0 | [1-9][0-9]* 96 | 97 | /* float literals */ 98 | 99 | FloatLiteral = ({FLit1}|{FLit2}|{FLit3}|{FLit4}) 100 | 101 | FLit1 = [0-9]+ \. [0-9]* {Exponent}? 102 | FLit2 = \. [0-9]+ {Exponent}? 103 | FLit3 = [0-9]+ {Exponent} 104 | FLit4 = [0-9]+ {Exponent}? 105 | 106 | Exponent = [eE] [+\-]? [0-9]+ 107 | 108 | %state IN_COMMENT 109 | 110 | %% 111 | 112 | { 113 | 114 | /* keywords */ 115 | "break" | 116 | "case" | 117 | "continue" | 118 | "do" | 119 | "else" | 120 | "for" | 121 | "default" | 122 | "new" | 123 | "in" | 124 | "goto" | 125 | "if" | 126 | "switch" | 127 | "return" | 128 | "while" | 129 | "var" | 130 | "function" | 131 | "with" | 132 | "const" | 133 | "this" { return KEYWORD_STYLE; } 134 | 135 | /* literals */ 136 | "true" | 137 | "false" | 138 | "null" | 139 | 140 | (\" ( [^\"\n\\] | \\[^\n] )* (\n | \\\n | \")) | 141 | (\' ( [^\'\n\\] | \\[^\n] )* (\n | \\\n | \')) | 142 | 143 | {DecLiteral} | 144 | {FloatLiteral} 145 | { return LITERAL_STYLE; } 146 | 147 | /* separators */ 148 | "(" | 149 | ")" | 150 | "{" | 151 | "}" | 152 | "[" | 153 | "]" | 154 | ";" | 155 | "," | 156 | "." { return SEPARATOR_STYLE; } 157 | 158 | /* operators */ 159 | "=" | 160 | ">" | 161 | "<" | 162 | "!" | 163 | "~" | 164 | "?" | 165 | ":" | 166 | "+" | 167 | "-" | 168 | "*" | 169 | "/" | 170 | "&" | 171 | "|" | 172 | "^" | 173 | "%" { return OPERATOR_STYLE; } 174 | 175 | {ConstantIdentifier} { return PLAIN_STYLE; } 176 | 177 | \n | 178 | {Identifier} | 179 | {WhiteSpace} { return PLAIN_STYLE; } 180 | 181 | // single line comment 182 | 183 | "//" [^\n]* \n | 184 | 185 | // short comment 186 | 187 | "/**/" { return JAVA_COMMENT_STYLE; } 188 | 189 | // comment start 190 | 191 | "/*" { yybegin(IN_COMMENT); return JAVA_COMMENT_STYLE;} 192 | 193 | } 194 | 195 | 196 | // normal comment mode 197 | 198 | { 199 | 200 | // comment unterminated 201 | 202 | ([^\n*]|\*+[^\n*/])* (\n | \*+\n) { return JAVA_COMMENT_STYLE; } 203 | 204 | // comment terminated 205 | 206 | ([^\n*]|\*+[^\n*/])* \*+ "/" { yybegin(YYINITIAL); return JAVA_COMMENT_STYLE; } 207 | 208 | } 209 | 210 | /* error fallback */ 211 | 212 | .|\n { return PLAIN_STYLE; } 213 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/renderer/CppXhtmlRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2006 Arnout Engelen 3 | * Copyright 2004-2006 Geert Bevin 4 | * Distributed under the terms of either: 5 | * - the common development and distribution license (CDDL), v1.0; or 6 | * - the GNU Lesser General Public License, v2.1 or later 7 | * $Id: CppXhtmlRenderer.java 3108 2006-03-13 18:03:00Z gbevin $ 8 | */ 9 | package org.codelibs.jhighlight.renderer; 10 | 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | import org.codelibs.jhighlight.highlighter.CppHighlighter; 15 | import org.codelibs.jhighlight.highlighter.ExplicitStateHighlighter; 16 | 17 | /** 18 | * Generates highlighted syntax in XHTML from Cpp source. 19 | * 20 | * @author Arnout Engelen (arnouten[remove] at bzzt dot net) 21 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 22 | * @version $Revision: 0$ 23 | */ 24 | public class CppXhtmlRenderer extends XhtmlRenderer 25 | { 26 | public final static HashMap DEFAULT_CSS = new HashMap() {{ 27 | put("h1", 28 | "font-family: sans-serif; " + 29 | "font-size: 16pt; " + 30 | "font-weight: bold; " + 31 | "color: rgb(0,0,0); " + 32 | "background: rgb(210,210,210); " + 33 | "border: solid 1px black; " + 34 | "padding: 5px; " + 35 | "text-align: center;"); 36 | 37 | put("code", 38 | "color: rgb(0,0,0); " + 39 | "font-family: monospace; " + 40 | "font-size: 12px; " + 41 | "white-space: nowrap;"); 42 | 43 | put(".cpp_plain", 44 | "color: rgb(0,0,0);"); 45 | 46 | put(".cpp_keyword", 47 | "color: rgb(0,0,0); " + 48 | "font-weight: bold;"); 49 | 50 | put(".cpp_type", 51 | "color: rgb(0,44,221);"); 52 | 53 | put(".cpp_operator", 54 | "color: rgb(0,124,31);"); 55 | 56 | put(".cpp_separator", 57 | "color: rgb(0,33,255);"); 58 | 59 | put(".cpp_literal", 60 | "color: rgb(188,0,0);"); 61 | 62 | put(".cpp_comment", 63 | "color: rgb(147,147,147); " + 64 | "background-color: rgb(247,247,247);"); 65 | 66 | put(".cpp_doxygen_comment", 67 | "color: rgb(147,147,147); " + 68 | "background-color: rgb(247,247,247); " + 69 | "font-style: italic;"); 70 | 71 | put(".cpp_doxygen_tag", 72 | "color: rgb(147,147,147); " + 73 | "background-color: rgb(247,247,247); " + 74 | "font-style: italic; " + 75 | "font-weight: bold;"); 76 | 77 | put(".cpp_preproc", 78 | "color: purple;"); 79 | }}; 80 | 81 | protected Map getDefaultCssStyles() 82 | { 83 | return DEFAULT_CSS; 84 | } 85 | 86 | protected String getCssClass(int style) 87 | { 88 | switch (style) 89 | { 90 | case CppHighlighter.PLAIN_STYLE: 91 | return "cpp_plain"; 92 | case CppHighlighter.KEYWORD_STYLE: 93 | return "cpp_keyword"; 94 | case CppHighlighter.TYPE_STYLE: 95 | return "cpp_type"; 96 | case CppHighlighter.OPERATOR_STYLE: 97 | return "cpp_operator"; 98 | case CppHighlighter.SEPARATOR_STYLE: 99 | return "cpp_separator"; 100 | case CppHighlighter.LITERAL_STYLE: 101 | return "cpp_literal"; 102 | case CppHighlighter.CPP_COMMENT_STYLE: 103 | return "cpp_comment"; 104 | case CppHighlighter.DOXYGEN_COMMENT_STYLE: 105 | return "cpp_doxygen_comment"; 106 | case CppHighlighter.DOXYGEN_TAG_STYLE: 107 | return "cpp_doxygen_tag"; 108 | case CppHighlighter.PREPROC_STYLE: 109 | return "cpp_preproc"; 110 | } 111 | 112 | return null; 113 | } 114 | 115 | protected ExplicitStateHighlighter getHighlighter() 116 | { 117 | return new CppHighlighter(); 118 | } 119 | } 120 | 121 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/renderer/GroovyXhtmlRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: GroovyXhtmlRenderer.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.renderer; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import org.codelibs.jhighlight.highlighter.ExplicitStateHighlighter; 14 | import org.codelibs.jhighlight.highlighter.GroovyHighlighter; 15 | 16 | /** 17 | * Generates highlighted syntax in XHTML from Groovy source. 18 | * 19 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 20 | * @version $Revision: 3108 $ 21 | * @since 1.0 22 | */ 23 | public class GroovyXhtmlRenderer extends XhtmlRenderer 24 | { 25 | public final static HashMap DEFAULT_CSS = new HashMap() {{ 26 | put("h1", 27 | "font-family: sans-serif; " + 28 | "font-size: 16pt; " + 29 | "font-weight: bold; " + 30 | "color: rgb(0,0,0); " + 31 | "background: rgb(210,210,210); " + 32 | "border: solid 1px black; " + 33 | "padding: 5px; " + 34 | "text-align: center;"); 35 | 36 | put("code", 37 | "color: rgb(0,0,0); " + 38 | "font-family: monospace; " + 39 | "font-size: 12px; " + 40 | "white-space: nowrap;"); 41 | 42 | put(".java_plain", 43 | "color: rgb(0,0,0);"); 44 | 45 | put(".java_keyword", 46 | "color: rgb(0,0,0); " + 47 | "font-weight: bold;"); 48 | 49 | put(".java_type", 50 | "color: rgb(0,44,221);"); 51 | 52 | put(".java_operator", 53 | "color: rgb(0,124,31);"); 54 | 55 | put(".java_separator", 56 | "color: rgb(0,33,255);"); 57 | 58 | put(".java_literal", 59 | "color: rgb(188,0,0);"); 60 | 61 | put(".java_comment", 62 | "color: rgb(147,147,147); " + 63 | "background-color: rgb(247,247,247);"); 64 | 65 | put(".java_javadoc_comment", 66 | "color: rgb(147,147,147); " + 67 | "background-color: rgb(247,247,247); " + 68 | "font-style: italic;"); 69 | 70 | put(".java_javadoc_tag", 71 | "color: rgb(147,147,147); " + 72 | "background-color: rgb(247,247,247); " + 73 | "font-style: italic; " + 74 | "font-weight: bold;"); 75 | }}; 76 | 77 | protected Map getDefaultCssStyles() 78 | { 79 | return DEFAULT_CSS; 80 | } 81 | 82 | protected String getCssClass(int style) 83 | { 84 | switch (style) 85 | { 86 | case GroovyHighlighter.PLAIN_STYLE: 87 | return "java_plain"; 88 | case GroovyHighlighter.KEYWORD_STYLE: 89 | return "java_keyword"; 90 | case GroovyHighlighter.TYPE_STYLE: 91 | return "java_type"; 92 | case GroovyHighlighter.OPERATOR_STYLE: 93 | return "java_operator"; 94 | case GroovyHighlighter.SEPARATOR_STYLE: 95 | return "java_separator"; 96 | case GroovyHighlighter.LITERAL_STYLE: 97 | return "java_literal"; 98 | case GroovyHighlighter.JAVA_COMMENT_STYLE: 99 | return "java_comment"; 100 | case GroovyHighlighter.JAVADOC_COMMENT_STYLE: 101 | return "java_javadoc_comment"; 102 | case GroovyHighlighter.JAVADOC_TAG_STYLE: 103 | return "java_javadoc_tag"; 104 | } 105 | 106 | return null; 107 | } 108 | 109 | protected ExplicitStateHighlighter getHighlighter() 110 | { 111 | return new GroovyHighlighter(); 112 | } 113 | } 114 | 115 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/renderer/JavaScriptXhtmlRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin . 3 | * Ulf Dittmer (ulf[remove] at ulfdittmer dot com) 4 | * Distributed under the terms of either: 5 | * - the common development and distribution license (CDDL), v1.0; or 6 | * - the GNU Lesser General Public License, v2.1 or later 7 | * $Id: JavaScriptXhtmlRenderer.java 3431 2006-08-02 04:09:28Z gbevin $ 8 | */ 9 | package org.codelibs.jhighlight.renderer; 10 | 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | 14 | import org.codelibs.jhighlight.highlighter.ExplicitStateHighlighter; 15 | import org.codelibs.jhighlight.highlighter.JavaScriptHighlighter; 16 | 17 | /** 18 | * Generates highlighted syntax in XHTML from Java source. 19 | * 20 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 21 | * @author Ulf Dittmer (ulf[remove] at ulfdittmer dot com) 22 | * @version $Revision: 3431 $ 23 | * @since 1.1 24 | */ 25 | public class JavaScriptXhtmlRenderer extends XhtmlRenderer 26 | { 27 | public final static HashMap DEFAULT_CSS = new HashMap() {{ 28 | put("h1", 29 | "font-family: sans-serif; " + 30 | "font-size: 16pt; " + 31 | "font-weight: bold; " + 32 | "color: rgb(0,0,0); " + 33 | "background: rgb(210,210,210); " + 34 | "border: solid 1px black; " + 35 | "padding: 5px; " + 36 | "text-align: center;"); 37 | 38 | put("code", 39 | "color: rgb(0,0,0); " + 40 | "font-family: monospace; " + 41 | "font-size: 12px; " + 42 | "white-space: nowrap;"); 43 | 44 | put(".java_plain", 45 | "color: rgb(0,0,0);"); 46 | 47 | put(".java_keyword", 48 | "color: rgb(0,0,0); " + 49 | "font-weight: bold;"); 50 | 51 | put(".java_type", 52 | "color: rgb(0,44,221);"); 53 | 54 | put(".java_operator", 55 | "color: rgb(0,124,31);"); 56 | 57 | put(".java_separator", 58 | "color: rgb(0,33,255);"); 59 | 60 | put(".java_literal", 61 | "color: rgb(188,0,0);"); 62 | 63 | put(".java_comment", 64 | "color: rgb(147,147,147); " + 65 | "background-color: rgb(247,247,247);"); 66 | 67 | put(".java_javadoc_comment", 68 | "color: rgb(147,147,147); " + 69 | "background-color: rgb(247,247,247); " + 70 | "font-style: italic;"); 71 | 72 | put(".java_javadoc_tag", 73 | "color: rgb(147,147,147); " + 74 | "background-color: rgb(247,247,247); " + 75 | "font-style: italic; " + 76 | "font-weight: bold;"); 77 | }}; 78 | 79 | protected Map getDefaultCssStyles() 80 | { 81 | return DEFAULT_CSS; 82 | } 83 | 84 | protected String getCssClass(int style) 85 | { 86 | switch (style) 87 | { 88 | case JavaScriptHighlighter.PLAIN_STYLE: 89 | return "java_plain"; 90 | case JavaScriptHighlighter.KEYWORD_STYLE: 91 | return "java_keyword"; 92 | case JavaScriptHighlighter.OPERATOR_STYLE: 93 | return "java_operator"; 94 | case JavaScriptHighlighter.SEPARATOR_STYLE: 95 | return "java_separator"; 96 | case JavaScriptHighlighter.LITERAL_STYLE: 97 | return "java_literal"; 98 | case JavaScriptHighlighter.JAVA_COMMENT_STYLE: 99 | return "java_comment"; 100 | } 101 | 102 | return null; 103 | } 104 | 105 | protected ExplicitStateHighlighter getHighlighter() 106 | { 107 | return new JavaScriptHighlighter(); 108 | } 109 | } 110 | 111 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/renderer/JavaXhtmlRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: JavaXhtmlRenderer.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.renderer; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import org.codelibs.jhighlight.highlighter.ExplicitStateHighlighter; 14 | import org.codelibs.jhighlight.highlighter.JavaHighlighter; 15 | 16 | /** 17 | * Generates highlighted syntax in XHTML from Java source. 18 | * 19 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 20 | * @version $Revision: 3108 $ 21 | * @since 1.0 22 | */ 23 | public class JavaXhtmlRenderer extends XhtmlRenderer 24 | { 25 | public final static HashMap DEFAULT_CSS = new HashMap() {{ 26 | put("h1", 27 | "font-family: sans-serif; " + 28 | "font-size: 16pt; " + 29 | "font-weight: bold; " + 30 | "color: rgb(0,0,0); " + 31 | "background: rgb(210,210,210); " + 32 | "border: solid 1px black; " + 33 | "padding: 5px; " + 34 | "text-align: center;"); 35 | 36 | put("code", 37 | "color: rgb(0,0,0); " + 38 | "font-family: monospace; " + 39 | "font-size: 12px; " + 40 | "white-space: nowrap;"); 41 | 42 | put(".java_plain", 43 | "color: rgb(0,0,0);"); 44 | 45 | put(".java_keyword", 46 | "color: rgb(0,0,0); " + 47 | "font-weight: bold;"); 48 | 49 | put(".java_type", 50 | "color: rgb(0,44,221);"); 51 | 52 | put(".java_operator", 53 | "color: rgb(0,124,31);"); 54 | 55 | put(".java_separator", 56 | "color: rgb(0,33,255);"); 57 | 58 | put(".java_literal", 59 | "color: rgb(188,0,0);"); 60 | 61 | put(".java_comment", 62 | "color: rgb(147,147,147); " + 63 | "background-color: rgb(247,247,247);"); 64 | 65 | put(".java_javadoc_comment", 66 | "color: rgb(147,147,147); " + 67 | "background-color: rgb(247,247,247); " + 68 | "font-style: italic;"); 69 | 70 | put(".java_javadoc_tag", 71 | "color: rgb(147,147,147); " + 72 | "background-color: rgb(247,247,247); " + 73 | "font-style: italic; " + 74 | "font-weight: bold;"); 75 | }}; 76 | 77 | protected Map getDefaultCssStyles() 78 | { 79 | return DEFAULT_CSS; 80 | } 81 | 82 | protected String getCssClass(int style) 83 | { 84 | switch (style) 85 | { 86 | case JavaHighlighter.PLAIN_STYLE: 87 | return "java_plain"; 88 | case JavaHighlighter.KEYWORD_STYLE: 89 | return "java_keyword"; 90 | case JavaHighlighter.TYPE_STYLE: 91 | return "java_type"; 92 | case JavaHighlighter.OPERATOR_STYLE: 93 | return "java_operator"; 94 | case JavaHighlighter.SEPARATOR_STYLE: 95 | return "java_separator"; 96 | case JavaHighlighter.LITERAL_STYLE: 97 | return "java_literal"; 98 | case JavaHighlighter.JAVA_COMMENT_STYLE: 99 | return "java_comment"; 100 | case JavaHighlighter.JAVADOC_COMMENT_STYLE: 101 | return "java_javadoc_comment"; 102 | case JavaHighlighter.JAVADOC_TAG_STYLE: 103 | return "java_javadoc_tag"; 104 | } 105 | 106 | return null; 107 | } 108 | 109 | protected ExplicitStateHighlighter getHighlighter() 110 | { 111 | JavaHighlighter highlighter = new JavaHighlighter(); 112 | highlighter.ASSERT_IS_KEYWORD = true; 113 | 114 | return highlighter; 115 | } 116 | } 117 | 118 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/renderer/Renderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: Renderer.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.renderer; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.io.OutputStream; 13 | 14 | /** 15 | * Provides interface to render the source code highlighting. 16 | * 17 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 18 | * @version $Revision: 3108 $ 19 | * @since 1.0 20 | */ 21 | public interface Renderer extends com.uwyn.jhighlight.renderer.Renderer 22 | { 23 | /** 24 | * Transforms source code that's provided through an 25 | * InputStream to highlighted syntax and writes it back to 26 | * an OutputStream. 27 | * 28 | * @param name The name of the source file. 29 | * @param in The input stream that provides the source code that needs to 30 | * be transformed. 31 | * @param out The output stream to which to result should be written. 32 | * @param encoding The encoding that will be used to read and write the 33 | * text. 34 | * @param fragment true if the result should be a fragment; 35 | * or false if it should be a complete document 36 | * @see #highlight(String, String, String, boolean) 37 | * @since 1.0 38 | */ 39 | public void highlight(String name, InputStream in, OutputStream out, String encoding, boolean fragment) throws IOException; 40 | 41 | /** 42 | * Transforms source code that's provided through a 43 | * String to highlighted syntax and returns it as a 44 | * String. 45 | * 46 | * @param name The name of the source file. 47 | * @param in The input string that provides the source code that needs to 48 | * be transformed. 49 | * @param encoding The encoding that will be used to read and write the 50 | * text. 51 | * @param fragment true if the result should be a fragment; 52 | * or false if it should be a complete document 53 | * @return the highlighted source code as a string 54 | * @see #highlight(String, InputStream, OutputStream, String, boolean) 55 | * @since 1.0 56 | */ 57 | public String highlight(String name, String in, String encoding, boolean fragment) throws IOException; 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/renderer/XhtmlRendererFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: XhtmlRendererFactory.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.renderer; 9 | 10 | import java.util.Collections; 11 | import java.util.HashMap; 12 | import java.util.Map; 13 | import java.util.Set; 14 | 15 | /** 16 | * Provides a single point of entry to instantiate Xhtml renderers. 17 | * 18 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 19 | * @version $Revision: 3108 $ 20 | * @since 1.0 21 | */ 22 | public abstract class XhtmlRendererFactory 23 | { 24 | public final static String GROOVY = "groovy"; 25 | public final static String JAVA = "java"; 26 | public final static String BEANSHELL = "beanshell"; 27 | public final static String BSH = "bsh"; 28 | public final static String XML = "xml"; 29 | public final static String XHTML = "xhtml"; 30 | public final static String LZX = "lzx"; 31 | public final static String HTML = "html"; 32 | public final static String CPP = "cpp"; 33 | public final static String CXX = "cxx"; 34 | public final static String CPLUSPLUS = "c++"; 35 | 36 | private final static Map RENDERERS_CLASSNAMES = new HashMap() {{ 37 | put(GROOVY, GroovyXhtmlRenderer.class.getName()); 38 | put(JAVA, JavaXhtmlRenderer.class.getName()); 39 | put(BEANSHELL, JavaXhtmlRenderer.class.getName()); 40 | put(BSH, JavaXhtmlRenderer.class.getName()); 41 | put(XML, XmlXhtmlRenderer.class.getName()); 42 | put(XHTML, XmlXhtmlRenderer.class.getName()); 43 | put(LZX, XmlXhtmlRenderer.class.getName()); 44 | put(HTML, XmlXhtmlRenderer.class.getName()); 45 | put(CPP, CppXhtmlRenderer.class.getName()); 46 | put(CXX, CppXhtmlRenderer.class.getName()); 47 | put(CPLUSPLUS, CppXhtmlRenderer.class.getName()); 48 | }}; 49 | 50 | /** 51 | * Instantiates an instance of a known XhtmlRenderer according to 52 | * the type that's provided. 53 | * 54 | * @param type The type of renderer, look at the static variables of this 55 | * class to see which ones are supported. 56 | * @return an instance of the XhtmlRenderer that corresponds to the type; or 57 | *

null if the type wasn't known 58 | * @since 1.0 59 | */ 60 | public static Renderer getRenderer(String type) 61 | { 62 | String classname = (String)RENDERERS_CLASSNAMES.get(type.toLowerCase()); 63 | if (null == classname) 64 | { 65 | return null; 66 | } 67 | 68 | try 69 | { 70 | Class klass = Class.forName(classname); 71 | return (Renderer)klass.newInstance(); 72 | } 73 | catch (Exception e) 74 | { 75 | throw new RuntimeException(e); 76 | } 77 | } 78 | 79 | /** 80 | * Returned a set with all the supported XHTML renderer types. 81 | * 82 | * @return a Set with the supported XHTML renderer types as strings. 83 | * @since 1.0 84 | */ 85 | public static Set getSupportedTypes() 86 | { 87 | return Collections.unmodifiableSet(RENDERERS_CLASSNAMES.keySet()); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/renderer/XmlXhtmlRenderer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: XmlXhtmlRenderer.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.renderer; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import org.codelibs.jhighlight.highlighter.ExplicitStateHighlighter; 14 | import org.codelibs.jhighlight.highlighter.XmlHighlighter; 15 | 16 | /** 17 | * Generates highlighted syntax in XHTML from XML source. 18 | *

RIFE template tags are also 19 | * supported and will be clearly highlighted. 20 | * 21 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 22 | * @version $Revision: 3108 $ 23 | * @since 1.0 24 | */ 25 | public class XmlXhtmlRenderer extends XhtmlRenderer 26 | { 27 | public final static HashMap DEFAULT_CSS = new HashMap() {{ 28 | put("h1", 29 | "font-family: sans-serif; "+ 30 | "font-size: 16pt; "+ 31 | "font-weight: bold; "+ 32 | "color: rgb(0,0,0); "+ 33 | "background: rgb(210,210,210); "+ 34 | "border: solid 1px black; "+ 35 | "padding: 5px; "+ 36 | "text-align: center;"); 37 | 38 | put("code", 39 | "color: rgb(0,0,0); "+ 40 | "font-family: monospace; "+ 41 | "font-size: 12px; " + 42 | "white-space: nowrap;"); 43 | 44 | put(".xml_plain", 45 | "color: rgb(0,0,0);"); 46 | 47 | put(".xml_char_data", 48 | "color: rgb(0,0,0);"); 49 | 50 | put(".xml_tag_symbols", 51 | "color: rgb(0,59,255);"); 52 | 53 | put(".xml_comment", 54 | "color: rgb(147,147,147); "+ 55 | "background-color: rgb(247,247,247);"); 56 | 57 | put(".xml_attribute_value", 58 | "color: rgb(193,0,0);"); 59 | 60 | put(".xml_attribute_name", 61 | "color: rgb(0,0,0); "+ 62 | "font-weight: bold;"); 63 | 64 | put(".xml_processing_instruction", 65 | "color: rgb(0,0,0); "+ 66 | "font-weight: bold; "+ 67 | "font-style: italic;"); 68 | 69 | put(".xml_tag_name", 70 | "color: rgb(0,55,255);"); 71 | 72 | put(".xml_rife_tag", 73 | "color: rgb(0,0,0); "+ 74 | "background-color: rgb(228,230,160);"); 75 | 76 | put(".xml_rife_name", 77 | "color: rgb(0,0,196); "+ 78 | "background-color: rgb(228,230,160);"); 79 | }}; 80 | 81 | protected Map getDefaultCssStyles() 82 | { 83 | return DEFAULT_CSS; 84 | } 85 | 86 | protected String getCssClass(int style) 87 | { 88 | switch (style) 89 | { 90 | case XmlHighlighter.PLAIN_STYLE: 91 | return "xml_plain"; 92 | case XmlHighlighter.CHAR_DATA: 93 | return "xml_char_data"; 94 | case XmlHighlighter.TAG_SYMBOLS: 95 | return "xml_tag_symbols"; 96 | case XmlHighlighter.COMMENT: 97 | return "xml_comment"; 98 | case XmlHighlighter.ATTRIBUTE_VALUE: 99 | return "xml_attribute_value"; 100 | case XmlHighlighter.ATTRIBUTE_NAME: 101 | return "xml_attribute_name"; 102 | case XmlHighlighter.PROCESSING_INSTRUCTION: 103 | return "xml_processing_instruction"; 104 | case XmlHighlighter.TAG_NAME: 105 | return "xml_tag_name"; 106 | case XmlHighlighter.RIFE_TAG: 107 | return "xml_rife_tag"; 108 | case XmlHighlighter.RIFE_NAME: 109 | return "xml_rife_name"; 110 | } 111 | 112 | return null; 113 | } 114 | 115 | protected ExplicitStateHighlighter getHighlighter() 116 | { 117 | return new XmlHighlighter(); 118 | } 119 | } 120 | 121 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/servlet/HighlightFilter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2004-2006 Geert Bevin ; and 3 | * Frederic Daoud 4 | * Distributed under the terms of either: 5 | * - the common development and distribution license (CDDL), v1.0; or 6 | * - the GNU Lesser General Public License, v2.1 or later 7 | * $Id: HighlightFilter.java 3183 2006-04-13 21:06:25Z gbevin $ 8 | */ 9 | package org.codelibs.jhighlight.servlet; 10 | 11 | import java.io.ByteArrayInputStream; 12 | import java.io.IOException; 13 | import java.io.InputStream; 14 | import java.io.OutputStream; 15 | 16 | import javax.servlet.Filter; 17 | import javax.servlet.FilterChain; 18 | import javax.servlet.FilterConfig; 19 | import javax.servlet.ServletException; 20 | import javax.servlet.ServletOutputStream; 21 | import javax.servlet.ServletRequest; 22 | import javax.servlet.ServletResponse; 23 | import javax.servlet.http.HttpServletRequest; 24 | import javax.servlet.http.HttpServletRequestWrapper; 25 | import javax.servlet.http.HttpServletResponse; 26 | import javax.servlet.http.HttpServletResponseWrapper; 27 | 28 | import org.apache.commons.io.output.ByteArrayOutputStream; 29 | import org.codelibs.jhighlight.renderer.Renderer; 30 | import org.codelibs.jhighlight.renderer.XhtmlRendererFactory; 31 | import org.codelibs.jhighlight.tools.FileUtils; 32 | 33 | /** 34 | * A servlet filter that offers on-the-fly syntax highlighting for Java, HTML, 35 | * XHTML, XML and LZX files. 36 | *

The filter should be declared in a similar fashion as this: 37 | *

<filter>
 38 |  *    <filter-name>jhighlight</filter-name>
 39 |  *    <filter-class>org.codelibs.jhighlight.servlet.HighlightFilter</filter-class>
 40 |  *</filter>
 41 |  *
 42 |  *<filter-mapping>
 43 |  *    <filter-name>jhighlight</filter-name>
 44 |  *    <url-pattern>/*</url-pattern>
 45 |  *</filter-mapping>
46 | *

It will respond to files with the following extensions: 47 | * .javas, .htmls, .htms, 48 | * .xhtmls, .xmls and .lzxs. These will 49 | * be automatically mapped to files without the last s in the 50 | * filenames. Thus, for example, a request like this: 51 | *

http://myhost.com/folder/MySource.javas
52 | *

will retrieve this file: 53 | *

http://myhost.com/folder/MySource.java
54 | *

The contents of this file will be automatically highlighted and the 55 | * resulting HTML will be served. 56 | * 57 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 58 | * @version $Revision: 3183 $ 59 | * @since 1.0 60 | */ 61 | public final class HighlightFilter implements Filter 62 | { 63 | public void init(FilterConfig filterConfig) 64 | { 65 | } 66 | 67 | public void destroy() 68 | { 69 | } 70 | 71 | public void doFilter(ServletRequest request, 72 | ServletResponse response, FilterChain chain) 73 | throws IOException, ServletException 74 | { 75 | if (request instanceof HttpServletRequest && 76 | response instanceof HttpServletResponse) 77 | { 78 | HttpServletRequest http_request = (HttpServletRequest)request; 79 | HttpServletResponse http_response = (HttpServletResponse)response; 80 | 81 | Renderer renderer = null; 82 | String uri = http_request.getRequestURI(); 83 | String extension = FileUtils.getExtension(uri); 84 | if (extension != null && 85 | extension.endsWith("s")) 86 | { 87 | renderer = XhtmlRendererFactory.getRenderer(extension.substring(0, extension.length()-1)); 88 | } 89 | 90 | if (renderer != null) 91 | { 92 | SourceRequestWrapper request_wrapper = new SourceRequestWrapper(http_request); 93 | CharResponseWrapper response_wrapper = new CharResponseWrapper(http_response); 94 | 95 | chain.doFilter(request_wrapper, response_wrapper); 96 | 97 | OutputStream out = response.getOutputStream(); 98 | try 99 | { 100 | if (HttpServletResponse.SC_OK == response_wrapper.getStatus()) 101 | { 102 | InputStream is = new ByteArrayInputStream(response_wrapper.getWrappedOutputStream().toByteArray()); 103 | try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) 104 | { 105 | String encoding = request.getCharacterEncoding(); 106 | 107 | if (null == encoding) 108 | { 109 | encoding = "UTF-8"; 110 | } 111 | 112 | String name = http_request.getServletPath().substring(1); 113 | name = name.substring(0, name.length() - 1); 114 | renderer.highlight(name, is, os, encoding, false); 115 | 116 | String highlighted = os.toString("ISO-8859-1"); 117 | 118 | response.setContentType("text/html"); 119 | response.setContentLength(highlighted.length()); 120 | out.write(highlighted.getBytes("ISO-8859-1")); 121 | } 122 | } 123 | else 124 | { 125 | out.write(response_wrapper.getWrappedOutputStream().toByteArray()); 126 | } 127 | } 128 | finally 129 | { 130 | out.close(); 131 | } 132 | } 133 | else 134 | { 135 | chain.doFilter(request, response); 136 | } 137 | } 138 | else 139 | { 140 | chain.doFilter(request, response); 141 | } 142 | } 143 | 144 | private static class SourceRequestWrapper extends HttpServletRequestWrapper 145 | { 146 | public SourceRequestWrapper(HttpServletRequest request) 147 | { 148 | super(request); 149 | } 150 | 151 | public String getServletPath() 152 | { 153 | String path = super.getServletPath(); 154 | return path.substring(0, path.length() - 1); 155 | } 156 | 157 | public String getPathTranslated() 158 | { 159 | String path = super.getPathTranslated(); 160 | return path.substring(0, path.length() - 1); 161 | } 162 | 163 | public String getRequestURI() 164 | { 165 | String uri = super.getRequestURI(); 166 | return uri.substring(0, uri.length() - 1); 167 | } 168 | 169 | public StringBuffer getRequestURL() 170 | { 171 | StringBuffer url = super.getRequestURL(); 172 | url.setLength(url.length() - 1); 173 | return url; 174 | } 175 | } 176 | 177 | private static class CharResponseWrapper extends HttpServletResponseWrapper 178 | { 179 | private ServletOutputStreamWrapper mOutput; 180 | private int mStatus = HttpServletResponse.SC_OK; 181 | 182 | public ServletOutputStreamWrapper getWrappedOutputStream() 183 | { 184 | return mOutput; 185 | } 186 | 187 | public CharResponseWrapper(HttpServletResponse response) 188 | { 189 | super(response); 190 | 191 | mOutput = new ServletOutputStreamWrapper(); 192 | } 193 | 194 | public ServletOutputStream getOutputStream() 195 | throws IOException 196 | { 197 | return mOutput; 198 | } 199 | 200 | public void setStatus(int status) 201 | { 202 | mStatus = status; 203 | 204 | super.setStatus(status); 205 | } 206 | 207 | public void sendError(int status, String msg) 208 | throws IOException 209 | { 210 | mStatus = status; 211 | 212 | super.sendError(status, msg); 213 | } 214 | 215 | public void sendError(int status) 216 | throws IOException 217 | { 218 | mStatus = status; 219 | 220 | super.sendError(status); 221 | } 222 | 223 | public int getStatus() 224 | { 225 | return mStatus; 226 | } 227 | } 228 | 229 | private static class ServletOutputStreamWrapper extends ServletOutputStream 230 | { 231 | protected ByteArrayOutputStream mOutput; 232 | 233 | public ServletOutputStreamWrapper() 234 | { 235 | mOutput = new ByteArrayOutputStream(); 236 | } 237 | 238 | public void write(int b) throws IOException 239 | { 240 | mOutput.write(b); 241 | } 242 | 243 | public byte[] toByteArray() 244 | { 245 | return mOutput.toByteArray(); 246 | } 247 | } 248 | } 249 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/tools/ExceptionUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: ExceptionUtils.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.tools; 9 | 10 | import java.io.PrintWriter; 11 | import java.io.StringWriter; 12 | 13 | /** 14 | * Collection of utility methods to work with exceptions. 15 | * 16 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 17 | * @version $Revision: 3108 $ 18 | * @since 1.0 19 | */ 20 | public abstract class ExceptionUtils 21 | { 22 | private ExceptionUtils() 23 | { 24 | } 25 | 26 | /** 27 | * Obtains the entire stracktrace of an exception and converts it into a 28 | * string. 29 | * 30 | * @param exception the exception whose stacktrace has to be converted 31 | * @return the stracktrace, converted into a string 32 | * @since 1.0 33 | */ 34 | public static String getExceptionStackTrace(Throwable exception) 35 | { 36 | if (null == exception) throw new IllegalArgumentException("exception can't be null;"); 37 | 38 | String stack_trace = null; 39 | 40 | StringWriter string_writer = new StringWriter(); 41 | PrintWriter print_writer = new PrintWriter(string_writer); 42 | 43 | exception.printStackTrace(print_writer); 44 | 45 | stack_trace = string_writer.getBuffer().toString(); 46 | 47 | print_writer.close(); 48 | 49 | try 50 | { 51 | string_writer.close(); 52 | } 53 | // JDK 1.2.2 compatibility 54 | catch (Throwable e2) 55 | { 56 | } 57 | 58 | return stack_trace; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/tools/FileUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: FileUtils.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.tools; 9 | 10 | import java.io.File; 11 | import java.util.ArrayList; 12 | import java.util.Iterator; 13 | import java.util.regex.Pattern; 14 | 15 | /** 16 | * Collection of utility methods to work with files. 17 | * 18 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 19 | * @version $Revision: 3108 $ 20 | * @since 1.0 21 | */ 22 | public abstract class FileUtils 23 | { 24 | private FileUtils() 25 | { 26 | } 27 | 28 | /** 29 | * Recursively traverse a directory hierachy and obtain a list of all 30 | * absolute file names. 31 | *

Regular expression patterns can be provided to explicitly include 32 | * and exclude certain file names. 33 | * 34 | * @param file the directory whose file hierarchy will be traversed 35 | * @param included an array of regular expression patterns that will be 36 | * used to determine which files should be included; or 37 | *

null if all files should be included 38 | * @param excluded an array of regular expression patterns that will be 39 | * used to determine which files should be excluded; or 40 | *

null if no files should be excluded 41 | * @return the list of absolute file names 42 | * @since 1.0 43 | */ 44 | public static ArrayList getFileList(File file, Pattern[] included, Pattern[] excluded) 45 | { 46 | return getFileList(file, included, excluded, true); 47 | } 48 | 49 | private static ArrayList getFileList(File file, Pattern[] included, Pattern[] excluded, boolean root) 50 | { 51 | if (null == file) 52 | { 53 | return new ArrayList(); 54 | } 55 | 56 | ArrayList filelist = new ArrayList(); 57 | if (file.isDirectory()) 58 | { 59 | String[] list = file.list(); 60 | if (null != list) 61 | { 62 | String list_entry; 63 | for (int i = 0; i < list.length; i++) 64 | { 65 | list_entry = list[i]; 66 | 67 | File next_file = new File(file.getAbsolutePath() + File.separator + list_entry); 68 | ArrayList dir = getFileList(next_file, included, excluded, false); 69 | 70 | Iterator dir_it = dir.iterator(); 71 | String file_name; 72 | while (dir_it.hasNext()) 73 | { 74 | file_name = (String)dir_it.next(); 75 | 76 | if (root) 77 | { 78 | // if the file is not accepted, don't process it further 79 | if (!StringUtils.filter(file_name, included, excluded)) 80 | { 81 | continue; 82 | } 83 | 84 | } 85 | else 86 | { 87 | file_name = file.getName() + File.separator + file_name; 88 | } 89 | 90 | int filelist_size = filelist.size(); 91 | for (int j = 0; j < filelist_size; j++) 92 | { 93 | if (((String)filelist.get(j)).compareTo(file_name) > 0) 94 | { 95 | filelist.add(j, file_name); 96 | break; 97 | } 98 | } 99 | if (filelist.size() == filelist_size) 100 | { 101 | filelist.add(file_name); 102 | } 103 | } 104 | } 105 | } 106 | } 107 | else if (file.isFile()) 108 | { 109 | String file_name = file.getName(); 110 | 111 | if (root) 112 | { 113 | if (StringUtils.filter(file_name, included, excluded)) 114 | { 115 | filelist.add(file_name); 116 | } 117 | } 118 | else 119 | { 120 | filelist.add(file_name); 121 | } 122 | } 123 | 124 | return filelist; 125 | } 126 | 127 | public static String getExtension(String fileName) 128 | { 129 | if (null == fileName) throw new IllegalArgumentException("fileName can't be null."); 130 | 131 | String ext = null; 132 | 133 | int index = fileName.lastIndexOf('.'); 134 | if (index > 0 && index < fileName.length() - 1) 135 | { 136 | ext = fileName.substring(index+1).toLowerCase(); 137 | } 138 | 139 | return ext; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/main/java/org/codelibs/jhighlight/tools/exceptions/FileUtilsErrorException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2001-2006 Geert Bevin 3 | * Distributed under the terms of either: 4 | * - the common development and distribution license (CDDL), v1.0; or 5 | * - the GNU Lesser General Public License, v2.1 or later 6 | * $Id: FileUtilsErrorException.java 3108 2006-03-13 18:03:00Z gbevin $ 7 | */ 8 | package org.codelibs.jhighlight.tools.exceptions; 9 | 10 | /** 11 | * Exception that will be trigger when unexpected errors occur during the 12 | * functionalities of the {@link org.codelibs.jhighlight.tools.FileUtils} class. 13 | * 14 | * @author Geert Bevin (gbevin[remove] at uwyn dot com) 15 | * @version $Revision: 3108 $ 16 | * @since 1.0 17 | */ 18 | public class FileUtilsErrorException extends Exception 19 | { 20 | public FileUtilsErrorException(String message) 21 | { 22 | super(message); 23 | } 24 | 25 | public FileUtilsErrorException(String message, Throwable cause) 26 | { 27 | super(message, cause); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/resources/JHIGHLIGHT_VERSION: -------------------------------------------------------------------------------- 1 | 1.1 2 | -------------------------------------------------------------------------------- /src/main/resources/jhighlight.properties: -------------------------------------------------------------------------------- 1 | h1 = \ 2 | \ 3 | font-family: sans-serif; \ 4 | font-size: 16pt; \ 5 | font-weight: bold; \ 6 | color: rgb(0,0,0); \ 7 | background: rgb(210,210,210); \ 8 | border: solid 1px black; \ 9 | padding: 5px; \ 10 | text-align: center; 11 | 12 | code = \ 13 | \ 14 | color: rgb(0,0,0); \ 15 | font-family: monospace; \ 16 | font-size: 12px; \ 17 | white-space: nowrap; 18 | 19 | .java_plain = \ 20 | \ 21 | color: rgb(0,0,0); 22 | 23 | .java_keyword = \ 24 | \ 25 | color: rgb(0,0,0); \ 26 | font-weight: bold; 27 | 28 | .java_type = \ 29 | \ 30 | color: rgb(0,44,221); 31 | 32 | .java_operator = \ 33 | \ 34 | color: rgb(0,124,31); 35 | 36 | .java_separator = \ 37 | \ 38 | color: rgb(0,33,255); 39 | 40 | .java_literal = \ 41 | \ 42 | color: rgb(188,0,0); 43 | 44 | .java_comment = \ 45 | \ 46 | color: rgb(147,147,147); \ 47 | background-color: rgb(247,247,247); 48 | 49 | .java_javadoc_comment = \ 50 | \ 51 | color: rgb(147,147,147); \ 52 | background-color: rgb(247,247,247); \ 53 | font-style: italic; 54 | 55 | .java_javadoc_tag = \ 56 | \ 57 | color: rgb(147,147,147); \ 58 | background-color: rgb(247,247,247); \ 59 | font-style: italic; \ 60 | font-weight: bold; 61 | 62 | .xml_plain = \ 63 | \ 64 | color: rgb(0,0,0); 65 | 66 | .xml_char_data = \ 67 | \ 68 | color: rgb(0,0,0); 69 | 70 | .xml_tag_symbols = \ 71 | \ 72 | color: rgb(0,59,255); 73 | 74 | .xml_comment = \ 75 | \ 76 | color: rgb(147,147,147); \ 77 | background-color: rgb(247,247,247); 78 | 79 | .xml_attribute_value = \ 80 | \ 81 | color: rgb(193,0,0); 82 | 83 | .xml_attribute_name = \ 84 | \ 85 | color: rgb(0,0,0); \ 86 | font-weight: bold; 87 | 88 | .xml_processing_instruction = \ 89 | \ 90 | color: rgb(0,0,0); \ 91 | font-weight: bold; \ 92 | font-style: italic; 93 | 94 | .xml_tag_name = \ 95 | \ 96 | color: rgb(0,55,255); 97 | 98 | .xml_rife_tag = \ 99 | \ 100 | color: rgb(0,0,0); \ 101 | background-color: rgb(228,230,160); 102 | 103 | .xml_rife_name = \ 104 | \ 105 | color: rgb(0,0,196); \ 106 | background-color: rgb(228,230,160); 107 | 108 | .cpp_plain \ 109 | \ 110 | color: rgb(0,0,0); 111 | 112 | .cpp_keyword \ 113 | \ 114 | color: rgb(0,0,0); \ 115 | font-weight: bold; 116 | 117 | .cpp_type \ 118 | \ 119 | color: rgb(0,44,221); 120 | 121 | .cpp_operator \ 122 | \ 123 | color: rgb(0,124,31); 124 | 125 | .cpp_separator \ 126 | \ 127 | color: rgb(0,33,255); 128 | 129 | .cpp_literal \ 130 | \ 131 | color: rgb(188,0,0); 132 | 133 | .cpp_comment \ 134 | \ 135 | color: rgb(147,147,147); \ 136 | background-color: rgb(247,247,247); 137 | 138 | .cpp_doxygen_comment \ 139 | \ 140 | color: rgb(147,147,147); \ 141 | background-color: rgb(247,247,247); \ 142 | font-style: italic; 143 | 144 | .cpp_doxygen_tag \ 145 | \ 146 | color: rgb(147,147,147); \ 147 | background-color: rgb(247,247,247); \ 148 | font-style: italic; \ 149 | font-weight: bold; 150 | 151 | .cpp_preproc \ 152 | \ 153 | color: purple; 154 | 155 | -------------------------------------------------------------------------------- /src/test/java/org/codelibs/jhighlight/tools/StringUtilsTest.java: -------------------------------------------------------------------------------- 1 | package org.codelibs.jhighlight.tools; 2 | 3 | import static org.junit.Assert.assertEquals; 4 | import static org.junit.Assert.assertNull; 5 | 6 | import org.codelibs.jhighlight.tools.StringUtils; 7 | import org.junit.Test; 8 | 9 | public class StringUtilsTest { 10 | @Test 11 | public void encodeHtml() { 12 | assertEquals("abc", StringUtils.encodeHtml("abc")); 13 | assertEquals("a&c", StringUtils.encodeHtml("a&c")); 14 | assertEquals("<b>abc</b>", 15 | StringUtils.encodeHtml("abc")); 16 | 17 | assertNull(StringUtils.encodeHtml(null)); 18 | assertEquals("", StringUtils.encodeHtml("")); 19 | } 20 | } 21 | --------------------------------------------------------------------------------