jdk/src/share/classes/com/sun/imageio/plugins/png/PNGMetadata.java
changeset 2368 5a872c1edd4f
parent 1734 861400729115
child 2375 bb4dd76ca2c9
equal deleted inserted replaced
1741:f02cad8a9114 2368:5a872c1edd4f
  1038             node.setAttribute("keyword", iTXt_keyword.get(i));
  1038             node.setAttribute("keyword", iTXt_keyword.get(i));
  1039             node.setAttribute("value", iTXt_text.get(i));
  1039             node.setAttribute("value", iTXt_text.get(i));
  1040             node.setAttribute("language",
  1040             node.setAttribute("language",
  1041                               iTXt_languageTag.get(i));
  1041                               iTXt_languageTag.get(i));
  1042             if (iTXt_compressionFlag.get(i)) {
  1042             if (iTXt_compressionFlag.get(i)) {
  1043                 node.setAttribute("compression", "deflate");
  1043                 node.setAttribute("compression", "zip");
  1044             } else {
  1044             } else {
  1045                 node.setAttribute("compression", "none");
  1045                 node.setAttribute("compression", "none");
  1046             }
  1046             }
  1047 
  1047 
  1048             text_node.appendChild(node);
  1048             text_node.appendChild(node);
  1050 
  1050 
  1051         for (int i = 0; i < zTXt_keyword.size(); i++) {
  1051         for (int i = 0; i < zTXt_keyword.size(); i++) {
  1052             node = new IIOMetadataNode("TextEntry");
  1052             node = new IIOMetadataNode("TextEntry");
  1053             node.setAttribute("keyword", (String)zTXt_keyword.get(i));
  1053             node.setAttribute("keyword", (String)zTXt_keyword.get(i));
  1054             node.setAttribute("value", (String)zTXt_text.get(i));
  1054             node.setAttribute("value", (String)zTXt_text.get(i));
  1055             node.setAttribute("compression", "deflate");
  1055             node.setAttribute("compression", "zip");
  1056 
  1056 
  1057             text_node.appendChild(node);
  1057             text_node.appendChild(node);
  1058         }
  1058         }
  1059 
  1059 
  1060         return text_node;
  1060         return text_node;
  1419                         fatal(node,
  1419                         fatal(node,
  1420                               "Only an iTXtEntry may be a child of an iTXt!");
  1420                               "Only an iTXtEntry may be a child of an iTXt!");
  1421                     }
  1421                     }
  1422 
  1422 
  1423                     String keyword = getAttribute(iTXt_node, "keyword");
  1423                     String keyword = getAttribute(iTXt_node, "keyword");
  1424                     iTXt_keyword.add(keyword);
  1424                     if (isValidKeyword(keyword)) {
  1425 
  1425                         iTXt_keyword.add(keyword);
  1426                     boolean compressionFlag =
  1426 
  1427                         getBooleanAttribute(iTXt_node, "compressionFlag");
  1427                         boolean compressionFlag =
  1428                     iTXt_compressionFlag.add(Boolean.valueOf(compressionFlag));
  1428                             getBooleanAttribute(iTXt_node, "compressionFlag");
  1429 
  1429                         iTXt_compressionFlag.add(Boolean.valueOf(compressionFlag));
  1430                     String compressionMethod =
  1430 
  1431                         getAttribute(iTXt_node, "compressionMethod");
  1431                         String compressionMethod =
  1432                     iTXt_compressionMethod.add(Integer.valueOf(compressionMethod));
  1432                             getAttribute(iTXt_node, "compressionMethod");
  1433 
  1433                         iTXt_compressionMethod.add(Integer.valueOf(compressionMethod));
  1434                     String languageTag =
  1434 
  1435                         getAttribute(iTXt_node, "languageTag");
  1435                         String languageTag =
  1436                     iTXt_languageTag.add(languageTag);
  1436                             getAttribute(iTXt_node, "languageTag");
  1437 
  1437                         iTXt_languageTag.add(languageTag);
  1438                     String translatedKeyword =
  1438 
  1439                         getAttribute(iTXt_node, "translatedKeyword");
  1439                         String translatedKeyword =
  1440                     iTXt_translatedKeyword.add(translatedKeyword);
  1440                             getAttribute(iTXt_node, "translatedKeyword");
  1441 
  1441                         iTXt_translatedKeyword.add(translatedKeyword);
  1442                     String text = getAttribute(iTXt_node, "text");
  1442 
  1443                     iTXt_text.add(text);
  1443                         String text = getAttribute(iTXt_node, "text");
       
  1444                         iTXt_text.add(text);
       
  1445 
       
  1446                     }
       
  1447                     // silently skip invalid text entry
  1444 
  1448 
  1445                     iTXt_node = iTXt_node.getNextSibling();
  1449                     iTXt_node = iTXt_node.getNextSibling();
  1446                 }
  1450                 }
  1447             } else if (name.equals("pHYs")) {
  1451             } else if (name.equals("pHYs")) {
  1448                 pHYs_pixelsPerUnitXAxis =
  1452                 pHYs_pixelsPerUnitXAxis =
  1690 
  1694 
  1691             node = node.getNextSibling();
  1695             node = node.getNextSibling();
  1692         }
  1696         }
  1693     }
  1697     }
  1694 
  1698 
  1695     private boolean isISOLatin(String s) {
  1699     /*
       
  1700      * Accrding to PNG spec, keywords are restricted to 1 to 79 bytes
       
  1701      * in length. Keywords shall contain only printable Latin-1 characters
       
  1702      * and spaces; To reduce the chances for human misreading of a keyword,
       
  1703      * leading spaces, trailing spaces, and consecutive spaces are not
       
  1704      * permitted in keywords.
       
  1705      *
       
  1706      * See: http://www.w3.org/TR/PNG/#11keywords
       
  1707      */
       
  1708     private boolean isValidKeyword(String s) {
       
  1709         int len = s.length();
       
  1710         if (len < 1 || len >= 80) {
       
  1711             return false;
       
  1712         }
       
  1713         if (s.startsWith(" ") || s.endsWith(" ") || s.contains("  ")) {
       
  1714             return false;
       
  1715         }
       
  1716         return isISOLatin(s, false);
       
  1717     }
       
  1718 
       
  1719     /*
       
  1720      * According to PNG spec, keyword shall contain only printable
       
  1721      * Latin-1 [ISO-8859-1] characters and spaces; that is, only
       
  1722      * character codes 32-126 and 161-255 decimal are allowed.
       
  1723      * For Latin-1 value fields the 0x10 (linefeed) control
       
  1724      * character is aloowed too.
       
  1725      *
       
  1726      * See: http://www.w3.org/TR/PNG/#11keywords
       
  1727      */
       
  1728     private boolean isISOLatin(String s, boolean isLineFeedAllowed) {
  1696         int len = s.length();
  1729         int len = s.length();
  1697         for (int i = 0; i < len; i++) {
  1730         for (int i = 0; i < len; i++) {
  1698             if (s.charAt(i) > 255) {
  1731             char c = s.charAt(i);
  1699                 return false;
  1732             if (c < 32 || c > 255 || (c > 126 && c < 161)) {
       
  1733                 // not printable. Check whether this is an allowed
       
  1734                 // control char
       
  1735                 if (!isLineFeedAllowed || c != 0x10) {
       
  1736                     return false;
       
  1737                 }
  1700             }
  1738             }
  1701         }
  1739         }
  1702         return true;
  1740         return true;
  1703     }
  1741     }
  1704 
  1742 
  1927             } else if (name.equals("Text")) {
  1965             } else if (name.equals("Text")) {
  1928                 Node child = node.getFirstChild();
  1966                 Node child = node.getFirstChild();
  1929                 while (child != null) {
  1967                 while (child != null) {
  1930                     String childName = child.getNodeName();
  1968                     String childName = child.getNodeName();
  1931                     if (childName.equals("TextEntry")) {
  1969                     if (childName.equals("TextEntry")) {
  1932                         String keyword = getAttribute(child, "keyword");
  1970                         String keyword =
       
  1971                             getAttribute(child, "keyword", "", false);
  1933                         String value = getAttribute(child, "value");
  1972                         String value = getAttribute(child, "value");
  1934                         String encoding = getAttribute(child, "encoding");
  1973                         String language =
  1935                         String language = getAttribute(child, "language");
  1974                             getAttribute(child, "language", "", false);
  1936                         String compression =
  1975                         String compression =
  1937                             getAttribute(child, "compression");
  1976                             getAttribute(child, "compression", "none", false);
  1938 
  1977 
  1939                         if (isISOLatin(value)) {
  1978                         if (!isValidKeyword(keyword)) {
       
  1979                             // Just ignore this node, PNG requires keywords
       
  1980                         } else if (isISOLatin(value, true)) {
  1940                             if (compression.equals("zip")) {
  1981                             if (compression.equals("zip")) {
  1941                                 // Use a zTXt node
  1982                                 // Use a zTXt node
  1942                                 zTXt_keyword.add(keyword);
  1983                                 zTXt_keyword.add(keyword);
  1943                                 zTXt_text.add(value);
  1984                                 zTXt_text.add(value);
  1944                                 zTXt_compressionMethod.add(new Integer(0));
  1985                                 zTXt_compressionMethod.add(Integer.valueOf(0));
  1945                             } else {
  1986                             } else {
  1946                                 // Use a tEXt node
  1987                                 // Use a tEXt node
  1947                                 tEXt_keyword.add(keyword);
  1988                                 tEXt_keyword.add(keyword);
  1948                                 tEXt_text.add(value);
  1989                                 tEXt_text.add(value);
  1949                             }
  1990                             }