class MediaWikiUtil {

    public static WikiTemplate extractTemplate(String markup) {
        Validate.notNull(markup, "markup must not be null");
        
        Map<String, String> properties = new LinkedHashMap<String, String>();
        // trim surrounding {{ and }}
        String content = markup.substring(2, markup.length() - 2);
        String templateName = getTemplateName(content);

        // in case of geobox, we must remove the first | character
        if (markup.toLowerCase().startsWith("{{geobox")) {
            content = markup.substring(markup.indexOf('|') + 1, markup.length() - 2);
        }

        int i = 0;
        for (String part : splitTemplateMarkup(content)) {
            String key = String.valueOf(i++);
            int equalIdx = part.indexOf('=');
            if (equalIdx > 0) {
                String potentialKey = part.substring(0, equalIdx);
                if (isBracketBalanced(potentialKey) && isTagBalanced(potentialKey)) {
                    key = part.substring(0, equalIdx).trim();
                } else {
                    equalIdx = -1;
                }
            }
            String value = part.substring(equalIdx + 1).trim();
            properties.put(key, value);
        }

        return new WikiTemplate(templateName, properties);
    }

    private  MediaWikiUtil();

}