Java Code Examples for ucar.nc2.constants.CDM#UTF8

The following examples show how to use ucar.nc2.constants.CDM#UTF8 . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DodsURLExtractor.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Extract all A-HREF contained URLS from the given URL and return in List
 */
public ArrayList extract(String url) throws IOException {
  if (debug)
    System.out.println(" URLextract=" + url);

  baseURL = new URL(url);
  InputStream in = baseURL.openStream();
  InputStreamReader r = new InputStreamReader(filterTag(in), CDM.UTF8);
  HTMLEditorKit.ParserCallback callback = new CallerBacker();

  urlList = new ArrayList();
  wantURLS = true;
  wantText = false;
  parser.parse(r, callback, false);

  return urlList;
}
 
Example 2
Source File: DodsURLExtractor.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Extract text content from the given URL and return in String
 */
public String getTextContent(String url) throws IOException {
  if (debug)
    System.out.println(" URL.getTextContent=" + url);

  baseURL = new URL(url);
  InputStream in = baseURL.openStream();
  InputStreamReader r = new InputStreamReader(filterTag(in), CDM.UTF8);
  HTMLEditorKit.ParserCallback callback = new CallerBacker();

  textBuffer = new StringBuffer(3000);
  wantURLS = false;
  wantText = true;
  parser.parse(r, callback, false);

  return textBuffer.toString();
}
 
Example 3
Source File: DodsURLExtractor.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private InputStream filterTag(InputStream in) throws IOException {
  BufferedReader buffIn = new BufferedReader(new InputStreamReader(in, CDM.UTF8));
  ByteArrayOutputStream bos = new ByteArrayOutputStream(10000);

  String line = buffIn.readLine();
  while (line != null) {
    String lline = line.toLowerCase();
    if (lline.contains("<meta ")) // skip meta tags
      continue;
    // System.out.println("--"+line);
    bos.write(line.getBytes(StandardCharsets.UTF_8));
    line = buffIn.readLine();
  }
  buffIn.close();

  return new ByteArrayInputStream(bos.toByteArray());
}
 
Example 4
Source File: NcepMnemonicTest.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testReadBufrTable() throws IOException {
  Path tempDirectory = Files.createTempDirectory("NcepMnemonicTest");
  File fileOut = Files.createTempFile(tempDirectory, "temp", null).toFile();

  try (PrintStream pout = new PrintStream(fileOut, CDM.UTF8)) {
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.000.txt", pout, "MSG TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.001.txt", pout, "MESSAGE TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.002.txt", pout, "MSG TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.003.txt", pout, "MTYP ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.004.txt", pout, "MSG TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.005.txt", pout, "MSG TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.006.txt", pout, "M TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.007.txt", pout, "MTYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.008.txt", pout, "MSG TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.012.txt", pout, "M TYPE ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.021.txt", pout, "MTYP ");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.031.txt", pout, "031-");
    readSubCategories("../bufr/src/main/sources/ncep/bufrtab.255.txt", pout, "MTYP ");
  }
  System.out.printf("=======================================%n");
  System.out.printf("%s%n", IO.readFile(fileOut.getAbsolutePath()));
}
 
Example 5
Source File: ErddapStringArray.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Reads the contents of {@code inputStream} into a StringArray. Each line of input will result in an element in
 * the array.
 * <p/>
 * The specified stream remains open after this method returns.
 *
 * @param inputStream  a stream with line-based content.
 * @param charset      the name of a supported {@link java.nio.charset.Charset charset}.
 * @return  a StringArray created from the stream.
 * @throws IOException  if an I/O error occurs
 */
public static ErddapStringArray fromInputStream(InputStream inputStream, String charset) throws IOException {
    if (charset == null || charset.isEmpty()) {
        charset = CDM.UTF8;
    }
    InputStreamReader isr = new InputStreamReader(inputStream, charset);
    BufferedReader bufferedReader = new BufferedReader(isr);

    ErddapStringArray sa = new ErddapStringArray();
    for (String s; (s = bufferedReader.readLine()) != null; ) {
        sa.add(s);
    }

    // Do not call BufferedReader.close() here; that would close the underlying InputStream, which is the
    // responsibility of the client. This ought to be safe, as neither InputStreamReader nor BufferedReader hold any
    // resources that a call to close() would make free and which the garbage collector would not make free anyway.
    return sa;
}
 
Example 6
Source File: Tdm.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void main(String args[]) throws IOException, InterruptedException {
  try (FileSystemXmlApplicationContext springContext =
      new FileSystemXmlApplicationContext("classpath:resources/application-config.xml")) {
    Tdm app = (Tdm) springContext.getBean("TDM");

    Map<String, String> aliases = (Map<String, String>) springContext.getBean("dataRootLocationAliasExpanders");
    for (Map.Entry<String, String> entry : aliases.entrySet())
      AliasTranslator.addAlias(entry.getKey(), entry.getValue());

    EventBus eventBus = (EventBus) springContext.getBean("fcTriggerEventBus");
    CollectionUpdater collectionUpdater = (CollectionUpdater) springContext.getBean("collectionUpdater");
    collectionUpdater.setEventBus(eventBus); // Autowiring not working
    app.setUpdater(collectionUpdater, eventBus);

    String contentDir = System.getProperty("tds.content.root.path");
    if (contentDir == null)
      contentDir = "../content";
    app.setContentDir(contentDir);

    // RandomAccessFile.setDebugLeaks(true);
    HTTPSession.setGlobalUserAgent("TDM v5.0");
    // GribCollection.getDiskCache2().setNeverUseCache(true);

    String progName = Tdm.class.getName();

    try {
      CommandLine cmdLine = new CommandLine(progName, args);
      if (cmdLine.help) {
        cmdLine.printUsage();
        return;
      }

      if (cmdLine.catalog != null) {
        app.setCatalog(cmdLine.catalog);
      }

      if (cmdLine.cred != null) { // LOOK could be http://user:password@server
        String[] split = cmdLine.cred.split(":");
        app.user = split[0];
        app.pass = split[1];
        app.sendTriggers = true;
      }

      if (cmdLine.forceOnStartup)
        app.setForceOnStartup(true);

      if (cmdLine.nthreads != 0)
        app.setNThreads(cmdLine.nthreads);

      if (cmdLine.showOnly)
        app.setShowOnly(true);

      if (cmdLine.tds != null) {
        if (cmdLine.tds.equalsIgnoreCase("none")) {
          app.setServerNames(null);
          app.sendTriggers = false;

        } else {
          String[] tdss = cmdLine.tds.split(","); // comma separated
          app.setServerNames(tdss);
          app.sendTriggers = true;
        }
      }

    } catch (ParameterException e) {
      System.err.println(e.getMessage());
      System.err.printf("Try \"%s --help\" for more information.%n", progName);
    }

    if (!app.showOnly && app.pass == null && app.sendTriggers) {
      Scanner scanner = new Scanner(System.in, CDM.UTF8);
      String passw;
      while (true) {
        System.out.printf("%nEnter password for tds trigger: ");
        passw = scanner.nextLine();
        System.out.printf("%nPassword = '%s' OK (Y/N)?", passw);
        String ok = scanner.nextLine();
        if (ok.equalsIgnoreCase("Y"))
          break;
      }
      if (passw != null) {
        app.pass = passw;
        app.user = "tdm";
      } else {
        app.sendTriggers = false;
      }
    }

    if (app.init()) {
      app.start();
    } else {
      System.out.printf("%nEXIT DUE TO ERRORS");
    }
  }
}
 
Example 7
Source File: AdminDebugController.java    From tds with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET)
protected void showDebugPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
  response.setContentType(ContentType.html.getContentHeader());
  response.setHeader("Content-Description", "thredds_debug");

  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  PrintStream pw = new PrintStream(bos, false, CDM.UTF8);
  pw.println(htmlu.getHtmlDoctypeAndOpenTag());
  pw.println("<head>");
  pw.println("<title>THREDDS Debug</title>");
  pw.println("<meta http-equiv=\"Content-Type\" content=\"text/html\">");
  pw.println(htmlu.getTdsPageCssLink());
  pw.println(htmlu.getGoogleTrackingContent());
  pw.println("</head>");
  pw.println("<body>");
  pw.println(htmlu.getOldStyleHeader());
  pw.println("<br><a href='dir/content/thredds/logs/'>Show TDS Logs</a>");
  pw.println("<br><a href='dir/content/tdm/'>Show TDM Logs</a>");
  pw.println("<br><a href='dir/logs/'>Show Tomcat Logs</a>");
  pw.println("<br><a href='dir/catalogs/'>Show Config Catalogs</a>");
  pw.println("<br><a href='spring/showControllers'>Show Spring Controllers</a>");
  pw.println("<h2>Debug Actions</h2>");
  pw.println("<pre>");

  String cmds = request.getQueryString();
  if ((cmds == null) || (cmds.length() == 0)) {
    showDebugActions(request, pw);

  } else {

    StringTokenizer tz = new StringTokenizer(cmds, ";");
    while (tz.hasMoreTokens()) {
      String cmd = tz.nextToken();
      String target = null;

      pw.println("Cmd= " + cmd);
      int pos = cmd.indexOf('/');
      String dhName = "General";
      if (pos > 0) {
        dhName = cmd.substring(0, pos);
        cmd = cmd.substring(pos + 1);
      }

      pos = cmd.indexOf('=');
      if (pos >= 0) {
        target = cmd.substring(pos + 1);
        cmd = cmd.substring(0, pos);
      }

      DebugCommands.Category dh = debugCommands.findCategory(dhName);
      if (dh == null) {
        pw.println(" Unknown Debug Category=" + dhName + "=");
      } else {
        DebugCommands.Action action = dh.actions.get(cmd);
        if (action == null)
          pw.println(" Unknown action=" + cmd + "=");
        else
          action.doAction(new DebugCommands.Event(request, response, pw, bos, target));
      }
    }
  }
  pw.println("</pre></body></html>");

  response.setStatus(HttpServletResponse.SC_OK);

  // send it out
  PrintWriter responsePS = response.getWriter();
  responsePS.write(bos.toString(CDM.UTF8));
  responsePS.flush();
}