Search in sources :

Example 56 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project ceylon by eclipse.

the class CeylonCompileTool method initialize.

@Override
public void initialize(CeylonTool mainTool) throws Exception {
    super.initialize(mainTool);
    compiler = new Main("ceylon compile");
    helper.options.clear();
    Options.instance(new Context());
    includeDependencies = processCompileFlags(includeDependencies, DefaultToolOptions.getCompilerIncludeDependencies());
    if (modulesOrFiles.isEmpty() && !javac.contains("-help") && !javac.contains("-X") && !javac.contains("-version")) {
        throw new IllegalStateException("Argument moduleOrFile should appear at least 1 time(s)");
    }
    arguments = new ArrayList<>();
    if (cwd != null) {
        arguments.add("-cwd");
        arguments.add(cwd.getPath());
        validateWithJavac(org.eclipse.ceylon.langtools.tools.javac.main.Option.CEYLONCWD, "-cwd", cwd.getPath());
    }
    if (jdkProvider != null) {
        arguments.add("-jdk-provider");
        arguments.add(jdkProvider.toString());
    }
    if (aptModules != null) {
        for (ModuleSpec mod : aptModules) {
            arguments.add("-apt");
            arguments.add(mod.toString());
        }
    }
    for (File source : applyCwd(this.sources)) {
        arguments.add("-src");
        arguments.add(source.getPath());
        validateWithJavac(org.eclipse.ceylon.langtools.tools.javac.main.Option.CEYLONSOURCEPATH, "-sourcepath", source.getPath());
    }
    for (File resource : applyCwd(this.resources)) {
        arguments.add("-res");
        arguments.add(resource.getPath());
    }
    if (resourceRoot != null) {
        arguments.add("-resroot");
        arguments.add(resourceRoot);
    }
    if (continueOnErrors) {
        arguments.add("-continue");
    }
    if (progress) {
        arguments.add("-progress");
    }
    if (offline) {
        arguments.add("-offline");
    }
    if (timeout != -1) {
        arguments.add("-timeout");
        arguments.add(String.valueOf(timeout));
    }
    if (flatClasspath) {
        arguments.add("-flat-classpath");
    }
    if (autoExportMavenDependencies) {
        arguments.add("-auto-export-maven-dependencies");
    }
    if (fullyExportMavenDependencies) {
        arguments.add("-fully-export-maven-dependencies");
    }
    if (overrides != null) {
        arguments.add("-overrides");
        if (overrides.startsWith("classpath:")) {
            arguments.add(overrides);
        } else {
            arguments.add(applyCwd(new File(overrides)).getPath());
        }
    }
    if (jigsaw) {
        arguments.add("-module-info");
    }
    if (noOsgi) {
        arguments.add("-noosgi");
    }
    if (osgiProvidedBundles != null && !osgiProvidedBundles.isEmpty()) {
        arguments.add("-osgi-provided-bundles");
        arguments.add(osgiProvidedBundles);
    }
    if (noPom) {
        arguments.add("-nopom");
    }
    if (pack200) {
        arguments.add("-pack200");
    }
    if (verbose != null) {
        if (verbose.isEmpty()) {
            arguments.add("-verbose");
        } else {
            arguments.add("-verbose:" + verbose);
        }
    }
    if (out != null) {
        arguments.add("-out");
        arguments.add(out);
    }
    if (user != null) {
        arguments.add("-user");
        arguments.add(user);
    }
    if (pass != null) {
        arguments.add("-pass");
        arguments.add(pass);
    }
    String fileEncoding = encoding;
    if (fileEncoding == null) {
        fileEncoding = DefaultToolOptions.getDefaultEncoding();
    }
    if (fileEncoding != null) {
        try {
            Charset.forName(fileEncoding);
        } catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
            throw new IllegalArgumentException("Unsupported encoding: " + fileEncoding);
        }
        arguments.add(org.eclipse.ceylon.langtools.tools.javac.main.Option.ENCODING.text);
        arguments.add(fileEncoding);
    }
    if (systemRepo != null) {
        arguments.add("-sysrep");
        arguments.add(systemRepo);
    }
    if (cacheRepo != null) {
        arguments.add("-cacherep");
        arguments.add(cacheRepo);
    }
    if (noDefRepos) {
        arguments.add("-nodefreps");
    }
    if (repos != null) {
        for (URI uri : this.repos) {
            arguments.add("-rep");
            arguments.add(uri.toString());
        }
    }
    if (suppressWarnings != null) {
        arguments.add("-suppress-warnings");
        arguments.add(EnumUtil.enumsToString(suppressWarnings));
    }
    if (targetVersion != null) {
        arguments.add("-source");
        arguments.add(targetVersion.toString());
        arguments.add("-target");
        arguments.add(targetVersion.toString());
    }
    if (ee) {
        arguments.add("-ee");
    }
    if (eeImport != null) {
        for (String eeImport : this.eeImport) {
            arguments.add("-ee-import");
            arguments.add(eeImport);
        }
    }
    if (eeAnnotation != null) {
        for (String eeAnnotation : this.eeAnnotation) {
            arguments.add("-ee-annotation");
            arguments.add(eeAnnotation);
        }
    }
    addJavacArguments(arguments, javac);
    List<File> srcs = applyCwd(this.sources);
    Collection<String> expandedModulesOrFiles = ModuleWildcardsHelper.expandWildcards(srcs, this.modulesOrFiles, Backend.Java);
    expandedModulesOrFiles = normalizeFileNames(expandedModulesOrFiles);
    if (expandedModulesOrFiles.isEmpty()) {
        String msg = CeylonCompileMessages.msg("error.no.sources");
        if (ModuleWildcardsHelper.onlyGlobArgs(this.modulesOrFiles)) {
            throw new NonFatalToolMessage(msg);
        } else {
            throw new ToolUsageError(msg);
        }
    }
    for (String moduleOrFile : expandedModulesOrFiles) {
        if (!org.eclipse.ceylon.langtools.tools.javac.main.Option.SOURCEFILE.matches(moduleOrFile)) {
            throw new IllegalArgumentException(CeylonCompileMessages.msg("argument.error", moduleOrFile));
        }
        validateWithJavac(org.eclipse.ceylon.langtools.tools.javac.main.Option.SOURCEFILE, moduleOrFile, moduleOrFile);
    }
    // We validate that all source arguments are correct
    SourceArgumentsResolver sar = new SourceArgumentsResolver(this.sources, this.resources, Constants.CEYLON_SUFFIX, Constants.JAVA_SUFFIX);
    sar.cwd(cwd).parse(expandedModulesOrFiles);
    if (includeDependencies != null && !COMPILE_NEVER.equals(includeDependencies)) {
        // Determine any dependencies that might need compiling as well
        SourceDependencyResolver sdr = new SourceDependencyResolver(getModuleVersionReader(), this.sources, Backends.JAVA);
        if (sdr.traverseDependencies(sar.getSourceFiles())) {
            for (ModuleVersionDetails mvd : sdr.getAdditionalModules()) {
                if (COMPILE_FORCE.equals(includeDependencies) || (COMPILE_CHECK.equals(includeDependencies) && shouldRecompile(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JVM, true)) || (COMPILE_ONCE.equals(includeDependencies) && shouldRecompile(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JVM, false))) {
                    expandedModulesOrFiles.add(mvd.getModule());
                    if (incremental) {
                        sar.parse(expandedModulesOrFiles);
                    }
                }
            }
        }
    }
    if (incremental) {
        next_module: for (String module : sar.getModules()) {
            // Determine module version from source
            ModuleVersionDetails mvd = getModuleVersionReader().fromSource(module);
            if (mvd != null) {
                // Find the module's CAR file
                ArtifactResult artifact = getModuleArtifact(getOfflineRepositoryManager(), mvd.getModule(), mvd.getVersion(), ModuleQuery.Type.JVM);
                if (artifact != null) {
                    // Check the language module version
                    for (ArtifactResult dep : artifact.dependencies()) {
                        if ("ceylon.language".equals(dep.name())) {
                            if (!Versions.CEYLON_VERSION_NUMBER.equals(dep.version())) {
                                // Skip handling of --incremental on this module
                                continue next_module;
                            }
                            break;
                        }
                    }
                    File carFile = artifact.artifact();
                    // Check if it has META-INF/errors.txt
                    Properties errors = getMetaInfErrors(carFile);
                    if (errors != null && !errors.isEmpty()) {
                        // TODO handle this incrementally
                        continue;
                    }
                    // Check if it has META-INF/hashes.txt
                    Properties oldHashes = getMetaInfHashes(carFile);
                    if (oldHashes != null) {
                        // Get the hashes for the new files
                        List<File> files = sar.getFilesByModule().get(module);
                        Properties newHashes = getFileHashes(module, files);
                        // Compare the two and make list of changed files
                        Collection<String> changedFiles = determineChangedFiles(module, oldHashes, newHashes, carFile);
                        if (changedFiles == null) {
                        // This shouldn't happen, but if it does we just skip any
                        // special treatment and compile this module normally
                        } else if (changedFiles.isEmpty()) {
                            // No files were changed, we shouldn't compile the module
                            expandedModulesOrFiles.remove(module);
                            // And we remove its files too if any were mentioned
                            Collection<String> remove = filesToStrings(module, files);
                            expandedModulesOrFiles.removeAll(remove);
                        } else {
                            if (expandedModulesOrFiles.contains(module)) {
                                // The module itself was mentioned on the command line
                                if (changedFiles.size() < files.size()) {
                                    // There were fewer changed files than the total number
                                    // So we remove the module
                                    expandedModulesOrFiles.remove(module);
                                    // And we remove its files too if any were mentioned
                                    Collection<String> remove = filesToStrings(module, files);
                                    expandedModulesOrFiles.removeAll(remove);
                                    // And then we add only those files that were changed
                                    expandedModulesOrFiles.addAll(changedFiles);
                                }
                            } else {
                                // Separate source files were mentioned on the command line
                                // So we remove the unchanged files
                                Collection<String> unchanged = filesToStrings(module, files);
                                unchanged.removeAll(changedFiles);
                                expandedModulesOrFiles.removeAll(unchanged);
                            }
                        }
                    }
                }
            }
        }
        if (expandedModulesOrFiles.isEmpty()) {
            String msg = CeylonCompileMessages.msg("error.no.need");
            throw new NonFatalToolMessage(msg);
        }
    }
    arguments.addAll(expandedModulesOrFiles);
    if (verbose != null) {
        System.out.println(arguments);
        System.out.flush();
    }
}
Also used : Context(org.eclipse.ceylon.langtools.tools.javac.util.Context) SourceArgumentsResolver(org.eclipse.ceylon.common.tools.SourceArgumentsResolver) ModuleVersionDetails(org.eclipse.ceylon.cmr.api.ModuleVersionDetails) SourceDependencyResolver(org.eclipse.ceylon.common.tools.SourceDependencyResolver) Properties(java.util.Properties) URI(java.net.URI) NonFatalToolMessage(org.eclipse.ceylon.common.tool.NonFatalToolMessage) ArtifactResult(org.eclipse.ceylon.model.cmr.ArtifactResult) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ModuleSpec(org.eclipse.ceylon.common.ModuleSpec) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ToolUsageError(org.eclipse.ceylon.common.tool.ToolUsageError) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) Main(org.eclipse.ceylon.compiler.java.launcher.Main) File(java.io.File)

Example 57 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project structr by structr.

the class File method getInputStream.

static InputStream getInputStream(final File thisFile) {
    final java.io.File fileOnDisk = thisFile.getFileOnDisk();
    try {
        final FileInputStream fis = new FileInputStream(fileOnDisk);
        final SecurityContext securityContext = thisFile.getSecurityContext();
        if (thisFile.isTemplate()) {
            boolean editModeActive = false;
            if (securityContext.getRequest() != null) {
                final String editParameter = securityContext.getRequest().getParameter("edit");
                if (editParameter != null) {
                    editModeActive = !RenderContext.EditMode.NONE.equals(RenderContext.editMode(editParameter));
                }
            }
            if (!editModeActive) {
                final String content = IOUtils.toString(fis, "UTF-8");
                // close input stream here
                fis.close();
                try {
                    final String result = Scripting.replaceVariables(new ActionContext(securityContext), thisFile, content);
                    String encoding = "UTF-8";
                    final String cType = getContentType();
                    if (cType != null) {
                        final String charset = StringUtils.substringAfterLast(cType, "charset=").trim().toUpperCase();
                        try {
                            if (!"".equals(charset) && Charset.isSupported(charset)) {
                                encoding = charset;
                            }
                        } catch (IllegalCharsetNameException ice) {
                            logger.warn("Charset is not supported '{}'. Using 'UTF-8'", charset);
                        }
                    }
                    return IOUtils.toInputStream(result, encoding);
                } catch (Throwable t) {
                    logger.warn("Scripting error in {}:\n{}\n{}", thisFile.getUuid(), content, t.getMessage());
                }
            }
        }
        return fis;
    } catch (IOException ex) {
        logger.warn("Unable to open input stream for {}: {}", fileOnDisk.getPath(), ex.getMessage());
    }
    return null;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) SecurityContext(org.structr.common.SecurityContext) IOException(java.io.IOException) ActionContext(org.structr.schema.action.ActionContext) FileInputStream(java.io.FileInputStream)

Example 58 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project hbase by apache.

the class RegexStringComparator method parseFrom.

/**
 * @param pbBytes A pb serialized {@link RegexStringComparator} instance
 * @return An instance of {@link RegexStringComparator} made from <code>bytes</code>
 * @throws DeserializationException
 * @see #toByteArray
 */
public static RegexStringComparator parseFrom(final byte[] pbBytes) throws DeserializationException {
    ComparatorProtos.RegexStringComparator proto;
    try {
        proto = ComparatorProtos.RegexStringComparator.parseFrom(pbBytes);
    } catch (InvalidProtocolBufferException e) {
        throw new DeserializationException(e);
    }
    RegexStringComparator comparator;
    if (proto.hasEngine()) {
        EngineType engine = EngineType.valueOf(proto.getEngine());
        comparator = new RegexStringComparator(proto.getPattern(), proto.getPatternFlags(), engine);
    } else {
        comparator = new RegexStringComparator(proto.getPattern(), proto.getPatternFlags());
    }
    String charset = proto.getCharset();
    if (charset.length() > 0) {
        try {
            comparator.getEngine().setCharset(charset);
        } catch (IllegalCharsetNameException e) {
            LOG.error("invalid charset", e);
        }
    }
    return comparator;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) ComparatorProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.ComparatorProtos) InvalidProtocolBufferException(org.apache.hbase.thirdparty.com.google.protobuf.InvalidProtocolBufferException) DeserializationException(org.apache.hadoop.hbase.exceptions.DeserializationException)

Example 59 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project gitblit by gitblit.

the class StringUtils method decodeString.

/**
 * Decodes a string by trying several charsets until one does not throw a
 * coding exception.  Last resort is to interpret as UTF-8 with illegal
 * character substitution.
 *
 * @param content
 * @param charsets optional
 * @return a string
 */
public static String decodeString(byte[] content, String... charsets) {
    Set<String> sets = new LinkedHashSet<String>();
    if (!ArrayUtils.isEmpty(charsets)) {
        sets.addAll(Arrays.asList(charsets));
    }
    String value = null;
    sets.addAll(Arrays.asList("UTF-8", "ISO-8859-1", Charset.defaultCharset().name()));
    for (String charset : sets) {
        try {
            Charset cs = Charset.forName(charset);
            CharsetDecoder decoder = cs.newDecoder();
            CharBuffer buffer = decoder.decode(ByteBuffer.wrap(content));
            value = buffer.toString();
            break;
        } catch (CharacterCodingException e) {
        // ignore and advance to the next charset
        } catch (IllegalCharsetNameException e) {
        // ignore illegal charset names
        } catch (UnsupportedCharsetException e) {
        // ignore unsupported charsets
        }
    }
    if (value != null && value.startsWith("\uFEFF")) {
        // strip UTF-8 BOM
        return value.substring(1);
    }
    return value;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) CharsetDecoder(java.nio.charset.CharsetDecoder) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException)

Example 60 with IllegalCharsetNameException

use of java.nio.charset.IllegalCharsetNameException in project maven-archetype by apache.

the class ArchetypeDescriptorBuilder method addTestResourceToDescriptor.

/**
 * Adds the test-resource element <code>resource</code> to the list of test-resources in the
 * <code>descriptor</code> and sets its <code>TemplateDescriptor</code> to
 * <i>filtered</i> if the attribute <code>filtered</code> was not
 * specified or its value is <code>&quot;true&quot;</code>, or <code>false</code>
 * if its value is <code>&quot;false&quot;</code>, and the encoding specified
 * in the <code>encoding</code> attribute or the Java virtual machine's default if
 * it is not defined. If the <code>resource</code> is a property file (ends in
 * <code>.properties</code>) its encoding will be set to <code>iso-8859-1</code>
 * even if some other encoding is specified in the attribute.
 *
 * @param testResource a <code>&lt;resource&gt;</code> element from the <code>&lt;testResources&gt;</code>
 * @param descriptor   the <code>ArchetypeDescriptor</code> to add the test-resource template to.
 * @throws XmlPullParserException if the encoding specified is not valid or supported or if the
 *                                value of the attribute <code>filtered</code> is no valid.
 */
private static void addTestResourceToDescriptor(Xpp3Dom testResource, ArchetypeDescriptor descriptor) throws XmlPullParserException {
    descriptor.addTestResource(testResource.getValue());
    if (testResource.getAttribute("filtered") != null) {
        TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
        try {
            testResourceDesc.setFiltered(getValueFilteredAttribute(testResource.getAttribute("filtered")));
        } catch (IllegalArgumentException iae) {
            throw new XmlPullParserException(iae.getMessage());
        }
    }
    if (testResource.getAttribute("encoding") != null) {
        TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
        try {
            testResourceDesc.setEncoding(testResource.getAttribute("encoding"));
        } catch (IllegalCharsetNameException icne) {
            throw new XmlPullParserException(testResource.getAttribute("encoding") + " is not a valid encoding.");
        } catch (UnsupportedCharsetException uce) {
            throw new XmlPullParserException(testResource.getAttribute("encoding") + " is not a supported encoding.");
        }
    }
    if (testResource.getValue().endsWith(".properties")) {
        TemplateDescriptor testResourceDesc = descriptor.getTestResourceDescriptor(testResource.getValue());
        testResourceDesc.setEncoding("iso-8859-1");
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException)

Aggregations

IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)69 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)44 Charset (java.nio.charset.Charset)34 IOException (java.io.IOException)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 File (java.io.File)9 ByteBuffer (java.nio.ByteBuffer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 CharacterCodingException (java.nio.charset.CharacterCodingException)6 CoreException (org.eclipse.core.runtime.CoreException)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 HistoricallyNamedCharset (sun.nio.cs.HistoricallyNamedCharset)6 CharsetEncoder (java.nio.charset.CharsetEncoder)5 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)5 SequenceInputStream (java.io.SequenceInputStream)4 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)4 CharArrayWriter (java.io.CharArrayWriter)3 FileInputStream (java.io.FileInputStream)3