Search in sources :

Example 21 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.

the class StringCoding method decode.

static char[] decode(String charsetName, byte[] ba, int off, int len) throws UnsupportedEncodingException {
    StringDecoder sd = deref(decoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((sd == null) || !(csn.equals(sd.requestedCharsetName()) || csn.equals(sd.charsetName()))) {
        sd = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                sd = new StringDecoder(cs, csn);
        } catch (IllegalCharsetNameException x) {
        }
        if (sd == null)
            throw new UnsupportedEncodingException(csn);
        set(decoder, sd);
    }
    return sd.decode(ba, off, len);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) HistoricallyNamedCharset(sun.nio.cs.HistoricallyNamedCharset) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 22 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project jdk8u_jdk by JetBrains.

the class StringCoding method encode.

static byte[] encode(String charsetName, char[] ca, int off, int len) throws UnsupportedEncodingException {
    StringEncoder se = deref(encoder);
    String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
    if ((se == null) || !(csn.equals(se.requestedCharsetName()) || csn.equals(se.charsetName()))) {
        se = null;
        try {
            Charset cs = lookupCharset(csn);
            if (cs != null)
                se = new StringEncoder(cs, csn);
        } catch (IllegalCharsetNameException x) {
        }
        if (se == null)
            throw new UnsupportedEncodingException(csn);
        set(encoder, se);
    }
    return se.encode(ca, off, len);
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) HistoricallyNamedCharset(sun.nio.cs.HistoricallyNamedCharset) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 23 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project gerrit by GerritCodeReview.

the class Text method charset.

private static Charset charset(byte[] content, String encoding) {
    if (encoding == null) {
        UniversalDetector d = new UniversalDetector(null);
        d.handleData(content, 0, content.length);
        d.dataEnd();
        encoding = d.getDetectedCharset();
    }
    if (encoding == null) {
        return ISO_8859_1;
    }
    try {
        return Charset.forName(encoding);
    } catch (IllegalCharsetNameException err) {
        log.error("Invalid detected charset name '" + encoding + "': " + err);
        return ISO_8859_1;
    } catch (UnsupportedCharsetException err) {
        log.error("Detected charset '" + encoding + "' not supported: " + err);
        return ISO_8859_1;
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UniversalDetector(org.mozilla.universalchardet.UniversalDetector)

Example 24 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project ceylon-compiler by ceylon.

the class RecognizedOptions method getAll.

/**
     * Get all the recognized options.
     * @param helper an {@code OptionHelper} to help when processing options
     * @return an array of options
     */
public static Option[] getAll(final OptionHelper helper) {
    return new Option[] { new Option(G, "opt.g"), new Option(G_NONE, "opt.g.none") {

        @Override
        public boolean process(Options options, String option) {
            options.put("-g:", "none");
            return false;
        }
    }, new Option(G_CUSTOM, "opt.g.lines.vars.source", Option.ChoiceKind.ANYOF, "lines", "vars", "source"), new XOption(XLINT, "opt.Xlint"), new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist", Option.ChoiceKind.ANYOF, getXLintChoices()), // -nowarn is retained for command-line backward compatibility
    new Option(NOWARN, "opt.nowarn") {

        @Override
        public boolean process(Options options, String option) {
            options.put("-Xlint:none", option);
            return false;
        }
    }, new Option(VERBOSE, "opt.verbose"), new Option(VERBOSE_CUSTOM, "opt.verbose.suboptlist") {

        public boolean matches(String s) {
            return s.startsWith("-verbose:");
        }

        public boolean process(Options options, String option) {
            String suboptions = option.substring(9);
            options.put("-verbose:", suboptions);
            // enter all the -verbose suboptions as "-verbose:suboption"
            for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
                String tok = t.nextToken();
                // make sure all is an alias for --verbose
                if (tok.equals("all"))
                    options.put(VERBOSE, "true");
                String opt = "-verbose:" + tok;
                options.put(opt, opt);
            }
            return false;
        }
    }, // -deprecation is retained for command-line backward compatibility
    new Option(DEPRECATION, "opt.deprecation") {

        @Override
        public boolean process(Options options, String option) {
            options.put("-Xlint:deprecation", option);
            return false;
        }
    }, new Option(CLASSPATH, "opt.arg.path", "opt.classpath"), new Option(CP, "opt.arg.path", "opt.classpath") {

        @Override
        public boolean process(Options options, String option, String arg) {
            return super.process(options, "-classpath", arg);
        }
    }, new COption(CEYLONCWD, "opt.arg.path", "opt.ceyloncwd"), new COption(CEYLONREPO, "opt.arg.url", "opt.ceylonrepo") {

        @Override
        public boolean process(Options options, String option, String arg) {
            if (options != null)
                options.addMulti(CEYLONREPO, arg);
            return false;
        }
    }, new COption(CEYLONSYSTEMREPO, "opt.arg.url", "opt.ceylonsystemrepo"), new COption(CEYLONCACHEREPO, "opt.arg.url", "opt.ceyloncacherepo"), new COption(CEYLONNODEFREPOS, "opt.ceylonnodefrepos"), new COption(CEYLONUSER, "opt.arg.value", "opt.ceylonuser"), new COption(CEYLONPASS, "opt.arg.value", "opt.ceylonpass"), new COption(CEYLONNOOSGI, "opt.ceylonnoosgi"), new COption(CEYLONOSGIPROVIDEDBUNDLES, "opt.arg.value", "opt.ceylonosgiprovidedbundles"), new COption(CEYLONNOPOM, "opt.ceylonnopom"), new COption(CEYLONPACK200, "opt.ceylonpack200"), new COption(CEYLONRESOURCEROOT, "opt.arg.path", "opt.ceylonresourceroot"), new COption(CEYLONDISABLEOPT, "opt.ceylondisableopt"), new COption(CEYLONDISABLEOPT_CUSTOM, "opt.ceylondisableopt.suboptlist"), new COption(CEYLONSUPPRESSWARNINGS, "opt.arg.value", "opt.ceylonsuppresswarnings"), new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath") {

        @Override
        public boolean process(Options options, String option, String arg) {
            if (options != null)
                options.addMulti(SOURCEPATH, arg);
            return false;
        }
    }, new COption(CEYLONSOURCEPATH, "opt.arg.directory", "opt.ceylonsourcepath") {

        @Override
        public boolean process(Options options, String option, String arg) {
            if (options != null)
                options.addMulti(SOURCEPATH, arg);
            return false;
        }
    }, new COption(CEYLONRESOURCEPATH, "opt.arg.url", "opt.ceylonresourcepath") {

        @Override
        public boolean process(Options options, String option, String arg) {
            if (options != null)
                options.addMulti(CEYLONRESOURCEPATH, arg);
            return false;
        }
    }, new Option(BOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {

        @Override
        public boolean process(Options options, String option, String arg) {
            options.remove("-Xbootclasspath/p:");
            options.remove("-Xbootclasspath/a:");
            return super.process(options, option, arg);
        }
    }, new XOption(XBOOTCLASSPATH_PREPEND, "opt.arg.path", "opt.Xbootclasspath.p"), new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"), new XOption(XBOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {

        @Override
        public boolean process(Options options, String option, String arg) {
            options.remove("-Xbootclasspath/p:");
            options.remove("-Xbootclasspath/a:");
            return super.process(options, "-bootclasspath", arg);
        }
    }, new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"), new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs", "opt.extdirs") {

        @Override
        public boolean process(Options options, String option, String arg) {
            return super.process(options, "-extdirs", arg);
        }
    }, new Option(ENDORSEDDIRS, "opt.arg.dirs", "opt.endorseddirs"), new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs", "opt.endorseddirs") {

        @Override
        public boolean process(Options options, String option, String arg) {
            return super.process(options, "-endorseddirs", arg);
        }
    }, new Option(PROC, "opt.proc.none.only", Option.ChoiceKind.ONEOF, "none", "only"), new Option(PROCESSOR, "opt.arg.class.list", "opt.processor"), new Option(PROCESSORPATH, "opt.arg.path", "opt.processorpath"), new Option(D, "opt.arg.directory", "opt.d"), new COption(CEYLONOUT, "opt.arg.url", "opt.ceylonout") {

        @Override
        public boolean process(Options options, String option, String arg) {
            return super.process(options, "-d", arg);
        }
    }, new COption(CEYLONOFFLINE, "opt.ceylonoffline"), new COption(CEYLONTIMEOUT, "opt.arg.number", "opt.ceylontimeout"), new COption(CEYLONCONTINUE, "opt.ceyloncontinue"), new COption(CEYLONPROGRESS, "opt.ceylonprogress"), new COption(CEYLONAUTOEXPORTMAVENDEPENDENCIES, "opt.ceylonautoexportmavendependencies"), new COption(CEYLONFLATCLASSPATH, "opt.ceylonflatclasspath"), new COption(CEYLONOVERRIDES, "opt.arg.url", "opt.ceylonoverrides"), // backwards-compat
    new COption(CEYLONMAVENOVERRIDES, "opt.arg.url", "opt.ceylonoverrides") {

        @Override
        public boolean process(Options options, String option, String arg) {
            return super.process(options, "-overrides", arg);
        }
    }, new Option(S, "opt.arg.directory", "opt.sourceDest"), new Option(IMPLICIT, "opt.implicit", Option.ChoiceKind.ONEOF, "none", "class"), new Option(ENCODING, "opt.arg.encoding", "opt.encoding") {

        @Override
        public boolean process(Options options, String option, String operand) {
            try {
                Charset.forName(operand);
                options.put(option, operand);
                return false;
            } catch (UnsupportedCharsetException e) {
                helper.error("err.unsupported.encoding", operand);
                return true;
            } catch (IllegalCharsetNameException e) {
                helper.error("err.unsupported.encoding", operand);
                return true;
            }
        }
    }, new Option(SOURCE, "opt.arg.release", "opt.source") {

        @Override
        public boolean process(Options options, String option, String operand) {
            Source source = Source.lookup(operand);
            if (source == null) {
                helper.error("err.invalid.source", operand);
                return true;
            }
            return super.process(options, option, operand);
        }
    }, new Option(TARGET, "opt.arg.release", "opt.target") {

        @Override
        public boolean process(Options options, String option, String operand) {
            Target target = Target.lookup(operand);
            if (target == null) {
                helper.error("err.invalid.target", operand);
                return true;
            }
            return super.process(options, option, operand);
        }
    }, new COption(VERSION, "opt.version") {

        @Override
        public boolean process(Options options, String option) {
            helper.printVersion();
            return super.process(options, option);
        }
    }, new HiddenOption(FULLVERSION) {

        @Override
        public boolean process(Options options, String option) {
            helper.printFullVersion();
            return super.process(options, option);
        }
    }, new HiddenOption(DIAGS) {

        @Override
        public boolean process(Options options, String option) {
            Option xd = getOptions(helper, EnumSet.of(XD))[0];
            option = option.substring(option.indexOf('=') + 1);
            String diagsOption = option.contains("%") ? "-XDdiagsFormat=" : "-XDdiags=";
            diagsOption += option;
            if (xd.matches(diagsOption))
                return xd.process(options, diagsOption);
            else
                return false;
        }
    }, new COption(HELP, "opt.help") {

        @Override
        public boolean process(Options options, String option) {
            helper.printHelp();
            return super.process(options, option);
        }
    }, new Option(A, "opt.arg.key.equals.value", "opt.A") {

        @Override
        String helpSynopsis() {
            hasSuffix = true;
            return super.helpSynopsis();
        }

        @Override
        public boolean matches(String arg) {
            return arg.startsWith("-A");
        }

        @Override
        public boolean hasArg() {
            return false;
        }

        // Mapping for processor options created in
        // JavacProcessingEnvironment
        @Override
        public boolean process(Options options, String option) {
            int argLength = option.length();
            if (argLength == 2) {
                helper.error("err.empty.A.argument");
                return true;
            }
            int sepIndex = option.indexOf('=');
            String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength));
            if (!JavacProcessingEnvironment.isValidOptionName(key)) {
                helper.error("err.invalid.A.key", option);
                return true;
            }
            return process(options, option, option);
        }
    }, new Option(X, "opt.X") {

        @Override
        public boolean process(Options options, String option) {
            helper.printXhelp();
            return super.process(options, option);
        }
    }, // It's actually implemented by the launcher.
    new Option(J, "opt.arg.flag", "opt.J") {

        @Override
        String helpSynopsis() {
            hasSuffix = true;
            return super.helpSynopsis();
        }

        @Override
        public boolean process(Options options, String option) {
            throw new AssertionError("the -J flag should be caught by the launcher.");
        }
    }, // new Option("-moreinfo",                                      "opt.moreinfo") {
    new HiddenOption(MOREINFO) {

        @Override
        public boolean process(Options options, String option) {
            Type.moreInfo = true;
            return super.process(options, option);
        }
    }, // treat warnings as errors
    new Option(WERROR, "opt.Werror"), new Option(SRC, "opt.arg.src", "opt.src") {

        public boolean process(Options options, String option, String arg) {
            return super.process(options, "-src", arg);
        }
    }, // use complex inference from context in the position of a method call argument
    new HiddenOption(COMPLEXINFERENCE), // new Option("-prompt",                                        "opt.prompt"),
    new HiddenOption(PROMPT), // dump stack on error
    new HiddenOption(DOE), // new Option("-s",                                     "opt.s"),
    new HiddenOption(PRINTSOURCE), // allow us to compile ceylon.language
    new HiddenOption(BOOTSTRAPCEYLON), // display warnings for generic unchecked operations
    new HiddenOption(WARNUNCHECKED) {

        @Override
        public boolean process(Options options, String option) {
            options.put("-Xlint:unchecked", option);
            return false;
        }
    }, new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"), new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"), new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") {

        @Override
        public boolean process(Options options, String option, String arg) {
            try {
                helper.setOut(new PrintWriter(new FileWriter(arg), true));
            } catch (java.io.IOException e) {
                helper.error("err.error.writing.file", arg, e);
                return true;
            }
            return super.process(options, option, arg);
        }
    }, new XOption(XPRINT, "opt.print"), new XOption(XPRINTROUNDS, "opt.printRounds"), new XOption(XPRINTPROCESSORINFO, "opt.printProcessorInfo"), new XOption(XPREFER, "opt.prefer", Option.ChoiceKind.ONEOF, "source", "newer"), new XOption(XPKGINFO, "opt.pkginfo", Option.ChoiceKind.ONEOF, "always", "legacy", "nonempty"), /* -O is a no-op, accepted for backward compatibility. */
    new HiddenOption(O), /* -Xjcov produces tables to support the code coverage tool jcov. */
    new HiddenOption(XJCOV), /* This is a back door to the compiler's option table.
         * -XDx=y sets the option x to the value y.
         * -XDx sets the option x to the value x.
         */
    new HiddenOption(XD) {

        String s;

        @Override
        public boolean matches(String s) {
            this.s = s;
            return s.startsWith(name.optionName);
        }

        @Override
        public boolean process(Options options, String option) {
            s = s.substring(name.optionName.length());
            int eq = s.indexOf('=');
            String key = (eq < 0) ? s : s.substring(0, eq);
            String value = (eq < 0) ? s : s.substring(eq + 1);
            options.put(key, value);
            return false;
        }
    }, // It's actually implemented by the CommandLine class.
    new Option(AT, "opt.arg.file", "opt.AT") {

        @Override
        String helpSynopsis() {
            hasSuffix = true;
            return super.helpSynopsis();
        }

        @Override
        public boolean process(Options options, String option) {
            throw new AssertionError("the @ flag should be caught by CommandLine.");
        }
    }, /*
         * TODO: With apt, the matches method accepts anything if
         * -XclassAsDecls is used; code elsewhere does the lookup to
         * see if the class name is both legal and found.
         *
         * In apt, the process method adds the candidate class file
         * name to a separate list.
         */
    new HiddenOption(SOURCEFILE) {

        String s;

        @Override
        public boolean matches(String s) {
            this.s = s;
            return // Java source file
            s.endsWith(".java") || // FIXME: Should be a FileManager query
            s.endsWith(".ceylon") || // FIX for ceylon because default is not a valid name for Java
            "default".equals(s) || // Legal type name for Ceylon
            isCeylonName(s) || // Possibly a resource file
            (new File(s)).isFile();
        }

        @Override
        public boolean process(Options options, String option) {
            File f = new File(s);
            if (s.endsWith(".java") || // FIXME: Should be a FileManager query
            s.endsWith(".ceylon")) {
                // Most likely a source file
                if (!f.isFile()) {
                    // -sourcepath not -src because the COption for 
                    // CEYLONSOURCEPATH puts it in the options map as -sourcepath
                    List<String> sourcePaths = options.getMulti("-sourcepath");
                    if (sourcePaths.isEmpty())
                        sourcePaths = FileUtil.filesToPathList(DefaultToolOptions.getCompilerSourceDirs());
                    if (checkIfModule(sourcePaths, s)) {
                        // A Ceylon module name that ends with .ceylon or .java
                        helper.addClassName(s);
                        return false;
                    }
                    if (f.exists()) {
                        helper.error("err.file.not.file", f);
                        return true;
                    }
                }
                if (!f.exists()) {
                    helper.error("err.file.not.found", f);
                    return true;
                }
                helper.addFile(f);
            }
            if (f.isFile()) {
                // Most likely a resource file
                helper.addFile(f);
            } else {
                // the default module is always allowed, it doesn't need to have any folder
                if (s.equals(Module.DEFAULT_MODULE_NAME)) {
                    helper.addClassName(s);
                    return false;
                }
                // find a corresponding physical module in the source path
                List<String> sourcePaths = options.getMulti("-sourcepath");
                if (sourcePaths.isEmpty())
                    sourcePaths = FileUtil.filesToPathList(DefaultToolOptions.getCompilerSourceDirs());
                if (checkIfModule(sourcePaths, s)) {
                    helper.addClassName(s);
                    return false;
                }
                String paths = sourcePaths.toString();
                helper.error("err.module.not.found", s, paths.substring(1, paths.length() - 1));
                return true;
            }
            return false;
        }

        private boolean checkIfModule(List<String> paths, String moduleName) {
            String moduleDirName = moduleName.replace(".", File.separator);
            // walk every path arg
            for (String path : paths) {
                // split the path
                for (String part : path.split("\\" + File.pathSeparator)) {
                    // try to see if it's a module folder
                    File moduleFolder = new File(part, moduleDirName);
                    if (moduleFolder.isDirectory()) {
                        return true;
                    }
                }
            }
            return false;
        }
    } };
}
Also used : DefaultToolOptions(com.redhat.ceylon.common.config.DefaultToolOptions) Options(com.sun.tools.javac.util.Options) FileWriter(java.io.FileWriter) COption(com.sun.tools.javac.main.JavacOption.COption) Source(com.sun.tools.javac.code.Source) Lint(com.sun.tools.javac.code.Lint) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) StringTokenizer(java.util.StringTokenizer) Target(com.sun.tools.javac.jvm.Target) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Option(com.sun.tools.javac.main.JavacOption.Option) HiddenOption(com.sun.tools.javac.main.JavacOption.HiddenOption) COption(com.sun.tools.javac.main.JavacOption.COption) XOption(com.sun.tools.javac.main.JavacOption.XOption) List(java.util.List) HiddenOption(com.sun.tools.javac.main.JavacOption.HiddenOption) File(java.io.File) XOption(com.sun.tools.javac.main.JavacOption.XOption) PrintWriter(java.io.PrintWriter)

Aggregations

IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)24 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)13 Charset (java.nio.charset.Charset)10 File (java.io.File)3 IOException (java.io.IOException)3 PrintStream (java.io.PrintStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 CharBuffer (java.nio.CharBuffer)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 CharsetDecoder (java.nio.charset.CharsetDecoder)2 Matcher (java.util.regex.Matcher)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 HistoricallyNamedCharset (sun.nio.cs.HistoricallyNamedCharset)2 MediaType (com.google.common.net.MediaType)1 DefaultToolOptions (com.redhat.ceylon.common.config.DefaultToolOptions)1 Document (com.smartandroid.sa.tag.nodes.Document)1 Element (com.smartandroid.sa.tag.nodes.Element)1 Lint (com.sun.tools.javac.code.Lint)1