Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Java.temp1-master/MapSort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SORT JAVA MAP BY KEYS AND VALUES
--------------------------------

* "Key,Value" order in Java HashMap is not guaranteed.
* Sorting a Map type class by "keys" is given by TreeMap, just TreeMap.putAll(map) will return sorted map by keys.
* Sorting a Map by "values" needs one more step, we need to define a Comparator that compares values to each other.

19 changes: 19 additions & 0 deletions Java.temp1-master/MapSort/src/com/hmkcode/MapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.hmkcode;


import java.util.Map;
import java.util.TreeMap;

public class MapSort {

public static Map sortByValue(Map unsortedMap){
Map sortedMap = new TreeMap(new ValueComparator(unsortedMap));
sortedMap.putAll(unsortedMap);
return sortedMap;
}
public static Map sortByKey(Map unsortedMap){
Map sortedMap = new TreeMap();
sortedMap.putAll(unsortedMap);
return sortedMap;
}
}
28 changes: 28 additions & 0 deletions Java.temp1-master/MapSort/src/com/hmkcode/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.hmkcode;


import java.util.HashMap;
import java.util.Map;

public class Test {
public static void main(String[] args){


Map map = new HashMap();

//*value Class should implements the Comparable interface
//*String implements Comparable by default.

map.put("Z", "3");
map.put("D", "4");
map.put("A", "1");
map.put("B", "2");
map.put("F", "6");
map.put("E", "5");

System.out.println("Unsorted Map: "+map);
System.out.println("Sorted Map By Values: "+MapSort.sortByValue(map));
System.out.println("Sorted Map By Keys: "+MapSort.sortByKey(map));

}
}
25 changes: 25 additions & 0 deletions Java.temp1-master/MapSort/src/com/hmkcode/ValueComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.hmkcode;

import java.util.Comparator;
import java.util.Map;

public class ValueComparator implements Comparator {

Map map;

public ValueComparator(Map map){
this.map = map;
}
public int compare(Object keyA, Object keyB){

Comparable valueA = (Comparable) map.get(keyA);
Comparable valueB = (Comparable) map.get(keyB);

System.out.println(valueA +" - "+valueB);

return valueA.compareTo(valueB);

}


}
61 changes: 61 additions & 0 deletions Java.temp1-master/castor-xml-object/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="var" path="M2_REPO/org/codehaus/castor/castor/1.2/castor-1.2.jar"/>
<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
<classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.8.1/xercesImpl-2.8.1.jar"/>
<classpathentry kind="var" path="M2_REPO/xml-apis/xml-apis/1.3.03/xml-apis-1.3.03.jar"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
11 changes: 11 additions & 0 deletions Java.temp1-master/castor-xml-object/article.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<article>
<categories xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Java</categories>
<tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Java</tags>
<tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Castor</tags>
<tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">XML</tags>
<tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Marshalling</tags>
<tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Unmarshalling</tags>
<url>http://hmkcode.com/castor-java-object-xml</url>
<title>Castor - Java Object to XML &amp; XML to Object Mapping</title>
</article>
10 changes: 10 additions & 0 deletions Java.temp1-master/castor-xml-object/mapped_article.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<article title="Castor - Java Object to XML &amp; XML to Object Mapping">
<url>http://hmkcode.com/castor-java-object-xml</url>
<category>Java</category>
<tag>Java</tag>
<tag>Castor</tag>
<tag>XML</tag>
<tag>Marshalling</tag>
<tag>Unmarshalling</tag>
</article>
19 changes: 19 additions & 0 deletions Java.temp1-master/castor-xml-object/mapping.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?><!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.org/mapping.dtd">
<mapping>
<class name="com.hmkcode.vo.Article">
<map-to xml="article" />
<field name="title" type="string">
<bind-xml name="title" node="attribute"/>
</field>
<field name="url" type="string">
<bind-xml name="url" node="element"/>
</field>
<field name="categories" type="string" collection="arraylist">
<bind-xml name="category" />
</field>
<field name="tags" type="string" collection="arraylist">
<bind-xml name="tag" />
</field>
</class>
</mapping>
31 changes: 31 additions & 0 deletions Java.temp1-master/castor-xml-object/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.hmkcode</groupId>
<artifactId>castor-xml-object</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>castor-xml-object</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.1</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.hmkcode;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;

import com.hmkcode.vo.Article;

public class App
{
public static void main( String[] args )
{

try {

//( 1 ) OBJECT --> XML
FileWriter writer = new FileWriter("article.xml");
Marshaller.marshal(createArticle(), writer);
writer.close();


//( 2 ) XML --> OBJECT
FileReader reader = new FileReader("article.xml");
Article article = (Article) Unmarshaller.unmarshal(Article.class, reader);

System.out.println(article);


} catch (IOException e) {
e.printStackTrace();
} catch (MarshalException e) {
e.printStackTrace();
} catch (ValidationException e) {
e.printStackTrace();
}

}

public static Article createArticle(){
Article article = new Article();

article.setTitle("Castor - Java Object to XML & XML to Object Mapping");
article.setUrl("http://hmkcode.com/castor-java-object-xml");
article.addCategory("Java");
article.addTag("Java");
article.addTag("Castor");
article.addTag("XML");
article.addTag("Marshalling");
article.addTag("Unmarshalling");

return article;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.hmkcode;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.exolab.castor.xml.XMLContext;

import com.hmkcode.vo.Article;


public class AppMapping
{
public static void main( String[] args )
{

try {
//Load Mapping
Mapping mapping = new Mapping();
mapping.loadMapping("mapping.xml");
XMLContext context = new XMLContext();
context.addMapping(mapping);

//( 1 ) OBJECT --> XML

//1.1 Prepare file writer
Writer writer = new FileWriter("mapped_article.xml");

//1.2 Create a new Marshaller
Marshaller marshaller = context.createMarshaller();
marshaller.setWriter(writer);

//1.3 Marshal "map" to xml
marshaller.marshal(createArticle());

//1.4
writer.close();


//( 2 ) XML --> OBJECT

//2.1 Prepare file writer
Reader reader = new FileReader("mapped_article.xml");

//2.2 Create a new Unmarshaller
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setClass(Article.class);

//2.3 Unmarshal "map" to Object
Article article = (Article) unmarshaller.unmarshal(reader);

//2.4
reader.close();

System.out.println(article);

} catch (IOException e1) {
e1.printStackTrace();
} catch (MappingException e1) {
e1.printStackTrace();
} catch (MarshalException e) {
e.printStackTrace();
} catch (ValidationException e) {
e.printStackTrace();
}
}
public static Article createArticle(){
Article article = new Article();

article.setTitle("Castor - Java Object to XML & XML to Object Mapping");
article.setUrl("http://hmkcode.com/castor-java-object-xml");
article.addCategory("Java");
article.addTag("Java");
article.addTag("Castor");
article.addTag("XML");
article.addTag("Marshalling");
article.addTag("Unmarshalling");

return article;
}
}
Loading