Pages

Tuesday 27 September 2011

Convert Java Map to String


http://java.dzone.com/articles/two-ways-convert-java-map

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class MapUtil {
 public static String mapToString(Map<String, String> map) {
   StringBuilder stringBuilder = new StringBuilder();

   for (String key : map.keySet()) {
    if (stringBuilder.length() > 0) {
     stringBuilder.append("&");
    }
    String value = map.get(key);
    try {
     stringBuilder.append((key != null ? URLEncoder.encode(key, "UTF-8") : ""));
     stringBuilder.append("=");
     stringBuilder.append(value != null ? URLEncoder.encode(value, "UTF-8") : "");
    } catch (UnsupportedEncodingException e) {
     throw new RuntimeException("This method requires UTF-8 encoding support", e);
    }
   }

   return stringBuilder.toString();
  }

  public static Map<String, String> stringToMap(String input) {
   Map<String, String> map = new HashMap<String, String>();

   String[] nameValuePairs = input.split("&");
   for (String nameValuePair : nameValuePairs) {
    String[] nameValue = nameValuePair.split("=");
    try {
     map.put(URLDecoder.decode(nameValue[0], "UTF-8"), nameValue.length > 1 ? URLDecoder.decode(
     nameValue[1], "UTF-8") : "");
    } catch (UnsupportedEncodingException e) {
     throw new RuntimeException("This method requires UTF-8 encoding support", e);
    }
   }

   return map;
  }
}

Example usage

Map<String, String> map = new HashMap<String, String>();
map.put("color", "red");
map.put("symbols", "{,=&*?}");
map.put("empty", "");
String output = MapUtil.mapToString(map);
Map<String, String> parsedMap = MapUtil.stringToMap(output);
for (String key : map.keySet()) {
 Assert.assertEquals(parsedMap.get(key), map.get(key));
}

Monday 19 September 2011

Linux / Unix Command: rsync

Linux / Unix Command: rsync

rsync -avz foo:src/bar/ /data/tmp


this would recursively transfer all files from the directory src/bar on the machine foo into the /data/tmp/bar directory on the local machine. The files are transferred in "archive" mode, which ensures that symbolic links, devices, attributes, permissions, ownerships etc are preserved in the transfer. Additionally, compression will be used to reduce the size of data portions of the transfer.

ApacheMySQLPHP

Friday 9 September 2011

Eclipse hangs while loading workbench on Ubuntu

Eclipse was hanging on startup while loading the workbench. I tried various solutions and eventually got it to load by changing the version of java used

sudo update-alternatives --config java 

 Original stack trace was

Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
    at org.eclipse.swt.SWT.error(SWT.java:3884)
    at org.eclipse.swt.SWT.error(SWT.java:3799)
    at org.eclipse.swt.SWT.error(SWT.java:3770)
    at org.eclipse.swt.widgets.Widget.error(Widget.java:450)
    at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:389)
    at org.eclipse.swt.widgets.Control.isVisible(Control.java:3168)
    at org.eclipse.swt.widgets.ProgressBar.timerProc(ProgressBar.java:276)
    at org.eclipse.swt.widgets.Display.windowTimerProc(Display.java:4209)
    at org.eclipse.equinox.launcher.JNIBridge._takedown_splash(Native Method)
    at org.eclipse.equinox.launcher.JNIBridge.takeDownSplash(JNIBridge.java:138)
    at org.eclipse.equinox.launcher.Main.takeDownSplash(Main.java:1940)
    at org.eclipse.equinox.launcher.Main$SplashHandler.run(Main.java:109)

Thursday 1 September 2011

Propeller Hat

This is Sean McMains' technical blog, where he writes about matters that would bore his wife too much to post on his personal site. Its focus is largely on information about Magnolia CMS, for which he went to work in March 2011.

Freemarker

FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.
FreeMarker is designed to be practical for the generation of HTML Web pages, particularly by servlet-based applications following the MVC (Model View Controller) pattern. The idea behind using the MVC pattern for dynamic Web pages is that you separate the designers (HTML authors) from the programmers. Everybody works on what they are good at. Designers can change the appearance of a page without programmers having to change or recompile code, because the application logic (Java programs) and page design (FreeMarker templates) are separated. Templates do not become polluted with complex program fragments. This separation is useful even for projects where the programmer and the HTML page author is the same person, since it helps to keep the application clear and easily maintainable.
Although FreeMarker has some programming capabilities, it is not a full-blown programming language like PHP. Instead, Java programs prepare the data to be displayed (like issue SQL queries), and FreeMarker just generates textual pages that display the prepared data using templates.

Querying and Searching using JCR

Querying and Searching using JCR
The JCR API defines a way to query a repository for content that meets user-defined criteria. The JCR 2.0 API actually makes it possible for implementations to support multiple query languages, and the specification requires support for two languages: JCR-SQL2 and JCR-QOM. JCR 1.0 defined two other languages (XPath and JCR-SQL), though these languages were deprecated in JCR 2.0.

XPathJCR-SQL2
//*
SELECT * FROM [nt:base]
//element(*,my:type)
SELECT * FROM [my:type]
/jcr:root/nodes/element(*,my:type)
SELECT * FROM [my:type] 
WHERE PATH([my:type])> LIKE '/nodes/%'
 AND DEPTH([my:type]) = CAST(2 AS LONG)
/jcr:root/nodes//element(*,my:type)
SELECT * FROM [my:type] 
WHERE PATH([my:type]) LIKE '/nodes/%'
/jcr:root/nodes//element(ex:nodeName,my:type)
SELECT * FROM [my:type] 
WHERE PATH([my:type]) LIKE '/nodes/%'
 AND NAME([my:type]) = 'ex:nodeName'

Magnolia JCR Cheat Sheet

JCR 2.0 SQL-2 Grammar

These railroad diagrams are based on the JCR 2.0 specification.

examples from Jackrabbit's test cases:


Find pages using any paragraph in Magnolia