public static void main(String args[]) throws IOException { //String helloGreek = new TestUnicode("dumm").makeString(helloGreekCode, true); //testCharsets(); write("hello", null); write("hello", "UTF-8"); write("hello", "EUC-JP"); //String filename = "C:/data/unicode/UTF-8-demo.html"; //String filename = "C:/data/unicode/russian.txt"; //testRAF(filename); //testUnicodeRead(filename); //testRead(filename); }
String makeString(int[] codes, boolean debug) throws UnsupportedEncodingException { byte[] b = new byte[codes.length]; for (int i=0; i<codes.length; i++) b[i] = (byte) codes[i]; if (debug) System.out.println(" orgBytes= "+showBytes(b)); String s = new String(b, "UTF-8"); if (debug) System.out.println("convBytes= "+showString( s)); return s; }
static void testRAF(String filename) throws IOException { ucar.unidata.io.RandomAccessFile raf = new ucar.unidata.io.RandomAccessFile(filename, "r"); int nelems = 37; byte[] b = new byte[nelems]; raf.read(b); String line = new String(b, "UTF-8"); // all strings are considered to be UTF-8 unicode. System.out.println(line); System.out.println(showBytes(b)); System.out.println(showBytes(line.getBytes(CDM.utf8Charset))); }
public static void read(InputStream is, StringBuilder errlog) throws IOException { Document doc; SAXBuilder saxBuilder = new SAXBuilder(); try { doc = saxBuilder.build(is); } catch (JDOMException e) { throw new IOException(e.getMessage()); } read(doc.getRootElement(), errlog); }
public static Element writeAttribute(ucar.nc2.Attribute att, String elementName, Namespace ns) { Element attElem = new Element(elementName, ns); attElem.setAttribute("name", att.getShortName()); DataType dt = att.getDataType(); if ((dt != null) && (dt != DataType.STRING)) attElem.setAttribute("type", dt.toString()); if (att.getLength() == 0) { return attElem; } if (att.isString()) { StringBuilder buff = new StringBuilder(); for (int i = 0; i < att.getLength(); i++) { String sval = att.getStringValue(i); if (i > 0) buff.append(","); buff.append(sval); } attElem.setAttribute("value", Parse.cleanCharacterData(buff.toString())); if (att.getLength() > 1) attElem.setAttribute("separator", ","); } else { StringBuilder buff = new StringBuilder(); for (int i = 0; i < att.getLength(); i++) { Number val = att.getNumericValue(i); if (i > 0) buff.append(" "); buff.append(val.toString()); } attElem.setAttribute("value", buff.toString()); } return attElem; }
aggElem = ucar.nc2.util.xml.Parse.readRootElement("file:"+cacheFile.getPath()); } catch (IOException e) { if (debugCache) System.out.println(" No cache for " + cacheName+" - "+e.getMessage());
String helloGreek = makeString(helloGreekCode, true); String s2 ="";// = Normalizer.normalize(helloGreek, Normalizer.Form.NFC); System.out.println("char values= "+showString(helloGreek)); System.out.println(" UTF-8= "+showBytes(helloGreek.getBytes("UTF-8"))); System.out.println(" UTF-16= "+showBytes(helloGreek.getBytes("UTF-16"))); System.out.println(" default= "+showBytes(helloGreek.getBytes())); System.out.println("norm values= "+showString(s2)); System.out.println(" UTF-8= "+showBytes(s2.getBytes("UTF-8"))); System.out.println(" UTF-16= "+showBytes(s2.getBytes("UTF-16"))); System.out.println(" default= "+showBytes(s2.getBytes())); write(helloGreek, null); write(helloGreek, "UTF-8");
public void makeNetCDF() throws IOException, InvalidRangeException { String helloGreek = makeString(helloGreekCode, true); helloGreek = ""; //Normalizer.normalize(helloGreek, Normalizer.Form.NFC); System.out.println("normalized= "+showString(helloGreek)); String filename = "C:/data/unicode/helloNorm.nc"; NetcdfFileWriter ncfile = NetcdfFileWriter.createNew(filename, true); ucar.nc2.Dimension dim = ncfile.addDimension(helloGreek, 20); ncfile.addVariable(helloGreek, DataType.CHAR, helloGreek); ncfile.addVariableAttribute(helloGreek, "units", helloGreek); ncfile.create(); ArrayChar.D1 data = new ArrayChar.D1(dim.getLength()); data.setString(helloGreek); ncfile.write(helloGreek, data); ncfile.close(); NetcdfFile nc = NetcdfFile.open(filename); Variable v = ncfile.findVariable(helloGreek); assert v != null; assert v.getShortName().equals(helloGreek); Attribute att = v.findAttribute("units"); assert att != null; assert att.isString(); assert(helloGreek.equals(att.getStringValue())); nc.close(); }
public static void read(InputStream is, StringBuilder errlog) throws IOException { Document doc; SAXBuilder saxBuilder = new SAXBuilder(); try { doc = saxBuilder.build(is); } catch (JDOMException e) { throw new IOException(e.getMessage()); } read(doc.getRootElement(), errlog); }
attElem.setAttribute("value", Parse.cleanCharacterData(buff.toString())); if (attribute.getLength() > 1) attElem.setAttribute("separator", "|");
aggElem = ucar.nc2.util.xml.Parse.readRootElement("file:" + cacheFile.getPath()); } catch (IOException e) { if (debugCache) System.out.println(" No cache for " + cacheName + " - " + e.getMessage());
static void testUnicodeRead(String filename) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(filename), "UTF-8")); int count = 0; String line; while (null != (line = in.readLine())) { System.out.println("ULine "+count++); System.out.println(line); System.out.println(showBytes(line.getBytes(CDM.utf8Charset))); } in.close(); }
public static void read(InputStream is, StringBuilder errlog) throws IOException { Document doc; SAXBuilder saxBuilder = new SAXBuilder(); try { doc = saxBuilder.build(is); } catch (JDOMException e) { throw new IOException(e.getMessage()); } read(doc.getRootElement(), errlog); }
buff.append(sval); attElem.setAttribute("value", Parse.cleanCharacterData(buff.toString())); if (att.getLength() > 1) attElem.setAttribute("separator", ",");
aggElem = ucar.nc2.util.xml.Parse.readRootElement("file:"+cacheFile.getPath()); } catch (IOException e) { if (debugCache) System.out.println(" No cache for " + cacheName+" - "+e.getMessage());
static void testRead(String filename) throws IOException { DataInputStream in = new DataInputStream( new BufferedInputStream(new FileInputStream(filename))); int count = 0; String line; while (null != (line = in.readLine())) { System.out.println("Line "+count++); System.out.println(line); System.out.println(showBytes(line.getBytes(CDM.utf8Charset))); } in.close(); }
public ThreddsConfigReader( String filename, org.slf4j.Logger log) { this.log = log; File file = new File(filename); if (!file.exists()) return; log.info( "ThreddsConfigReader reading xml file = " + filename); org.jdom2.Document doc; try { InputStream is = new FileInputStream(filename); SAXBuilder builder = new SAXBuilder(); doc = builder.build(is); } catch (IOException | JDOMException e) { log.error( "ThreddsConfigReader: incorrectly formed xml file [" + filename + "]: " + e.getMessage()); return; } rootElem = doc.getRootElement(); // nj22 runtime loading Element elem = rootElem.getChild("nj22Config"); if (elem != null) { StringBuilder errlog = new StringBuilder(); RuntimeConfigParser.read(elem, errlog); if (errlog.length() > 0) //System.out.println( "ThreddsConfig:WARN: " + errlog.toString()); log.warn( "ThreddsConfigReader nj22Config: {}", errlog.toString()); } }