Changeset 10866 in josm for trunk/src/com
- Timestamp:
- 2016-08-21T05:59:42+02:00 (8 years ago)
- Location:
- trunk/src/com/kitfox/svg
- Files:
-
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/xml/XMLParseUtil.java
r8084 r10866 38 38 39 39 import com.kitfox.svg.SVGConst; 40 import org.w3c.dom.*;41 40 import java.awt.*; 42 import java.net.*;43 41 import java.util.*; 44 42 import java.util.regex.*; 45 import java.lang.reflect.*;46 43 import java.util.logging.Level; 47 44 import java.util.logging.Logger; … … 61 58 } 62 59 63 /**64 * Scans the tag's children and returns the first text element found65 */66 public static String getTagText(Element ele)67 {68 NodeList nl = ele.getChildNodes();69 int size = nl.getLength();70 71 Node node = null;72 int i = 0;73 for (; i < size; i++)74 {75 node = nl.item(i);76 if (node instanceof Text) break;77 }78 if (i == size || node == null) return null;79 80 return ((Text)node).getData();81 }82 83 /**84 * Returns the first node that is a direct child of root with the coresponding85 * name. Does not search children of children.86 */87 public static Element getFirstChild(Element root, String name)88 {89 NodeList nl = root.getChildNodes();90 int size = nl.getLength();91 for (int i = 0; i < size; i++)92 {93 Node node = nl.item(i);94 if (!(node instanceof Element)) continue;95 Element ele = (Element)node;96 if (ele.getTagName().equals(name)) return ele;97 }98 99 return null;100 }101 102 60 public static String[] parseStringList(String list) 103 61 { 104 // final Pattern patWs = Pattern.compile("\\s+");105 62 final Matcher matchWs = Pattern.compile("[^\\s]+").matcher(""); 106 63 matchWs.reset(list); … … 116 73 } 117 74 118 public static boolean isDouble(String val)119 {120 fpMatch.reset(val);121 return fpMatch.matches();122 }123 124 75 public static double parseDouble(String val) 125 76 { … … 233 184 } 234 185 235 public static float parseFloat(String val)236 {237 /*238 if (val == null) return 0f;239 240 float retVal = 0f;241 try242 { retVal = Float.parseFloat(val); }243 catch (Exception e)244 {}245 return retVal;246 */247 return findFloat(val);248 }249 250 186 /** 251 187 * Searches the given string for the first floating point number it contains, … … 298 234 } 299 235 300 public static int parseInt(String val) 236 /** 237 * Searches the given string for the first integer point number it contains, 238 * parses and returns it. 239 */ 240 public static int findInt(String val) 301 241 { 302 242 if (val == null) return 0; 243 244 intMatch.reset(val); 245 if (!intMatch.find()) return 0; 246 247 val = intMatch.group(); 303 248 304 249 int retVal = 0; … … 310 255 } 311 256 312 /**313 * Searches the given string for the first integer point number it contains,314 * parses and returns it.315 */316 public static int findInt(String val)317 {318 if (val == null) return 0;319 320 intMatch.reset(val);321 if (!intMatch.find()) return 0;322 323 val = intMatch.group();324 //System.err.println("Parsing " + val);325 326 int retVal = 0;327 try328 { retVal = Integer.parseInt(val); }329 catch (Exception e)330 {}331 return retVal;332 }333 334 257 public static int[] parseIntList(String list) 335 258 { … … 355 278 return retArr; 356 279 } 357 /* 358 public static int parseHex(String val) 359 { 360 int retVal = 0; 361 362 for (int i = 0; i < val.length(); i++) 363 { 364 retVal <<= 4; 365 366 char ch = val.charAt(i); 367 if (ch >= '0' && ch <= '9') 368 { 369 retVal |= ch - '0'; 370 } 371 else if (ch >= 'a' && ch <= 'z') 372 { 373 retVal |= ch - 'a' + 10; 374 } 375 else if (ch >= 'A' && ch <= 'Z') 376 { 377 retVal |= ch - 'A' + 10; 378 } 379 else throw new RuntimeException(); 380 } 381 382 return retVal; 383 } 384 */ 280 385 281 /** 386 282 * The input string represents a ratio. Can either be specified as a … … 403 299 404 300 return new NumberWithUnits(val); 405 }406 /*407 public static Color parseColor(String val)408 {409 Color retVal = null;410 411 if (val.charAt(0) == '#')412 {413 String hexStrn = val.substring(1);414 415 if (hexStrn.length() == 3)416 {417 hexStrn = "" + hexStrn.charAt(0) + hexStrn.charAt(0) + hexStrn.charAt(1) + hexStrn.charAt(1) + hexStrn.charAt(2) + hexStrn.charAt(2);418 }419 int hexVal = parseHex(hexStrn);420 421 retVal = new Color(hexVal);422 }423 else424 {425 final Matcher rgbMatch = Pattern.compile("rgb\\((\\d+),(\\d+),(\\d+)\\)", Pattern.CASE_INSENSITIVE).matcher("");426 427 rgbMatch.reset(val);428 if (rgbMatch.matches())429 {430 int r = Integer.parseInt(rgbMatch.group(1));431 int g = Integer.parseInt(rgbMatch.group(2));432 int b = Integer.parseInt(rgbMatch.group(3));433 retVal = new Color(r, g, b);434 }435 else436 {437 Color lookupCol = ColorTable.instance().lookupColor(val);438 if (lookupCol != null) retVal = lookupCol;439 }440 }441 442 return retVal;443 }444 */445 /**446 * Parses the given attribute of this tag and returns it as a String.447 */448 public static String getAttribString(Element ele, String name)449 {450 return ele.getAttribute(name);451 }452 453 /**454 * Parses the given attribute of this tag and returns it as an int.455 */456 public static int getAttribInt(Element ele, String name)457 {458 String sval = ele.getAttribute(name);459 int val = 0;460 try { val = Integer.parseInt(sval); } catch (Exception e) {}461 462 return val;463 }464 465 /**466 * Parses the given attribute of this tag as a hexadecimal encoded string and467 * returns it as an int468 */469 public static int getAttribIntHex(Element ele, String name)470 {471 String sval = ele.getAttribute(name);472 int val = 0;473 try { val = Integer.parseInt(sval, 16); } catch (Exception e) {}474 475 return val;476 }477 478 /**479 * Parses the given attribute of this tag and returns it as a float480 */481 public static float getAttribFloat(Element ele, String name)482 {483 String sval = ele.getAttribute(name);484 float val = 0.0f;485 try { val = Float.parseFloat(sval); } catch (Exception e) {}486 487 return val;488 }489 490 /**491 * Parses the given attribute of this tag and returns it as a double.492 */493 public static double getAttribDouble(Element ele, String name)494 {495 String sval = ele.getAttribute(name);496 double val = 0.0;497 try { val = Double.parseDouble(sval); } catch (Exception e) {}498 499 return val;500 }501 502 /**503 * Parses the given attribute of this tag and returns it as a boolean.504 * Essentially compares the lower case textual value to the string "true"505 */506 public static boolean getAttribBoolean(Element ele, String name)507 {508 String sval = ele.getAttribute(name);509 510 return sval.toLowerCase().equals("true");511 }512 513 public static URL getAttribURL(Element ele, String name, URL docRoot)514 {515 String sval = ele.getAttribute(name);516 517 URL url;518 try519 {520 return new URL(docRoot, sval);521 }522 catch (Exception e)523 {524 return null;525 }526 }527 528 /**529 * Returns the first ReadableXMLElement with the given name530 */531 public static ReadableXMLElement getElement(Class classType, Element root, String name, URL docRoot)532 {533 if (root == null) return null;534 535 //Do not process if not a LoadableObject536 if (!ReadableXMLElement.class.isAssignableFrom(classType))537 {538 return null;539 }540 541 NodeList nl = root.getChildNodes();542 int size = nl.getLength();543 for (int i = 0; i < size; i++)544 {545 Node node = nl.item(i);546 if (!(node instanceof Element)) continue;547 Element ele = (Element)node;548 if (!ele.getTagName().equals(name)) continue;549 550 ReadableXMLElement newObj = null;551 try552 {553 newObj = (ReadableXMLElement)classType.newInstance();554 }555 catch (Exception e)556 {557 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);558 continue;559 }560 newObj.read(ele, docRoot);561 562 if (newObj == null) continue;563 564 return newObj;565 }566 567 return null;568 }569 570 /**571 * Returns a HashMap of nodes that are children of root. All nodes will572 * be of class classType and have a tag name of 'name'. 'key' is573 * an attribute of tag 'name' who's string value will be used as the key574 * in the HashMap575 */576 public static HashMap getElementHashMap(Class classType, Element root, String name, String key, URL docRoot)577 {578 if (root == null) return null;579 580 //Do not process if not a LoadableObject581 if (!ReadableXMLElement.class.isAssignableFrom(classType))582 {583 return null;584 }585 586 HashMap retMap = new HashMap();587 588 NodeList nl = root.getChildNodes();589 int size = nl.getLength();590 for (int i = 0; i < size; i++)591 {592 Node node = nl.item(i);593 if (!(node instanceof Element)) continue;594 Element ele = (Element)node;595 if (!ele.getTagName().equals(name)) continue;596 597 ReadableXMLElement newObj = null;598 try599 {600 newObj = (ReadableXMLElement)classType.newInstance();601 }602 catch (Exception e)603 {604 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);605 continue;606 }607 newObj.read(ele, docRoot);608 609 if (newObj == null) continue;610 611 String keyVal = getAttribString(ele, key);612 retMap.put(keyVal, newObj);613 }614 615 return retMap;616 }617 618 public static HashSet getElementHashSet(Class classType, Element root, String name, URL docRoot)619 {620 if (root == null) return null;621 622 //Do not process if not a LoadableObject623 if (!ReadableXMLElement.class.isAssignableFrom(classType))624 {625 return null;626 }627 628 HashSet retSet = new HashSet();629 630 NodeList nl = root.getChildNodes();631 int size = nl.getLength();632 for (int i = 0; i < size; i++)633 {634 Node node = nl.item(i);635 if (!(node instanceof Element)) continue;636 Element ele = (Element)node;637 if (!ele.getTagName().equals(name)) continue;638 639 ReadableXMLElement newObj = null;640 try641 {642 newObj = (ReadableXMLElement)classType.newInstance();643 }644 catch (Exception e)645 {646 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);647 continue;648 }649 newObj.read(ele, docRoot);650 651 if (newObj == null)652 {653 continue;654 }655 656 retSet.add(newObj);657 }658 659 return retSet;660 }661 662 663 public static LinkedList getElementLinkedList(Class classType, Element root, String name, URL docRoot)664 {665 if (root == null) return null;666 667 //Do not process if not a LoadableObject668 if (!ReadableXMLElement.class.isAssignableFrom(classType))669 {670 return null;671 }672 673 NodeList nl = root.getChildNodes();674 LinkedList elementCache = new LinkedList();675 int size = nl.getLength();676 for (int i = 0; i < size; i++)677 {678 Node node = nl.item(i);679 if (!(node instanceof Element)) continue;680 Element ele = (Element)node;681 if (!ele.getTagName().equals(name)) continue;682 683 ReadableXMLElement newObj = null;684 try685 {686 newObj = (ReadableXMLElement)classType.newInstance();687 }688 catch (Exception e)689 {690 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e);691 continue;692 }693 newObj.read(ele, docRoot);694 695 elementCache.addLast(newObj);696 }697 698 return elementCache;699 }700 701 public static Object[] getElementArray(Class classType, Element root, String name, URL docRoot)702 {703 if (root == null) return null;704 705 //Do not process if not a LoadableObject706 if (!ReadableXMLElement.class.isAssignableFrom(classType))707 {708 return null;709 }710 711 LinkedList elementCache = getElementLinkedList(classType, root, name, docRoot);712 713 Object[] retArr = (Object[])Array.newInstance(classType, elementCache.size());714 return elementCache.toArray(retArr);715 }716 717 /**718 * Takes a number of tags of name 'name' that are children of 'root', and719 * looks for attributes of 'attrib' on them. Converts attributes to an720 * int and returns in an array.721 */722 public static int[] getElementArrayInt(Element root, String name, String attrib)723 {724 if (root == null) return null;725 726 NodeList nl = root.getChildNodes();727 LinkedList elementCache = new LinkedList();728 int size = nl.getLength();729 730 for (int i = 0; i < size; i++)731 {732 Node node = nl.item(i);733 if (!(node instanceof Element)) continue;734 Element ele = (Element)node;735 if (!ele.getTagName().equals(name)) continue;736 737 String valS = ele.getAttribute(attrib);738 int eleVal = 0;739 try { eleVal = Integer.parseInt(valS); }740 catch (Exception e) {}741 742 elementCache.addLast(new Integer(eleVal));743 }744 745 int[] retArr = new int[elementCache.size()];746 Iterator it = elementCache.iterator();747 int idx = 0;748 while (it.hasNext())749 {750 retArr[idx++] = ((Integer)it.next()).intValue();751 }752 753 return retArr;754 }755 756 /**757 * Takes a number of tags of name 'name' that are children of 'root', and758 * looks for attributes of 'attrib' on them. Converts attributes to an759 * int and returns in an array.760 */761 public static String[] getElementArrayString(Element root, String name, String attrib)762 {763 if (root == null) return null;764 765 NodeList nl = root.getChildNodes();766 LinkedList elementCache = new LinkedList();767 int size = nl.getLength();768 769 for (int i = 0; i < size; i++)770 {771 Node node = nl.item(i);772 if (!(node instanceof Element)) continue;773 Element ele = (Element)node;774 if (!ele.getTagName().equals(name)) continue;775 776 String valS = ele.getAttribute(attrib);777 778 elementCache.addLast(valS);779 }780 781 String[] retArr = new String[elementCache.size()];782 Iterator it = elementCache.iterator();783 int idx = 0;784 while (it.hasNext())785 {786 retArr[idx++] = (String)it.next();787 }788 789 return retArr;790 }791 792 /**793 * Takes a CSS style string and retursn a hash of them.794 * @param styleString - A CSS formatted string of styles. Eg,795 * "font-size:12;fill:#d32c27;fill-rule:evenodd;stroke-width:1pt;"796 */797 public static HashMap parseStyle(String styleString) {798 return parseStyle(styleString, new HashMap());799 301 } 800 302
Note:
See TracChangeset
for help on using the changeset viewer.