Wednesday, November 28, 2007

Java

Install on Ubuntu.

Main install:
"sudo aptitude install sun-java5-jdk"
Set up Sun's JVM as default:
"sudo update-java-alternatives -s java-1.5.0-sun" or "sudo gedit /etc/jvm"

For eXo java/jdk1.5
Set up system environments for java snd maven within your home dir .profile file
==========
export JAVA_HOME=/home/alexey/java/jdk1.5
export MAVEN_HOME=/home/alexey/java/maven2
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH
==========
Links official and not official


Here some usefull java code pieces would be posted.

Collection to Array:
vector -> string array
String[] strArray = vector.toArray(new String [vector.size()]);
list -> string array
String[] strArray = list.toArray(new String [list.size()]);
etc.


Array to Collection:
vector = Arrays.asList(strArray);

Enumeration to Collection
return Collections.list(returnedEnumeration);

Collection to Enumeration
return Collections.enumeration(returnedColection);

How to return Collection
Collection v = new ArrayList();
v.add(...);
return v;


How to return Enumeration
public Enumeration<string> getProperties(String s) {
// creating typified
Vector<string> result = new Vector<string>();
// fill with sample string
result.add("Hello");
// make return statement
return result.elements();
}


How get InputSource from org.w3c.dom.Document?
(link)
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerFactory;

Document doc = ... // your existing DOM document
DOMSource source = new DOMSource(doc);
StringWriter xmlAsWriter = new StringWriter();
StreamResult result = new StreamResult(xmlAsWriter);
TransformerFactory.newInstance().newTransformer().transform(source, result);
StringReader xmlReader = new StringReader(xmlAsWriter.toString());
InputSource viola = new InputSource(xmlReader);



Read and write line in file
---------------------------
BufferedReader in;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream("c:\\in.xml")));
while (in.ready()) {
String s = in.readLine();
System.out.println(s);
}
} catch (IOException e) {
}
---------------------------
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\out.xml",true)));
out.write("hello");
out.newLine();
out.write("test");
out.close();
} catch (IOException e) {
}
---------------------------


Print Exception to print stream (ex.: PrintWriter w = renderResponse.getWriter();)
(new Exception()).printStackTrace(w);
or
StackTraceElement[] ste = Thread.currentThread().getStackTrace();

URL
-
java.net.URL
official
это частный случай java.net.URI official
Example:
url = http://localhost:8080/portal/portal/?portal:componentId=producer1-1556486924@portlets/HelloWorld
protocol http
host localhost
port 8080
getAuthority localhost:8080
getPath /portal/portal/
getQuery portal:componentId=producer1-1556486924@portlets/HelloWorld
getFile /portal/portal/?portal:componentId=producer1-1556486924@portlets/HelloWorld
toExternalForm http://localhost:8080/portal/portal/?portal:componentId=producer1-1556486924@portlets/HelloWorld
getRef null

HttpServletRequest
request.getContextPath() = /portal
request.getServletPath() = /portal/
request.getServerName() = localhost
request.getRequestURL() = http://localhost:8080/portal/portal/
request.getRequestURI() = /portal/portal/

ArrayList converting
Converter applet
Conversion table for Java 1.5+

4 comments:

Sergey said...

In java5 no need to cast (String[]). It will be returned automatically depend of the given type of argument function toArray

Alexey Zavizionov said...

As I understood toArray returns Object[]

Alexey Zavizionov said...

Added "How to return Collection"

Alexey Zavizionov said...

Updated.