Here you can find the source of addMessagesToMap(final Map
private static void addMessagesToMap(final Map<String, String> hangmanMap, String messagesURIString) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*ww w . ja v a 2 s . c o m*/ * Boris Bokowski, IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class Main { private static void addMessagesToMap(final Map<String, String> hangmanMap, String messagesURIString) throws Exception { // System.out.println("Loading message bundle from: " + // messagesURIString); DefaultHandler handler = new DefaultHandler() { String name; String value; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { name = attributes.getValue("name"); } public void characters(char[] ch, int start, int length) throws SAXException { value = new String(ch, start, length); } public void endElement(String uri, String localName, String qName) throws SAXException { if ("msg".equalsIgnoreCase(qName) && name != null && value != null) { hangmanMap.put("__MSG_" + name + "__", value); } } }; parseXML(new java.net.URI(messagesURIString), handler); } private static void addMessagesToMap(Map<String, String> hangmanMap, Map<String, String> messages) { for (Map.Entry<String, String> entry : messages.entrySet()) { hangmanMap.put("__MSG_" + entry.getKey() + "__", entry.getValue()); } } private static void parseXML(java.net.URI uri, DefaultHandler handler) throws MalformedURLException, IOException, ParserConfigurationException, SAXException { URL messagesURL = uri.toURL(); URLConnection connection = messagesURL.openConnection(); String encoding = connection.getContentEncoding(); if (encoding == null) { String contentType = connection.getHeaderField("Content-Type"); int charsetIndex; if (contentType != null && (charsetIndex = contentType.indexOf("charset=")) != -1) { int charsetBegin = charsetIndex + "charset=".length(); int charsetEnd = charsetBegin; while (contentType.length() > charsetEnd && " ,;".indexOf(contentType.charAt(charsetEnd)) == -1) { charsetEnd++; } if (charsetEnd > charsetBegin) { encoding = contentType.substring(charsetBegin, charsetEnd); } } } InputStream is = connection.getInputStream(); try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = spf.newSAXParser(); InputSource source = new InputSource(is); if (encoding != null) { source.setEncoding(encoding); } parser.parse(source, handler); } finally { is.close(); } } }