<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Archanciel's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 07 Aug 2008 17:58:59 GMT</pubDate>
    <description>DZone Snippets: Archanciel's Code Snippets</description>
    <item>
      <title>Printing out a Map in sorted order</title>
      <link>http://snippets.dzone.com/posts/show/5283</link>
      <description>// print out a Map in sorted order of its keys&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    public static String dumpToString(Map&lt;String, Object&gt; map) {&lt;br /&gt;        if (map == null) {&lt;br /&gt;            return Constants.EMPTY_STRING;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        StringBuffer strbuf = new StringBuffer();&lt;br /&gt;&lt;br /&gt;        SortedMap&lt;String, Object&gt; sMap = new TreeMap&lt;String, Object&gt;(map);&lt;br /&gt;        Set&lt;Map.Entry&lt;String, Object&gt;&gt; s = sMap.entrySet();&lt;br /&gt;&lt;br /&gt;        for (Map.Entry&lt;String, Object&gt; elem : s) {&lt;br /&gt;            String key = elem.getKey();&lt;br /&gt;            Object value = elem.getValue();&lt;br /&gt;            strbuf.append(key);&lt;br /&gt;            strbuf.append("=");&lt;br /&gt;            strbuf.append(value == null ? Constants.EMPTY_STRING : value);&lt;br /&gt;            strbuf.append("\r\n");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return strbuf.toString();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Mar 2008 09:09:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5283</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Recursively dump imbricated Map of Maps</title>
      <link>http://snippets.dzone.com/posts/show/4531</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    public void dumpMapOfMap(Map map) {&lt;br /&gt;        Set s = map.entrySet();&lt;br /&gt;        Iterator sit = s.iterator();&lt;br /&gt;        boolean isFirst = true;&lt;br /&gt;&lt;br /&gt;        while (sit.hasNext()) {&lt;br /&gt;            Map.Entry elem = (Map.Entry)sit.next();&lt;br /&gt;            String key = (String)elem.getKey();&lt;br /&gt;            Object value = elem.getValue();&lt;br /&gt;&lt;br /&gt;            if (value instanceof String) {&lt;br /&gt;                // recursivity stop condition&lt;br /&gt;                System.out.print(key);&lt;br /&gt;                System.out.print(" : ");&lt;br /&gt;                System.out.println(value);&lt;br /&gt;            } else {&lt;br /&gt;                if (!isFirst) {&lt;br /&gt;                    System.out.println("");&lt;br /&gt;                } else {&lt;br /&gt;                    isFirst = false;&lt;br /&gt;                }&lt;br /&gt;                System.out.println(key);&lt;br /&gt;                Map valueMap = (Map)elem.getValue();&lt;br /&gt;                dumpMapOfMap(valueMap);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 12:58:09 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4531</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Obtaining the value of a public static field by reflexion </title>
      <link>http://snippets.dzone.com/posts/show/4501</link>
      <description>// Obtaining the value of a static public constant of a class, here Boolean for example&lt;br /&gt;&lt;code&gt;&lt;br /&gt;        Object returnedObj = null;&lt;br /&gt;        try {&lt;br /&gt;            Class clazz = Class.forName("java.lang.Boolean");&lt;br /&gt;            returnedObj = clazz.getField("TRUE").get(clazz);&lt;br /&gt;            assert java.lang.Boolean.TRUE == returnedObj : "wrong returned object";&lt;br /&gt;        } catch (ClassNotFoundException e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        } catch (SecurityException e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        } catch (NoSuchFieldException e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        } catch (IllegalArgumentException e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        } catch (IllegalAccessException e) {&lt;br /&gt;            e.printStackTrace();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        return returnedObj;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 06 Sep 2007 14:59:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4501</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Printing out the full path of a resource on the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4489</link>
      <description>// displaying the full path of a resource found by the class loader on the classpath, here the log4j.xml file&lt;br /&gt;&lt;code&gt;&lt;br /&gt;String resourceName = "/log4j.xml"; // pay attention to the leading '/' !&lt;br /&gt;URL location = AnyClass.class.getResource(resourceName);&lt;br /&gt;&lt;br /&gt;if (location != null) {&lt;br /&gt;    System.out.println(location.getPath());&lt;br /&gt;} else {&lt;br /&gt;    System.out.println(resourceName + " not found on the classpath");&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 04 Sep 2007 09:59:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4489</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Loading a text file into a String from the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4480</link>
      <description>Locate a text file in the classpath and read it into a String&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    /** &lt;br /&gt;     * @param filePath      name of file to open. The file can reside&lt;br /&gt;     *                      anywhere in the classpath&lt;br /&gt;     */&lt;br /&gt;    private String readFileAsString(String filePath) throws java.io.IOException {&lt;br /&gt;        StringBuffer fileData = new StringBuffer(1000);&lt;br /&gt;        BufferedReader reader = new BufferedReader(new InputStreamReader(this&lt;br /&gt;            .getClass().getClassLoader().getResourceAsStream(filePath)));&lt;br /&gt;        char[] buf = new char[1024];&lt;br /&gt;        int numRead = 0;&lt;br /&gt;        while ((numRead = reader.read(buf)) != -1) {&lt;br /&gt;            String readData = String.valueOf(buf, 0, numRead);&lt;br /&gt;            fileData.append(readData);&lt;br /&gt;            buf = new char[1024];&lt;br /&gt;        }&lt;br /&gt;        reader.close();&lt;br /&gt;        return fileData.toString();&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 30 Aug 2007 12:48:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4480</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
    <item>
      <title>Loading a property file from the classpath</title>
      <link>http://snippets.dzone.com/posts/show/4479</link>
      <description>Loads a property file located anywhere in the classpath&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    private Properties getPropertiesFromClasspath(String propFileName) throws IOException {&lt;br /&gt;        // loading xmlProfileGen.properties from the classpath&lt;br /&gt;        Properties props = new Properties();&lt;br /&gt;        InputStream inputStream = this.getClass().getClassLoader()&lt;br /&gt;            .getResourceAsStream(propFileName);&lt;br /&gt;&lt;br /&gt;        if (inputStream == null) {&lt;br /&gt;            throw new FileNotFoundException("property file '" + propFileName&lt;br /&gt;                + "' not found in the classpath");&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        props.load(inputStream);&lt;br /&gt;&lt;br /&gt;        return props;&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 30 Aug 2007 12:28:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4479</guid>
      <author>Archanciel (Jean-Pierre Schnyder)</author>
    </item>
  </channel>
</rss>
