If Code2Web is given a file as an input (as opposed to a stream), it can use the extension of the file to determine the language and grammar that must be used.
In order to handle the specifics of each language, HTMLConvert delegates some of the tasks to a "language definition class". There is one such class per language, and its role is:
You can think about this class like a bridge between HTMLConvert and the JavaCC grammar. It exists in order to minimize the changes that must be done to a JavaCC grammar in order to make it work under Code2Web.
All "language definition classes" must have a default constructor. The ones included with the standard distribution are:
| Language | Class (Fully qualified) |
| Javascript | org.code2web.lang.javascript.JavaScriptDefinition |
| HTML / XML (with JSP) | org.code2web.lang.html.HTMLDefinition |
| Java | org.code2web.lang.java.JavaDefinition |
| Text | org.code2web.lang.text.TextDefinition |
The LangBase class represents a "language database", although the name is a bit stretched. Basically, it keeps to maps:
You can register a new language by calling the addLanguage method.
lb.addLanguage(
"javascript", "org.code2web.lang.javascript.JavaScriptDefinition",
"js","javascript"
);
addLanguage takes at least two parameters : the language name and the definition class for that language. After these two parameters, you can specify the file extensions to associate to that language - as many as you want.
If you want to avoid including in all programs code to initialize the set of recognized languages, you can store the two maps in two property files : one for the extensions and one for the languages. For example:
#
# Properties file that associates file extensions to language names.
#
js = javascript
javascript = javascript
#
# Properties file that maps language names to handling classes
#
javascript = org.code2web.lang.javascript.JavaScriptDefinition
Once you have these files, you can load them into a LangBase using the loadFrom method:
lb.loadFrom("extensions.properties","languages.properties");
The HTML Converter needs to be provided with an instance of the appropriate class. If you are programming HTMLConverter directly, simply call the setLanguage method, passing an instance of the class:
conv.setLanguage( new JavaScriptDefinition() );
Or if you have a LangBase instance, you can tell it to create an instance for you:
conv.setLanguage( lb.createInstance("javascript") );
If you are using the JSP tag or the console program, you can use the languageClass option, and set it to the fully qualified name of the class to use. This allows the usage of third-party language definition classes.
<c2w:convert ... languageClass="org.code2web.lang.javascript.JavaScriptDefinition" ... />
However, if you are formatting source code written in one of the built-in languages (such as Java, JavaScript, etc.) you may find it easier to simply use tha "language" attribute:
<c2w:convert ... language="Java" ... />