use of java.io.Reader in project es6draft by anba.
the class IntlDataTools method oldStyleLanguageTags.
/**
* {@link IntlAbstractOperations#oldStyleLanguageTags}
*
* @param cldrMainDir
* the CLDR main directory
* @throws IOException
* if an I/O error occurs
*/
static void oldStyleLanguageTags(Path cldrMainDir) throws IOException {
try (DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(cldrMainDir)) {
Map<String, String> names = new LinkedHashMap<>();
Map<String, String> aliased = new LinkedHashMap<>();
for (Path path : newDirectoryStream) {
try (Reader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
Document xml = xml(reader);
Element identity = getElementByTagName(xml.getDocumentElement(), "identity");
assert identity != null;
Element language = getElementByTagName(xml.getDocumentElement(), "language");
Element script = getElementByTagName(xml.getDocumentElement(), "script");
Element territory = getElementByTagName(xml.getDocumentElement(), "territory");
String tag = language.getAttribute("type");
if (script != null) {
tag += "-" + script.getAttribute("type");
}
if (territory != null) {
tag += "-" + territory.getAttribute("type");
}
String filename = Objects.requireNonNull(path.getFileName()).toString();
filename = filename.substring(0, filename.lastIndexOf('.'));
names.put(filename, tag);
Element alias = getElementByTagName(xml.getDocumentElement(), "alias");
if (alias != null && script == null && territory != null) {
aliased.put(tag, alias.getAttribute("source"));
}
}
}
Map<String, String> result = new LinkedHashMap<>();
for (Map.Entry<String, String> entry : aliased.entrySet()) {
String from = entry.getKey();
String to = names.get(entry.getValue());
String value = result.get(to);
if (value == null) {
value = "";
} else {
value += ", ";
}
value += "\"" + from + "\"";
result.put(to, value);
}
for (Map.Entry<String, String> entry : result.entrySet()) {
System.out.printf("map.put(\"%s\", new String[]{%s});%n", entry.getKey(), entry.getValue());
}
}
}
use of java.io.Reader in project es6draft by anba.
the class ChakraTest method readSettings.
private static Map<String, TestSetting> readSettings(Path dir) {
Path settingsFile = dir.resolve("rlexe.xml");
if (Files.isRegularFile(settingsFile)) {
try (Reader reader = bomReader(Files.newInputStream(settingsFile))) {
Document doc = Resources.xml(reader);
NodeList elements = doc.getElementsByTagName("default");
HashMap<String, TestSetting> settingMap = new HashMap<>();
for (int i = 0, length = elements.getLength(); i < length; ++i) {
Element element = (Element) elements.item(i);
String files = element.getElementsByTagName("files").item(0).getTextContent();
TestSetting setting = new TestSetting();
NodeList baseline = element.getElementsByTagName("baseline");
if (baseline.getLength() > 0) {
setting.baseline = baseline.item(0).getTextContent();
}
NodeList compileFlags = element.getElementsByTagName("compile-flags");
if (compileFlags.getLength() > 0) {
String flags = compileFlags.item(0).getTextContent();
setting.disabled = flags.contains("-verbose") || flags.contains("-dump:") || flags.contains("-trace:") || flags.contains("-testtrace:") || flags.contains("-testTrace:");
}
settingMap.putIfAbsent(files, setting);
}
return settingMap;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return Collections.emptyMap();
}
use of java.io.Reader in project es6draft by anba.
the class Resources method readExcludeXML.
/**
* Load the exclusion xml-list for invalid test cases from {@link InputStream}
*/
private static Set<String> readExcludeXML(InputStream is) throws IOException {
Set<String> exclude = new HashSet<>();
Reader reader = new InputStreamReader(new BOMInputStream(is), StandardCharsets.UTF_8);
NodeList ns = xml(reader).getDocumentElement().getElementsByTagName("test");
for (int i = 0, len = ns.getLength(); i < len; ++i) {
exclude.add(((Element) ns.item(i)).getAttribute("id"));
}
return exclude;
}
use of java.io.Reader in project groovy by apache.
the class IOGroovyMethods method withReader.
/**
* Allows this reader to be used within the closure, ensuring that it
* is closed before this method returns.
*
* @param reader the reader which is used and then closed
* @param closure the closure that the writer is passed into
* @return the value returned by the closure
* @throws IOException if an IOException occurs.
* @since 1.5.2
*/
public static <T> T withReader(Reader reader, @ClosureParams(FirstParam.class) Closure<T> closure) throws IOException {
try {
T result = closure.call(reader);
Reader temp = reader;
reader = null;
temp.close();
return result;
} finally {
closeWithWarning(reader);
}
}
use of java.io.Reader in project groovy by apache.
the class InvokerHelper method write.
/**
* Writes an object to a Writer using Groovy's default representation for the object.
*/
public static void write(Writer out, Object object) throws IOException {
if (object instanceof String) {
out.write((String) object);
} else if (object instanceof Object[]) {
out.write(toArrayString((Object[]) object));
} else if (object instanceof Map) {
out.write(toMapString((Map) object));
} else if (object instanceof Collection) {
out.write(toListString((Collection) object));
} else if (object instanceof Writable) {
Writable writable = (Writable) object;
writable.writeTo(out);
} else if (object instanceof InputStream || object instanceof Reader) {
// Copy stream to stream
Reader reader;
if (object instanceof InputStream) {
reader = new InputStreamReader((InputStream) object);
} else {
reader = (Reader) object;
}
char[] chars = new char[8192];
int i;
while ((i = reader.read(chars)) != -1) {
out.write(chars, 0, i);
}
reader.close();
} else {
out.write(toString(object));
}
}
Aggregations