KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > i18n > regexp > Translator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.i18n.regexp;
22
23 import java.util.Map JavaDoc;
24
25 /**
26  * Translator of Apache's Regexp regular expressions to JDK's regular
27  * expressions.
28  *
29  * @author Marian Petras
30  */

31 public final class Translator {
32
33     /**
34      * Translates the given Apache Regexp regular expression
35      * into a JDK's regular expression.
36      *
37      * @param regexp regular expression according to Apache's Regexp library
38      * syntax rules
39      * @return regular expression according to syntax rules of JDK's class
40      * {@link java.util.regex.Pattern Pattern}.
41      * @exception java.lang.IllegalArgumentException
42      * if the argument was <code>null</code>
43      * @exception ParseException
44      * if the given expression contained a syntax error
45      */

46     public static String JavaDoc translateRegexp(String JavaDoc regexp)
47             throws IllegalArgumentException JavaDoc, ParseException {
48         TreeNodeRoot tree = Parser.parse(regexp);
49         return Generator.generateRegexp(tree);
50     }
51
52     /**
53      * Translates the given Apache Regexp regular expression
54      * into a JDK's regular expression.
55      *
56      * @param regexp regular expression according to Apache's Regexp library
57      * syntax rules
58      * @param tokenReplacements maps token names to strings to be put in place
59      * of them, or <code>null</code> to ignore tokens
60      * (leave them unchanged)
61      * @return regular expression according to syntax rules of JDK's class
62      * {@link java.util.regex.Pattern Pattern}.
63      * @exception java.lang.IllegalArgumentException
64      * if the regular expression is <code>null</code>
65      * @exception java.lang.ClassCastException
66      * if not all keys in the map were strings
67      * @exception ParseException
68      * if the given expression contained a syntax error
69      */

70     public static String JavaDoc translateRegexp(String JavaDoc regexp,
71                                          Map JavaDoc tokenReplacements)
72             throws IllegalArgumentException JavaDoc, ParseException {
73
74         if ((tokenReplacements == null) || (tokenReplacements.isEmpty())) {
75             return translateRegexp(regexp);
76         }
77
78         String JavaDoc[] tokenNames = new String JavaDoc[tokenReplacements.size()];
79         try {
80             tokenReplacements.keySet().toArray(tokenNames);
81         } catch (ArrayStoreException JavaDoc ex) {
82             throw new ClassCastException JavaDoc();
83         }
84         TreeNodeRoot tree = Parser.parse(regexp, tokenNames);
85         return Generator.generateRegexp(tree, tokenReplacements);
86     }
87
88 }
89
Popular Tags