XML file i have is
<xml>
<ticket>
<team>A</team>
<imp>I1</imp>
</ticket>
<ticket>
<team>A</team>
<imp>I2</imp>
</ticket>
<ticket>
<team>b</team>
<imp>I2</imp>
</ticket>
<ticket>
<team>A</team>
<imp>I1</imp>
</ticket>
<ticket>
<team>B</team>
<imp>I2</imp>
</ticket>
<ticket>
<team>c</team>
<imp>I1</imp>
ticketcount.java
package com.asn.model;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import com.asn.model.ticket;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.asn.model.ticket;
public class ticketcount {
public static void main(String[] args) {
List<ticket> ticketList = new ArrayList<ticket>();
try {
String Path = "C:\\Users\\";
File fXmlFile = new File(Path + "\\ticket.xml");
// File fXmlFile = new File(App.class.getClassLoader().getResource("C:\\Users\// \
// \tickets.xml").getFile());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList ticketNodeList = doc.getElementsByTagName("ticket");
for (int temp = 0; temp < ticketNodeList.getLength(); temp++) {
Node varNode = ticketNodeList.item(temp);
if (varNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) varNode;
NodeList teamList = eElement.getElementsByTagName("team");
NodeList varsionList = eElement.getElementsByTagName("imp");
Node teamNode = teamList.item(0);
Node impNode = varsionList.item(0);
if (teamNode.getNodeType() == Node.ELEMENT_NODE && impNode.getNodeType() ==
Node.ELEMENT_NODE) {
Element teamElement = (Element) teamNode;
Element impElement = (Element) impNode;
ticket ticket = new ticket(teamElement.getTextContent(),
impElement.getTextContent());
ticketList.add(ticket);
}
}
String content1 =
"<HTML><HEAD></HEAD><TABLE><tr><td>Chain</td><td><b>Priority</b></td<td></td><td>count</td><
/ tr > ";
String content = "";
File file = new File(Path + "\\result1.html");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
}
} catch (Exception e) {
e.printStackTrace();
}
Map<ticket, Integer> count = new HashMap<ticket, Integer>();
for (ticket c : ticketList)
if (!count.containsKey(c))
count.put(c, Collections.frequency(ticketList, c));
List<String> imps = getimps(count);
List<String> teams = getteams(count);
StringBuilder heading = new StringBuilder("ticket \t| ");
for (String s : imps) {
heading.append(s);
heading.append("\t| ");
}
System.out.println(heading);
System.out.println("---------------------------------");
for (String m : teams) {
System.out.println(m + "\t| " + getNumOfteams(m, imps, count));
content = content + "<tr><td>" + m + "</td><td>" + getNumOfteams(m, imps, count) + "</td></tr>";
}
bw.write(content1 + content + "</TABLE></HTML>");
bw.close();
Runtime rTime = Runtime.getRuntime();
String url = Path + "result.html";
//String url = "C:\\Users\\a561922\\Desktop\\TEST.html";//"D:/hi.html";
String browser = "C:/Program Files/Internet Explorer/iexplore.exe ";
File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());
// Process pc = rTime.exec(browser + url);
// pc.waitFor();
//Runtime.getRuntime().exec("C:\\Users\\a561922\\Desktop\\TEST.html");
}
private static List<String> getteams(Map<ticket, Integer> count) {
List<String> teams = new ArrayList<String>();
for (Map.Entry<ticket, Integer> ent : count.entrySet())
if (!teams.contains(ent.getKey().getteam()))
teams.add(ent.getKey().getteam());
return teams;
}
private static String getNumOfteams(String team, List<String> imps, Map<ticket, Integer>
count) {
StringBuilder builder = new StringBuilder();
for (String v : imps) {
Integer cnt = count.get(new ticket(team, v));
if (cnt == null) {
cnt = 0;
}
builder.append(cnt + "\t");
}
return builder.toString();
}
private static List<String> getimps(Map<ticket, Integer> count) {
List<String> imps = new ArrayList<String>();
for (Map.Entry<ticket, Integer> ent : count.entrySet())
if (!imps.contains(ent.getKey().getimp()))
imps.add(ent.getKey().getimp());
return imps;
}
}
ticket.java
package com.asn.model;
public class ticket {
private String team;
private String imp;
public ticket(String team, String imp) {
super();
this.team = team;
this.imp = imp;
}
public String getteam() {
return team;
}
public void setteam(String team) {
this.team = team;
}
public String getimp() {
return imp;
}
public void setimp(String imp) {
this.imp = imp;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((team == null) ? 0 : team.hashCode());
result = prime * result + ((imp == null) ? 0 : imp.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ticket other = (ticket) obj;
if (team == null) {
if (other.team != null)
return false;
} else if (!team.equals(other.team))
return false;
if (imp == null) {
if (other.imp != null)
return false;
} else if (!imp.equals(other.imp))
return false;
return true;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("incident [team=");
builder.append(team);
builder.append(", imp=");
builder.append(imp);
builder.append("]");
return builder.toString();
}
}
you can do like this...change Path according to you
package com.asn.model;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ticketcount {
public static void main(String[] args) throws IOException {
List<ticket> ticketList = new ArrayList<ticket>();
String content = "";
String content1 ="<HTML><HEAD></HEAD><TABLE border=3>";
FileWriter fw =null;
BufferedWriter bw=null;
String Path = "src";
try {
File fXmlFile = new File(Path + "\\file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList ticketNodeList = doc.getElementsByTagName("ticket");
for (int temp = 0; temp < ticketNodeList.getLength(); temp++) {
Node varNode = ticketNodeList.item(temp);
if (varNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) varNode;
NodeList teamList = eElement.getElementsByTagName("team");
NodeList varsionList = eElement.getElementsByTagName("imp");
Node teamNode = teamList.item(0);
Node impNode = varsionList.item(0);
if (teamNode.getNodeType() == Node.ELEMENT_NODE
&& impNode.getNodeType() ==
Node.ELEMENT_NODE) {
Element teamElement = (Element) teamNode;
Element impElement = (Element) impNode;
ticket ticket = new ticket(
teamElement.getTextContent(),
impElement.getTextContent());
ticketList.add(ticket);
}
}
File file = new File(Path + "\\result1.html");
if (!file.exists()) {
file.createNewFile();
}
fw = new FileWriter(file.getAbsoluteFile());
bw = new BufferedWriter(fw);
}
} catch (Exception e) {
e.printStackTrace();
}
Map<ticket, Integer> count = new HashMap<ticket, Integer>();
for (ticket c : ticketList)
if (!count.containsKey(c))
count.put(c, Collections.frequency(ticketList, c));
List<String> imps = getimps(count);
List<String> teams = getteams(count);
content=content+"<tr><th>ticket</th> ";
for (String s : imps) {
content=content+"<th>"+s+"</th>";
}
content=content+"</tr>";
System.out.println("---------------------------------");
for (String m : teams) {
System.out.println(m + "\t| " + getNumOfteams(m, imps, count));
content = content + "<tr><td>" + m + "</td>"
+ getNumOfteams(m, imps, count) + "</tr>";
}
bw.write(content1 + content + "</TABLE></HTML>");
bw.close();
Runtime rTime = Runtime.getRuntime();
String url = Path + "//result1.html";
// String url = "C:\\Users\\a561922\\Desktop\\TEST.html";//"D:/hi.html";
String browser = "C:/Program Files/Internet Explorer/iexplore.exe ";
File htmlFile = new File(url);
System.out.println(url);
Desktop.getDesktop().browse(htmlFile.toURI());
// Process pc = rTime.exec(browser + url);
// pc.waitFor();
// Runtime.getRuntime().exec("C:\\Users\\a561922\\Desktop\\TEST.html");
}
private static List<String> getteams(Map<ticket, Integer> count) {
List<String> teams = new ArrayList<String>();
for (Map.Entry<ticket, Integer> ent : count.entrySet())
if (!teams.contains(ent.getKey().getteam()))
teams.add(ent.getKey().getteam());
return teams;
}
private static String getNumOfteams(String team, List<String> imps,
Map<ticket, Integer>
count) {
StringBuilder builder = new StringBuilder();
for (String v : imps) {
Integer cnt = count.get(new ticket(team, v));
if (cnt == null) {
cnt = 0;
}
builder.append("<td>"+cnt + "</td>");
}
return builder.toString();
}
private static List<String> getimps(Map<ticket, Integer> count) {
List<String> imps = new ArrayList<String>();
for (Map.Entry<ticket, Integer> ent : count.entrySet())
if (!imps.contains(ent.getKey().getimp()))
imps.add(ent.getKey().getimp());
return imps;
}
}
Now the Output :-
I think this the output what you want ... Let me know if u face any issues
Related
I created below class with getter and setter.
import java.util.List;
public class DMNDetails {
private List<String> inputParam;
private List<String> outputParam;
public List<String> getInputParam() {
return inputParam;
}
public void setInputParam(List<String> inputParam) {
this.inputParam = inputParam;
}
public List<String> getOutputParam() {
return outputParam;
}
public void setOutputParam(List<String> outputParam) {
this.outputParam = outputParam;
}
#Override
public String toString() {
return ("Inputname: "+ getInputParam()+
" Outputname: "+ getOutputParam());
}
}
Extracting input and output variables from XML and return using List.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ExtractingValues {
public DMNDetails Param() {
List<String> inputval = new ArrayList<String>();
DMNDetails dtls = new DMNDetails();
String variables;
// String dataType;
try {
File file = new File("C:/test.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
// System.out.println("Root element: "+ doc.getDocumentElement().getNodeName());
NodeList nodeList = doc.getElementsByTagName("inputExpression");
for (int i = 0; i < nodeList.getLength(); ++i) {
Node node = nodeList.item(i);
// System.out.println("\nNode Name :" + node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element tElement = (Element)node;
variables = tElement.getElementsByTagName("text").item(0).getTextContent();
// dataType = tElement.getAttribute("typeRef");
// System.out.println("Variable: "+ variables);
// System.out.println("Type: "+ dataType);
switch(i) {
case 0:
inputval.add(variables);
// System.out.println("Input1: "+dtls1.getInput1());
break;
case 1:
inputval.add(variables);
// System.out.println("Input2: "+dtls2.getInput2());
break;
case 2:
inputval.add(variables);
// System.out.println("Input3: "+dtls3.getInput3());
break;
default:
System.out.println("Error");
}
}
}
}
catch (Exception e) {
System.out.println(e);
}
dtls.setInputParam(inputval);
/*** OutputVariable Section ***/
List<String> outputval = new ArrayList<String>();
String variablesout;
// String dataType;
try {
File file = new File("C:/Jira_task/Sprint_17/Business.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
// System.out.println("Root element: "+ doc.getDocumentElement().getNodeName());
NodeList nodeList2 = doc.getElementsByTagName("output");
for (int i = 0; i < nodeList2.getLength(); ++i) {
Node node2 = nodeList2.item(i);
// System.out.println("\nNode Name :" + node2.getNodeName());
if (node2.getNodeType() == Node.ELEMENT_NODE) {
Element tElement = (Element)node2;
variablesout = tElement.getAttribute("name");
// dataType = tElement.getAttribute("typeRef");
// System.out.println("Variable: "+ variables);
// System.out.println("Type: "+ dataType);
switch(i) {
case 0:
outputval.add(variablesout);
// System.out.println("Output1: "+dtls1.getOutput1());
break;
case 1:
outputval.add(variablesout);
// System.out.println("Output2: "+dtls2.getOutput2());
break;
case 2:
outputval.add(variablesout);
// System.out.println("Output3: "+dtls3.getOutput3());
break;
default:
System.out.println("Error");
}
}
}
}
catch (Exception e) {
System.out.println(e);
}
dtls.setOutputParam(outputval);
return dtls;
}
}
Here we are get data from "TestCase.xlsx" excel and store in data list and return. My expectation are written inside commented block inside.
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelTestCase2 {
public static List<DMNRow> getdata() throws IOException {
List<DMNRow> data = new ArrayList<>();
// Reading file from local directory
FileInputStream file = new FileInputStream("C:\\TestCase.xlsx");
// Create Workbook instance holding reference to .xlsx file
XSSFWorkbook XS = new XSSFWorkbook(file);
int numberofsheets = XS.getNumberOfSheets();
for (int i = 0; i < numberofsheets; i++) {
if (XS.getSheetName(i).equalsIgnoreCase("Sheet1")) {
XSSFSheet XSS = XS.getSheetAt(i);
Iterator<Row> r = XSS.iterator();
/* Here input and output variables are hardcode, but cannot able to hardcode value here, because each xml file contain
n number of input and output. So according to n number of variables we need to extract data from excel and store data
in "data". Datatype also change for each variables.
String cr;
long lc;
String arn;
DMNRow DMNRow;
while (r.hasNext()) {
Row row = r.next();
if (row.getRowNum() == 0) {
continue;
}
cr = row.getCell(0).getStringCellValue();
lc = (long) row.getCell(1).getNumericCellValue();
DataFormatter formatter = new DataFormatter();
arn = formatter.formatCellValue(row.getCell(2));
DMNRow = new DMNRow(cr, lc, arn);
data.add(DMNRow);
*/
}
}
}
// Closing file output streams
XS.close();
file.close();
return data;
}
}
After data from excel store and return to another method to compare data with DMN engine and write status in same excel in Result column.
Can anyone please guide how to store data from excel into list to use by another method for compare.
Though I import all packages it can't work. I am new in java.
Exception in thread "main"
java.lang.NoClassDefFoundError:org/apache/commons/collections4/ListValuedMap
at demo.ReadAndPrintXMLFile.main(ReadAndPrintXMLFile.java:101) Caused
by:java.lang.ClassNotFoundException: org.apache.commons.collections4.
package demo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class ReadAndPrintXMLFile {
private static XSSFWorkbook workbook;
public static void main(String argv[]) {
ArrayList<String> firstNames = new ArrayList<String>();
ArrayList<String> lastNames = new ArrayList<String>();
ArrayList<String> ages = new ArrayList<String>();
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("D:/amit/book.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
System.out.println("Root element of the doc is :\" "+ doc.getDocumentElement().getNodeName() + "\"");
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for (int s = 0; s < listOfPersons.getLength(); s++)
{
Node firstPersonNode = listOfPersons.item(s);
if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE)
{
Element firstPersonElement = (Element) firstPersonNode;
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element) firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : "+ ((Node) textFNList.item(0)).getNodeValue().trim());
firstNames.add(((Node) textFNList.item(0)).getNodeValue().trim());
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element) lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : "+ ((Node) textLNList.item(0)).getNodeValue().trim());
lastNames.add(((Node) textLNList.item(0)).getNodeValue().trim());
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element) ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : "+ ((Node) textAgeList.item(0)).getNodeValue().trim());
ages.add(((Node) textAgeList.item(0)).getNodeValue().trim());
}// end of if clause
}// end of for loop with s var
for(String firstName:firstNames)
{
System.out.println("firstName : "+firstName);
}
for(String lastName:lastNames)
{
System.out.println("lastName : "+lastName);
}
for(String age:ages)
{
System.out.println("age : "+age);
}
}
catch (SAXParseException err)
{
System.out.println("** Parsing error" + ", line "+ err.getLineNumber() + ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
}
catch (SAXException e)
{
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
}
catch (Throwable t)
{
t.printStackTrace();
}
workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sample sheet");
Map<String, Object[]> data = new HashMap<String, Object[]>();
for(int i=0;i<firstNames.size();i++)
{
data.put(i+"",new Object[]{firstNames.get(i),lastNames.get(i),ages.get(i)});
}
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object[] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
if (obj instanceof Date)
cell.setCellValue((Date) obj);
else if (obj instanceof Boolean)
cell.setCellValue((Boolean) obj);
else if (obj instanceof String)
cell.setCellValue((String) obj);
else if (obj instanceof Double)
cell.setCellValue((Double) obj);
}
}
try {
FileOutputStream out = new FileOutputStream(new File("D:/amit/book1.xlsx"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}// end of main
}
You are missing a dependency to org.apache.commons.collections4. This might be because of your other dependencies (which should have the collections4 dependency in theirs, but apparently there is still some sort of error).
You can try to add the JAR to the path, or if you use any build tools (Maven, Gradle, Ivy, etc.) you can add it to the dependencies list. Try and add this dependency: https://mvnrepository.com/artifact/org.apache.commons/commons-collections4/4.1
Currently I try to get the cosine similarity between two document with Lucene (4.10.4).
I already read this answer about cosine similarity with Lucene , and I used this example to understand how it works with Lucene.
But when I tested with 2 same words per each document (ex: "Hello world"), I've got a cosine of similarity at 0.9999999999999998
My code look like that:
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.math3.linear.ArrayRealVector;
import org.apache.commons.math3.linear.RealVector;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.DocsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;
public class CosineSimeTest {
static String indexName = "/tmp/CosineExample";
public static final String CONTENT = "field";
public static final int N = 2;
private final Set<String> terms = new HashSet<>();
private final RealVector v1;
private final RealVector v2;
public static void main(String[] args) {
try {
CosineSimeTest cosSim = new CosineSimeTest("hello world", "hello world");
System.out.println(cosSim.getCosineSimilarity());
} catch (IOException e) {
e.printStackTrace();
}
}
public CosineSimeTest(String s1, String s2) throws IOException {
Directory directory = createIndex(s1, s2);
IndexReader reader = DirectoryReader.open(directory);
Map<String, Double> f1 = getWieghts(reader, 0);
Map<String, Double> f2 = getWieghts(reader, 1);
reader.close();
v1 = toRealVector(f1);
System.out.println("V1: " + v1);
v2 = toRealVector(f2);
System.out.println("V2: " + v2);
}
public Directory createIndex(String s1, String s2) throws IOException {
File f = new File(indexName);
if (f.exists()) {
FileUtils.deleteDirectory(f);
}
Directory directory = FSDirectory.open(new File(indexName));
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(null, analyzer);
IndexWriter writer = new IndexWriter(directory, iwc);
addDocument(writer, s1);
addDocument(writer, s2);
writer.close();
return directory;
}
public void addDocument(IndexWriter writer, String data) throws IOException {
Document doc = new Document();
FieldType type = new FieldType();
type.setIndexed(true);
type.setStoreTermVectors(true);
type.setStoreTermVectorPositions(true);
type.freeze();
Field field = new Field(CONTENT, data, type);
doc.add(field);
writer.addDocument(doc);
}
public double getCosineSimilarity() {
double dotProduct = v1.dotProduct(v2);
System.out.println("Dot: " + dotProduct);
System.out.println("V1_norm: " + v1.getNorm() + ", V2_norm: " + v2.getNorm());
double normalization = (v1.getNorm() * v2.getNorm());
System.out.println("Norm: " + normalization);
return dotProduct / normalization;
}
public Map<String, Double> getWieghts(IndexReader reader, int docId) throws IOException {
Terms vector = reader.getTermVector(docId, CONTENT);
Map<String, Integer> docFrequencies = new HashMap<>();
Map<String, Integer> termFrequencies = new HashMap<>();
Map<String, Double> tf_Idf_Weights = new HashMap<>();
TermsEnum termsEnum = null;
DocsEnum docsEnum = null;
termsEnum = vector.iterator(termsEnum);
BytesRef text = null;
while ((text = termsEnum.next()) != null) {
String term = text.utf8ToString();
docFrequencies.put(term, reader.docFreq(new Term(CONTENT, term)));
docsEnum = termsEnum.docs(null, null);
while (docsEnum.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
termFrequencies.put(term, docsEnum.freq());
}
terms.add(term);
}
for (String term : docFrequencies.keySet()) {
int tf = termFrequencies.get(term);
int df = docFrequencies.get(term);
double idf = (1 + Math.log(N) - Math.log(df));
double w = tf * idf;
tf_Idf_Weights.put(term, w);
}
// System.out.println("Printing docFrequencies:");
// printMap(docFrequencies);
//
// System.out.println("Printing termFrequencies:");
// printMap(termFrequencies);
//
// System.out.println("Printing if/idf weights:");
// printMapDouble(tf_Idf_Weights);
return tf_Idf_Weights;
}
public RealVector toRealVector(Map<String, Double> map) {
RealVector vector = new ArrayRealVector(terms.size());
int i = 0;
double value = 0;
for (String term : terms) {
if (map.containsKey(term)) {
value = map.get(term);
} else {
value = 0;
}
vector.setEntry(i++, value);
}
return vector;
}
public static void printMap(Map<String, Integer> map) {
for (String key : map.keySet()) {
System.out.println("Term: " + key + ", value: " + map.get(key));
}
}
public static void printMapDouble(Map<String, Double> map) {
for (String key : map.keySet()) {
System.out.println("Term: " + key + ", value: " + map.get(key));
}
}
public void getVersionOfLucene(StandardAnalyzer analyzer) {
System.out.println("version : " + analyzer.getVersion());
}
}
What is the problem ? How to fix this ?
Thanks in advance.
I would like to split my Sitemap into Sitemaps, if it has more than maxURLs. The following example should split the Sitemap, if it has more than one url.
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.CharacterData;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SiteMapSplitter {
public static void main(String[] args){
String sitemapStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" +
"<url>\n" +
"<loc>test1.html</loc>\n" +
"<lastmod>today</lastmod>\n" +
"<changefreq>daily</changefreq>\n" +
"<priority>1.0</priority>\n" +
"</url>\n" +
"<url>\n" +
"<loc>test2.html</loc>\n" +
"<lastmod>yesterday</lastmod>\n" +
"<changefreq>daily</changefreq>\n" +
"<priority>1.0</priority>\n" +
"</url></urlset>";
try {
splitSitemap(sitemapStr);
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
static private void splitSitemap(String sitemapStr) throws ParserConfigurationException {
DocumentBuilder db = null;
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(sitemapStr));
Document doc = null;
try {
doc = db.parse(is);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
NodeList nodes = doc.getElementsByTagName("url");
int maxURLs = 1;
Set<String> smURLsSet= new HashSet<String>();
if (nodes.getLength()>maxURLs){
for (int i = 0; i < nodes.getLength(); i++) {
StringBuilder smURLsBuilder = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
for (int k = 0; k<maxURLs; k++){
Element element = (Element) nodes.item(i);
smURLsBuilder.append(element);
}
smURLsSet.add(smURLsBuilder.toString());
}
Iterator i = smURLsSet.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}
}
The problem is that Element element = (Element) nodes.item(i); smURLsBuilder.append(element);
does not append the whole element (in this case the url and its childreen) to the smURLsBuilder. How to do this?
You should consider using an object oriented approach to the sitemap. Either with data binding (JAXB) or even shorter using data projection (Disclosure: I'm affiliated with that project). This way you do not need to create the XML by string concatenation.
public class SitemapSplitter {
static String sitemapStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" +
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" +
"<url>\n" +
"<loc>test1.html</loc>\n" +
"<lastmod>today</lastmod>\n" +
"<changefreq>daily</changefreq>\n" +
"<priority>1.0</priority>\n" +
"</url>\n" +
"<url>\n" +
"<loc>test2.html</loc>\n" +
"<lastmod>yesterday</lastmod>\n" +
"<changefreq>daily</changefreq>\n" +
"<priority>1.0</priority>\n" +
"</url></urlset>";
public interface Sitemap {
#XBWrite("/urlset/url")
Sitemap setUrls(List<? extends Node> urls);
}
public static void main(String... args) {
XBProjector projector = new XBProjector(Flags.TO_STRING_RENDERS_XML);
// Get all urls from existing sitemap.
List<Node> urlNodes = projector.onXMLString(sitemapStr).evalXPath("/xbdefaultns:urlset/xbdefaultns:url").asListOf(Node.class);
for (Node urlNode: urlNodes) {
// Create a new sitemap, here with only one url
Sitemap newSitemap = projector.onXMLString(sitemapStr).createProjection(Sitemap.class).setUrls(Collections.singletonList(urlNode));
System.out.println(newSitemap);
}
}
}
This program prints out
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>test1.html</loc>
<lastmod>today</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</urlset>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>test2.html</loc>
<lastmod>yesterday</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</urlset>
I already have this code as below, but the setName in TypeDeclaration cannot take a String as a argument, it needs a SimpleName object, while I don't know how to get a SimpleName object, maybe this idea is wrong? After all, what I need is to change the Java class name, any solutions?
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str.toCharArray()); //str is the code of .java file
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
List types = cu.types();
TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class
System.out.println("className:" + typeDec.getName());
//SimpleName sn = new SimpleName();
//sn.setIdentifier("aqaa"); //change the class name to "aqaa", but this code fails
//typeDec.setName(sn);
if we change the last 3 sentences into:
SimpleName sn = typeDec.getName();
sn.setIdentifier("aqaa");
typeDec.setName(sn);
The last setName will throw an exception:
importName: java.awt.BorderLayout start: 61 length: 29Exception in thread "main" java.lang.IllegalArgumentException
className: NI
at org.eclipse.jdt.core.dom.ASTNode.checkNewChild(ASTNode.java:1873)
at org.eclipse.jdt.core.dom.ASTNode.preReplaceChild(ASTNode.java:1935)
at org.eclipse.jdt.core.dom.AbstractTypeDeclaration.setName(AbstractTypeDeclaration.java:155)
at org.catr.JavaRefiner.parse(JavaRefiner.java:52)
at org.catr.JavaRefiner.ParseFilesInDir(JavaRefiner.java:130)
at org.catr.JavaRefiner.main(JavaRefiner.java:142)
Maybe I should post the whole class, so you guys could try the code on your own:)
package org.catr;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ImportDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
public class JavaRefiner
{
static int a;
//use ASTParse to parse string
public static void parse(String str)
{
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
List imports = cu.imports();
ImportDeclaration importDec = (ImportDeclaration) imports.get(0);
System.out.println("importName: " + importDec.getName() + " start: " + importDec.getStartPosition() + " length: " + importDec.getLength());
List types = cu.types();
TypeDeclaration typeDec = (TypeDeclaration) types.get(0);
System.out.println("className: " + typeDec.getName());
SimpleName sn = typeDec.getName();
sn.setIdentifier("NII");
typeDec.setName(sn);
cu.accept(new ASTVisitor()
{
Set<String> names = new HashSet<String>();
public boolean visit( VariableDeclarationStatement state)
{
List<VariableDeclarationFragment> frags = state.fragments();
Type type = state.getType();
System.out.println("Type:\t\t\t'" + type + "'\t\t\tat line " + cu.getLineNumber(type.getStartPosition()));
for (VariableDeclarationFragment frag: frags)
{
visit2(frag);
}
return false;
}
public boolean visit2(VariableDeclarationFragment node)
{
SimpleName name = node.getName();
this.names.add(name.getIdentifier());
System.out.println("Declaration:\t\t'" + name + "'\t\t\tat line "
+ cu.getLineNumber(name.getStartPosition()));
return false; // do not continue
}
public boolean visit(SimpleName node)
{
if (this.names.contains(node.getIdentifier()))
{
System.out.println("Usage:\t\t\t'" + node + "'\t\t\tat line "
+ cu.getLineNumber(node.getStartPosition()));
}
return true;
}
});
}
//read file content into a string
public static String readFileToString(String filePath) throws IOException
{
StringBuilder fileData = new StringBuilder(1000);
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1)
{
//System.out.println(numRead);
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
return fileData.toString();
}
//loop directory to get file list
public static void ParseFilesInDir() throws IOException
{
File dirs = new File(".");
String dirPath = dirs.getCanonicalPath() + File.separator + "data" + File.separator;
File root = new File(dirPath);
//System.out.println(rootDir.listFiles());
File[] files = root.listFiles ( );
String filePath = null;
for (File f : files )
{
filePath = f.getAbsolutePath();
if (f.isFile())
{
parse(readFileToString(filePath));
}
else
{
a = 0;
a = a + 1;
}
}
}
public static void main(String[] args) throws IOException
{
ParseFilesInDir();
}
}
Use following code:
List types = cu.types();
TypeDeclaration typeDec = (TypeDeclaration) types.get(0); //typeDec is the class
System.out.println("className:" + typeDec.getName());
SimpleName sn = typeDec.getName();
sn.setIdentifier("aqaa");
typeDec.setName(sn);