List of usage examples for java.util StringTokenizer hasMoreTokens
public boolean hasMoreTokens()
From source file:com.intel.chimera.cipher.Openssl.java
private static Transform tokenizeTransformation(String transformation) throws NoSuchAlgorithmException { if (transformation == null) { throw new NoSuchAlgorithmException("No transformation given."); }/* w w w.ja v a2 s .c o m*/ /* * Array containing the components of a Cipher transformation: * * index 0: algorithm (e.g., AES) * index 1: mode (e.g., CTR) * index 2: padding (e.g., NoPadding) */ String[] parts = new String[3]; int count = 0; StringTokenizer parser = new StringTokenizer(transformation, "/"); while (parser.hasMoreTokens() && count < 3) { parts[count++] = parser.nextToken().trim(); } if (count != 3 || parser.hasMoreTokens()) { throw new NoSuchAlgorithmException("Invalid transformation format: " + transformation); } return new Transform(parts[0], parts[1], parts[2]); }
From source file:ch.entwine.weblounge.common.impl.content.image.ImageMetadataUtils.java
/** * This utility method extracts image metadata stored in EXIF and IPTC tags * and returns the extracted information in a {@link ImageMetadata}. * /*from w ww. j a va 2 s . c o m*/ * @param img * image input stream * @return extracted meta information */ public static ImageMetadata extractMetadata(BufferedInputStream img) { Metadata meta; try { meta = ImageMetadataReader.readMetadata(img); } catch (ImageProcessingException e) { if ("File is not the correct format".equals(e.getMessage())) return null; logger.warn("Failed to extract image metadata from image: {}", e.getMessage()); return null; } if (meta == null) { logger.debug("Extracted metadata is null"); return null; } else { ImageMetadata imgmeta = new ImageMetadata(); // Extract IPTC information Directory iptc = meta.getDirectory(IptcDirectory.class); if (iptc.containsTag(IptcDirectory.TAG_HEADLINE)) imgmeta.setCaption(iptc.getString(IptcDirectory.TAG_HEADLINE)); if (iptc.containsTag(IptcDirectory.TAG_CAPTION)) imgmeta.setLegend(iptc.getString(IptcDirectory.TAG_CAPTION)); if (iptc.containsTag(IptcDirectory.TAG_BY_LINE)) imgmeta.setPhotographer(iptc.getString(IptcDirectory.TAG_BY_LINE)); if (iptc.containsTag(IptcDirectory.TAG_COPYRIGHT_NOTICE)) imgmeta.setCopyright(iptc.getString(IptcDirectory.TAG_COPYRIGHT_NOTICE)); if (iptc.containsTag(IptcDirectory.TAG_CITY)) imgmeta.setLocation(iptc.getString(IptcDirectory.TAG_CITY)); if (iptc.containsTag(IptcDirectory.TAG_KEYWORDS)) { StringTokenizer st = new StringTokenizer(iptc.getString(IptcDirectory.TAG_KEYWORDS), ",;"); while (st.hasMoreTokens()) { imgmeta.addKeyword(st.nextToken()); } } // Extract EXIF information Directory exif = meta.getDirectory(ExifDirectory.class); if (exif.containsTag(ExifDirectory.TAG_DATETIME)) { try { imgmeta.setDateTaken(exif.getDate(ExifDirectory.TAG_DATETIME)); } catch (MetadataException e) { } } if (exif.containsTag(ExifDirectory.TAG_ISO_EQUIVALENT)) { try { imgmeta.setFilmspeed(exif.getInt(ExifDirectory.TAG_ISO_EQUIVALENT)); } catch (MetadataException e) { } } if (exif.containsTag(ExifDirectory.TAG_FNUMBER)) { try { imgmeta.setFNumber(exif.getFloat(ExifDirectory.TAG_FNUMBER)); } catch (MetadataException e) { } } if (exif.containsTag(ExifDirectory.TAG_FOCAL_LENGTH)) { try { imgmeta.setFocalWidth(exif.getInt(ExifDirectory.TAG_FOCAL_LENGTH)); } catch (MetadataException e) { } } if (exif.containsTag(ExifDirectory.TAG_EXPOSURE_TIME)) { try { imgmeta.setExposureTime(exif.getFloat(ExifDirectory.TAG_EXPOSURE_TIME)); } catch (MetadataException e) { } } if (StringUtils.isBlank(imgmeta.getCopyright()) && exif.containsTag(ExifDirectory.TAG_COPYRIGHT)) imgmeta.setCopyright(exif.getString(ExifDirectory.TAG_COPYRIGHT)); // Extract GPS information try { Directory gps = meta.getDirectory(GpsDirectory.class); if (gps.containsTag(GpsDirectory.TAG_GPS_LATITUDE)) { Rational[] lat = gps.getRationalArray(GpsDirectory.TAG_GPS_LATITUDE); String latRef = gps.getString(GpsDirectory.TAG_GPS_LATITUDE_REF); double latitude = parseHMS(lat); if (latitude != 0) { if (StringUtils.isNotBlank(latRef) && "S".equalsIgnoreCase(latRef) && latitude > 0) latitude *= -1; } imgmeta.setGpsLat(latitude); } if (gps.containsTag(GpsDirectory.TAG_GPS_LONGITUDE)) { Rational[] lng = gps.getRationalArray(GpsDirectory.TAG_GPS_LONGITUDE); String lngRef = gps.getString(GpsDirectory.TAG_GPS_LONGITUDE_REF); double longitude = parseHMS(lng); if (longitude != 0) { if (StringUtils.isNotBlank(lngRef) && "W".equalsIgnoreCase(lngRef) && longitude > 0) longitude *= -1; } imgmeta.setGpsLong(longitude); } } catch (MetadataException e) { logger.info("Error while extracting GPS information out of an image."); imgmeta.setGpsLat(0); imgmeta.setGpsLong(0); } return imgmeta; } }
From source file:RequestUtil.java
static public String removeQueryStringParam(String queryString, String paramName) { String resultString = ""; if (queryString == null) { return queryString; }/*from www . jav a 2 s . c o m*/ StringTokenizer st = new StringTokenizer(queryString, "&"); while (st.hasMoreTokens()) { String pair = (String) st.nextToken(); int pos = pair.indexOf('='); if (pos != -1) { String key = pair.substring(0, pos); if (paramName.equals(key)) continue; } if (resultString.length() > 0) resultString += "&"; resultString += pair; } return resultString; }
From source file:de.tudarmstadt.ukp.csniper.webapp.search.tgrep.PennTreeUtils.java
public static PennTreeNode parsePennTree(String aTree) { StringTokenizer st = new StringTokenizer(aTree, "() ", true); PennTreeNode root = null;/* ww w . jav a 2 s . c o m*/ Stack<PennTreeNode> stack = new Stack<PennTreeNode>(); boolean seenLabel = false; int i = 0; while (st.hasMoreTokens()) { String t = st.nextToken().trim(); if (t.length() == 0) { // Skip } else if ("(".equals(t)) { PennTreeNode n = new PennTreeNode(); stack.push(n); if (root == null) { root = n; } seenLabel = false; } else if (")".equals(t)) { PennTreeNode n = stack.pop(); if (!stack.isEmpty()) { PennTreeNode p = stack.peek(); p.addChild(n); } } else if (seenLabel) { // If the node has two labels, its a leaf, add a new terminal node then. PennTreeNode p = stack.peek(); PennTreeNode n = new PennTreeNode(); n.setTokenIndex(i); i++; n.setLabel(t); p.addChild(n); } else { PennTreeNode n = stack.peek(); n.setLabel(t); seenLabel = true; } } return root; }
From source file:StringUtils.java
public static List<String> explode(String s, String sep) { StringTokenizer st = new StringTokenizer(s, sep); List<String> v = new ArrayList<String>(); for (; st.hasMoreTokens();) { v.add(st.nextToken());/* w w w . j a v a 2 s . co m*/ } return v; }
From source file:net.ripe.rpki.commons.util.VersionedId.java
public static VersionedId parse(String s) { Validate.notNull(s, "string required"); StringTokenizer tokenizer = new StringTokenizer(s, ":"); int count = tokenizer.countTokens(); Validate.isTrue(count == 1 || count == 2, "invalid number of tokens in versioned id"); long id = Long.parseLong(tokenizer.nextToken()); long version; if (tokenizer.hasMoreTokens()) { version = Long.parseLong(tokenizer.nextToken()); } else {/*from w ww . j a v a 2 s. co m*/ version = 0; } return new VersionedId(id, version); }
From source file:com.impetus.kundera.rest.common.EntityUtils.java
/** * @param queryString//from w w w.j a va 2 s. com * @param q */ public static void setQueryParameters(String queryString, String parameterString, Query q) { Map<String, String> paramsMap = new HashMap<String, String>(); StringTokenizer st = new StringTokenizer(parameterString, "&"); while (st.hasMoreTokens()) { String element = st.nextToken(); paramsMap.put(element.substring(0, element.indexOf("=")), element.substring(element.indexOf("=") + 1, element.length())); } KunderaQuery kq = ((QueryImpl) q).getKunderaQuery(); Set<Parameter<?>> parameters = kq.getParameters(); for (String paramName : paramsMap.keySet()) { String value = paramsMap.get(paramName); if (paramName.equalsIgnoreCase("firstResult")) { q.setFirstResult(Integer.parseInt(value)); } else if (paramName.equalsIgnoreCase("maxResult")) { q.setMaxResults(Integer.parseInt(value)); } else if (StringUtils.isNumeric(paramName)) { for (Parameter param : parameters) { if (param.getPosition() == Integer.parseInt(paramName)) { Class<?> paramClass = param.getParameterType(); PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass); Object paramValue = accessor.fromString(paramClass, value); q.setParameter(Integer.parseInt(paramName), paramValue); break; } } } else { for (Parameter param : parameters) { if (param.getName().equals(paramName)) { Class<?> paramClass = param.getParameterType(); PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass); Object paramValue = accessor.fromString(paramClass, value); q.setParameter(paramName, paramValue); break; } } } } }
From source file:net.lightbody.bmp.proxy.jetty.http.InclusiveByteRange.java
/** * @param headers Enumeration of Range header fields. * @param size Size of the resource.//w ww. ja v a2 s. co m * @return LazyList of satisfiable ranges */ public static List satisfiableRanges(Enumeration headers, long size) { Object satRanges = null; // walk through all Range headers headers: while (headers.hasMoreElements()) { String header = (String) headers.nextElement(); StringTokenizer tok = new StringTokenizer(header, "=,", false); String t = null; try { // read all byte ranges for this header while (tok.hasMoreTokens()) { t = tok.nextToken().trim(); long first = -1; long last = -1; int d = t.indexOf('-'); if (d < 0 || t.indexOf("-", d + 1) >= 0) { if ("bytes".equals(t)) continue; log.warn("Bad range format: " + t); continue headers; } else if (d == 0) { if (d + 1 < t.length()) last = Long.parseLong(t.substring(d + 1).trim()); else { log.warn("Bad range format: " + t); continue headers; } } else if (d + 1 < t.length()) { first = Long.parseLong(t.substring(0, d).trim()); last = Long.parseLong(t.substring(d + 1).trim()); } else first = Long.parseLong(t.substring(0, d).trim()); if (first == -1 && last == -1) continue headers; if (first != -1 && last != -1 && (first > last)) continue headers; if (first < size) { InclusiveByteRange range = new InclusiveByteRange(first, last); satRanges = LazyList.add(satRanges, range); } } } catch (Exception e) { log.warn("Bad range format: " + t); LogSupport.ignore(log, e); } } return LazyList.getList(satRanges, true); }
From source file:com.thoughtworks.go.util.FileUtil.java
public static File normalize(final String path) { Stack s = new Stack(); String[] dissect = dissect(path); s.push(dissect[0]);/*from w w w . j ava 2 s .co m*/ StringTokenizer tok = new StringTokenizer(dissect[1], File.separator); while (tok.hasMoreTokens()) { String thisToken = tok.nextToken(); if (".".equals(thisToken)) { continue; } if ("..".equals(thisToken)) { if (s.size() < 2) { // Cannot resolve it, so skip it. return new File(path); } s.pop(); } else { // plain component s.push(thisToken); } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.size(); i++) { if (i > 1) { // not before the filesystem root and not after it, since root // already contains one sb.append(File.separatorChar); } sb.append(s.elementAt(i)); } return new File(sb.toString()); }
From source file:elaborate.util.StringUtil.java
/** * change ULRs in <code>textWithURLs</code> to links * /*from ww w . ja v a2 s .c om*/ * @param textWithURLs * text with URLs * @return text with links */ public static String activateURLs(String textWithURLs) { StringTokenizer tokenizer = new StringTokenizer(textWithURLs, "<> ", true); StringBuilder replaced = new StringBuilder(textWithURLs.length()); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); // Log.info("token={}", token); try { URL url = new URL(token); // If possible then replace with anchor... String linktext = token; String file = url.getFile(); if (StringUtils.isNotBlank(file)) { linktext = file; } String protocol = url.getProtocol(); if (hostProtocols.contains(protocol)) { linktext = url.getHost() + linktext; } replaced.append("<a target=\"_blank\" href=\"" + url + "\">" + linktext + "</a>"); } catch (MalformedURLException e) { replaced.append(token); } } return replaced.toString(); }