0
以下是代碼有兩個類FieldMap和Xfrmr_new -------------------------- -------------------------------------------------- -----------------<identifier>預計錯誤。無法解決的問題
package javax.xml.xpath;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.lang.Object;
import org.apache.commons.io.FileUtils;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Text;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.*;
import org.jdom2.*;
import org.jdom2.filter.*;
import org.jdom2.xpath;
class FieldMap
{
private String xpath;
private XPathExpression<Object> xPathExp;
public FieldMap()
{
setXpathRoot();
setXpathData();
}
public String getXpath()
{
return xpath;
}
public String getLeafName()
{
if (xpath != null && xpath.isEmpty() == false)
{
return xpath.substring(xpath.lastIndexOf("/") + 1,xpath.length());
} else return null;
}
public void setXpath(String xpath)
{
this.xpath = xpath;
xPathExp = XPathFactory.instance().compile(xpath);
}
public void setXpath (String xpath, Integer collectionCount)
{
this.xpath = xpath;
if (collectionCount > 0 && xpath != null && xpath.isEmpty() == false && xpath.contains("/"))
{
xpath = xpath.substring(0, xpath.lastIndexOf("/"));
}
xPathExp = XPathFactory.instance().compile(xpath);
}
public XPathExpression<Object> getXpathExp()
{
return xPathExp;
}
private ValueType valueType;
public ValueType getValueType()
{
return valueType;
}
public void setValueType(ValueType valueType)
{
this.valueType = valueType;
}
private String dataType;
public String getDataType()
{
return dataType;
}
public void setDataType(String dataType)
{
this.dataType = dataType;
}
private String format;
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
private int collectionCount;
public int getCollectionCount() {
return collectionCount;
}
public void setCollectionCount(int collectionCount) {
this.collectionCount = collectionCount;
}
private String outputName;
public String getOutputName() {
return outputName;
}
public void setOutputName(String outputName) {
this.outputName = outputName;
}
private String delimiter;
public String getDelimiter() {
return delimiter;
}
public void setDelimiter(String delimiter) {
this.delimiter = delimiter;
}
private String xpathNormalize;
public String getXpathNormalize() {
return xpath;
}
private XPathExpression<Element> xPathExpNormalize;
public void setXpathNormalize (String xpath) {
xPathExpNormalize = XPathFactory.instance().compile(xpath,Filters.element());
}
public XPathExpression<Element> getXpathExpToNormalize(){
return xPathExpNormalize;
}
private XPathExpression<Document> xPathExpRoot;
private void setXpathRoot()
{
xPathExpRoot = XPathFactory.instance().compile("/",Filters.document());
System.out.println("Inside setXpathExpRoot"+xPathExpRoot);
}
public XPathExpression<Document> getXpathRoot()
{
return xPathExpRoot;
}
private XPathExpression<Document> xPathExpData;
private void setXpathData()
{
xPathExpData= XPathFactory.instance().compile("/",Filters.document());
System.out.println("Inside setXpathData"+xPathExpData);
}
public XPathExpression<Document> getXpathData()
{
return xPathExpData;
}
public enum ValueType
{
String, NUMERIC
}
private String outputData;
public String getOutputData()
{
return outputData;
}
public void setOutputData(String outputData)
{
this.outputData = outputData;
}
//private XPathExpression<Element> xPathExp;
private XPathExpression<Object> xPathNew;
private String xpathdata;
/*public void setXpathData (String xpathdata) {
this.xpathdata = xpathdata;
xPathNew = XPathFactory.instance().compile(xpathdata);
}*/
public void setXpathData (String xpathdata, Integer collectionCount)
{
this.xpathdata = xpathdata;
if (collectionCount > 0 && xpathdata != null
&& xpathdata.isEmpty() == false
&& xpathdata.contains("/"))
{
/*in the case where we want to create different columns for the same field name
* this sets the xpath to the parent of the leaf
*it is intended to keep the order
*/
xpathdata = xpathdata.substring(0, xpathdata.lastIndexOf("/"));
}
xPathNew = XPathFactory.instance().compile(xpathdata);
}
public XPathExpression<Object> getXpathNew()
{
return xPathNew;
}
public String getLeafNameNew()
{
if (xpathdata != null && xpathdata.isEmpty() == false)
{
return xpathdata.substring(xpathdata.lastIndexOf("/") + 1,xpathdata.length());
} else
{
return null;
}
}
}
public class Xfrmr_new
{
private static String ELEMENT = "ELEMENT";
private static String ATTRIBUTE = "ATTRIBUTE";
private static String STRING = "STRING";
private static String TEXT = "TEXT";
private static String DOUBLE = "DOUBLE";
private static String BOOLEAN = "BOOLEAN";
private static List<FieldMap> fields;
// Create a hash map
public static HashMap<String,String> hm = new HashMap<String,String>();
public static HashMap<String,String> hmn = new HashMap<String,String>();
public Xfrmr_new(List<FieldMap> fields){
Xfrmr_new.fields = fields;
}
public static String transform(byte[] payload){
try {
SAXBuilder sax = new SAXBuilder();
Document doc = sax.build(new ByteArrayInputStream(payload));
return transform(doc);
} catch (Exception e){
return null;
}
}
public static String transform(Document xmldoc)
{
StringBuilder sb = new StringBuilder("");
List<String> escapes = new ArrayList<String>();
escapes.add("\n");
escapes.add(fields.get(0).getDelimiter());
if (fields.get(0).getXpathExpToNormalize() == null)
{
//single row per pcn
Document root = fields.get(0).getXpathRoot().evaluateFirst(xmldoc);
sb.append(transformOneRow(root.getRootElement(),escapes));
} else
{
//System.out.println("Inside Normalize On");
//multiple rows per pcn
List<Element> elements = fields.get(0).getXpathExpToNormalize().evaluate(xmldoc);
for (Element element : elements)
{
sb.append(transformOneRow(element,escapes));
//sb.append("\t1"); //add a tab and 1 to match context.write
sb.append(System.getProperty("line.separator"));
//System.out.println("inside norm"+sb);
}
//get rid of the last tab,1,& newline
if (sb.length()>0)
{
//int newLineSize = System.getProperty("line.separator").length();
//sb.replace(sb.length()-(2+newLineSize),sb.length(),"");
}
}
return sb.toString();
}
public static String transformOneRow(Element element,List<String> escapes)
{
StringBuilder sb = new StringBuilder("");
String nameValue="";;
String dataValue="";
int pswFlag = 0;
int deltaFlag = 0;
int expFlag = 0;
int teltrkFlag = 0;
int sbdFlag = 0;
int digFlag = 0;
int hmFlag = 0;
int hvdFlag = 0;
for (FieldMap f : fields)
{
if (f.getCollectionCount() <= 0)
{
try
{
Object obj = f.getXpathExp().evaluateFirst(element);
List<Object> objs = f.getXpathExp().evaluate(element);
Element prnts = (Element)objs.get(0);
//System.out.println("prints print check"+getValue(prnts.getChild(f.getLeafName()),escapes));
System.out.println("prints check"+prnts.getValue());
if(prnts.getValue().equals("PSW01") && f.getOutputName().equals("Suite_id")){
pswFlag = 1;
System.out.println("inside psw01 if");
}
if(prnts.getValue().equals("DELTA01") && f.getOutputName().equals("Suite_id")){
deltaFlag = 1;
System.out.println("inside Delta01 if");
}
if(prnts.getValue().equals("EXP01") && f.getOutputName().equals("Suite_id")){
expFlag = 1;
System.out.println("inside exp01 if");
}
System.out.println("expFlag in first loop"+expFlag);
if(prnts.getValue().equals("TELTRK01") && f.getOutputName().equals("Suite_id")){
teltrkFlag = 1;
System.out.println("inside Teltrk01 if");
}
if(prnts.getValue().equals("SBD0001") && f.getOutputName().equals("Suite_id")){
sbdFlag = 1;
System.out.println("inside sbd0001 if");
}
if(prnts.getValue().equals("DIGITAL") && f.getOutputName().equals("Suite_id")){
digFlag = 1;
System.out.println("inside Digital if");
}
if(prnts.getValue().equals("HVD0001") && f.getOutputName().equals("Suite_id")){
hvdFlag = 1;
System.out.println("inside HVD0001 if");
}
sb.append(getValue(obj,escapes));
sb.append(f.getDelimiter());
//System.out.println("Fields outputname without CC:- "+f.getOutputName());
//System.out.println("NO CC objects:"+obj);
//System.out.println("NO CC SB:"+sb);
} catch (Exception e){
sb.append(f.getDelimiter());
}
}
else
{
System.out.println("inside else coll count>0");
int ctr = 0;
List<Object> objects = f.getXpathExp().evaluate(element);
List<Object> objectsNew = f.getXpathNew().evaluate(element);
//String occurrances[] = new String[f.getCollectionCount()];
//System.out.println("obj size :-"+objects.size());
//System.out.println("obj new size :-"+objectsNew.size());
for (ctr=0 ;ctr<f.getCollectionCount();ctr++){
try
{
/*if (objects.size() >= ctr){*/
if (10 >= ctr){
Element prnt = (Element)objects.get(ctr);
nameValue = getValue(prnt.getChild(f.getLeafName()),escapes);
System.out.println("else part nameValue"+nameValue);
System.out.println("get output name"+f.getOutputName());
System.out.println("exp flag"+expFlag);
//if(expFlag==1 && getValue(prnt.getChild(f.getLeafName()),escapes).equals("SSN-UNIQ-IND-1") &&
if(pswFlag==1 && nameValue.equals("SW-ADR-MATCH-IND-1") && f.getOutputName().equals("TagName")){
System.out.println("inside SW-ADR-MATCH-IND-1 if");
hm.put(nameValue,"");
}
if(pswFlag==1 && nameValue.equals("SW-AMT-BL-XFR-1") && f.getOutputName().equals("TagName")){
System.out.println("inside SW-AMT-BL-XFR-1 if");
hm.put(nameValue,"");
}
if(pswFlag==1 && nameValue.equals("SW-AMT-BL-XFR12M-1") && f.getOutputName().equals("TagName")){
System.out.println("inside SW-AMT-BL-XFR12M-1 if");
hm.put(nameValue,"");
}
if(pswFlag==1 && nameValue.equals("SW-AV-PAY-3M-1") && f.getOutputName().equals("TagName")){hm.put(nameValue,"");}
if(hvdFlag==1 && nameValue.equals("SOW-PRNT-1-NETWRTH-1") && f.getOutputName().equals("TagName")){hm.put(nameValue,"");}
if(hvdFlag==1 && nameValue.equals("SOW-PRNT-2-NETWRTH-1") && f.getOutputName().equals("TagName")){hm.put(nameValue,"");}
if(hvdFlag==1 && nameValue.equals("SOW-COLL-TIER-1") && f.getOutputName().equals("TagName")){hm.put(nameValue,"");}
if(hvdFlag==1 && nameValue.equals("HVD-INCOME-360-1") && f.getOutputName().equals("TagName")){hm.put(nameValue,"");}
System.out.println("hm flag"+hmFlag);
sb.append(getValue(prnt.getChild(f.getLeafName()),escapes));
sb.append(f.getDelimiter());
//System.out.println("getchild output :-"+prnt.getChild(f.getLeafName()));
//System.out.println("getleafname output :-"+ f.getLeafName());
//System.out.println("Fields outputname with CC :- "+f.getOutputName());
System.out.println("getvalue output :-"+getValue(prnt.getChild(f.getLeafName()),escapes));
//System.out.println("inside if:"+sb);
}
}catch (Exception e){
sb.append(f.getDelimiter());
//System.out.println("inside catch1");
//e.printStackTrace();
}
try{
/*if (objectsNew.size() >= ctr){*/
if (10 >= ctr){
Element prntNew = (Element)objectsNew.get(ctr);
//System.out.println("prntNew :"+prntNew);
dataValue = getValue(prntNew.getChild(f.getLeafNameNew()),escapes);
Object v = hm.get(nameValue);
if (v != null){
hmn.put(nameValue,dataValue);
}
sb.append(getValue(prntNew.getChild(f.getLeafNameNew()),escapes));
sb.append(f.getDelimiter());
//System.out.println("inside xpathNew sb : "+sb);
//System.out.println("getchild output second:-"+prntNew.getChild(f.getLeafNameNew()));
//System.out.println("getleafname output second :-"+ f.getLeafNameNew());
//System.out.println("Fields outputname with CC second :- "+f.getOutputData());
//System.out.println("getvalue output second :-"+getValue(prntNew.getChild(f.getLeafNameNew()),escapes));
//System.out.println("inside if:"+sb);
}
}catch (Exception e){
sb.append(f.getDelimiter());
//System.out.println("inside catch2");
//e.printStackTrace();
}
}
//System.out.println("CC objects:"+objects);
//System.out.println("CC SB:"+sb);
// Get a set of the entries
Set<Entry<String, String>> set = hm.entrySet();
// Get an iterator
Iterator<Entry<String, String>> i = set.iterator();
// Display elements
while(i.hasNext()) {
Map.Entry me = i.next();
System.out.print("key : " + me.getKey() + ": ");
System.out.println("value : " + me.getValue());
}
/*Set<Entry<String, String>> setn = hmn.entrySet();
Iterator<Entry<String, String>> in = setn.iterator();
while(in.hasNext()) {
Map.Entry men = in.next();
System.out.print("key : " + men.getKey() + ": ");
System.out.println("value : " + men.getValue());
}*/
}
}
return sb.toString();
}
private static String getValue(Object obj, List<String> escapes){
if (obj == null) {
return "";
}
String type = obj.getClass().toString();
type = type.substring(type.lastIndexOf(".")+1,type.length());
if (type.toUpperCase().equals(ELEMENT)) {
Element element = (Element)obj;
if (element.getTextTrim() != null) {
return CleanUp(element.getTextTrim(),escapes);
}
}else if (type.toUpperCase().equals(ATTRIBUTE)) {
Attribute attribute = (Attribute)obj;
if (attribute.getValue() != null) {
return CleanUp(attribute.getValue(),escapes);
}
}else if (type.toUpperCase().equals(STRING)) {
return CleanUp(obj.toString(),escapes);
}else if (type.toUpperCase().equals(TEXT)) {
Text text = (Text)obj;
return CleanUp(text.getText(),escapes);
}else if (type.toUpperCase().equals(DOUBLE)) {
return CleanUp(obj.toString(),escapes);
}else if (type.toUpperCase().equals(BOOLEAN)) {
return CleanUp(obj.toString(),escapes);
}else {
return null;
}
return "";
}
private static String CleanUp(String value, List<String> listToReplace){
for (String replace : listToReplace){
value = value.replace(replace, " ");
}
return value;
}
public static String Header(){
StringBuilder sb = new StringBuilder("");
for (FieldMap field : fields){
if(field.getCollectionCount() > 0) {
for (int i=0; i<field.getCollectionCount();i++){
sb.append(field.getOutputName() + "_" + i);
sb.append(field.getDelimiter());
sb.append(field.getOutputData() + "_" + i);
sb.append(field.getDelimiter());
}
}
else {
sb.append(field.getOutputName() + field.getDelimiter());
}
}
return sb.toString();
}
public static List<FieldMap> GetXfrmDetails(String mappingFile) {
List<FieldMap> fields = new ArrayList<FieldMap>();
SAXBuilder builder = new SAXBuilder();
File xmlFile = new File(mappingFile);
try {
Document document = (Document) builder.build(xmlFile);
Element rootNode = document.getRootElement();
List<Element> list = rootNode.getChildren("Table");
for (int i = 0; i < list.size(); i++) {
Element node = list.get(i);
FieldMap field = new FieldMap();
String childVal;
childVal = node.getChildTextTrim("collectionCount");
if (childVal != null && childVal.isEmpty() == false) {
field.setCollectionCount(Integer.parseInt(childVal));
}
field.setDataType(node.getChildTextTrim("DataType"));
field.setFormat(node.getChildTextTrim("formatString"));
field.setOutputName(node.getChildTextTrim("outputName"));
field.setOutputData(node.getChildTextTrim("outputData"));
childVal = node.getChildTextTrim("DataType");
if (childVal.equals("NUMERIC")) {
field.setValueType(FieldMap.ValueType.NUMERIC);
} else {
field.setValueType(FieldMap.ValueType.String);
}
//System.out.println("collec count"+field.getCollectionCount());
if (field.getCollectionCount() > 0) {
field.setXpath(node.getChildTextTrim("xPath"), field. getCollectionCount());
//System.out.println("CC xpathdata"+node.getChildTextTrim("xPathData"));
field.setXpathData(node.getChildTextTrim("xPathData"),field. getCollectionCount());
}else {
field.setXpath(node.getChildTextTrim("xPath"));
}
field.setDelimiter(node.getChildTextTrim("ReportDelimiter"));
if (node.getChildTextTrim("xPathToNormalizeOn") != null) {
field.setXpathNormalize(node.getChildTextTrim("xPathToNormalizeOn"));
}
fields.add(field);
}
} catch (IOException io) {
System.out.println("error in io");
System.out.println(io.getMessage());
} catch (JDOMException jdomex) {
System.out.println("error in jdom");
System.out.println(jdomex.getMessage());
} /*catch (Exception e) {
System.out.println("error message");
System.out.println(e.getMessage());
}*/
return fields;
}
public static void main(String[] args) throws IOException {
List<FieldMap> fields;
Xfrmr_new xfrm;
fields = Xfrmr_new.GetXfrmDetails("/mdl/us4/quetool/QUEDATA/OPEN_XML/SBE_Inquiry/input/SBEInquiry.xml");
File directory = new File("/mdl/us4/quetool/QUEDATA/OPEN_XML/SBE_Inquiry/inquiry_pcn");
System.out.println("looking for input files in " + directory.getAbsolutePath());
File[] files = directory.listFiles();
File ofile = new File("/mdl/us4/quetool/QUEDATA/OPEN_XML/SBE_Inquiry/output/inquiry_output.csv");
FileOutputStream fop = new FileOutputStream(ofile);
int count=0;
for(File file : files){
byte[] xml;
xml = FileUtils.readFileToByteArray(file);
xfrm = new Xfrmr_new(fields);
// if file doesnt exists, then create it
if (!ofile.exists()) {
ofile.createNewFile();
}
//add the header record
if (count==0){
fop.write((Xfrmr_new.Header() + System.getProperty("line.separator")).getBytes());
count++;
}
fop.write((Xfrmr_new.transform(xml)).getBytes());
fop.write((System.getProperty("line.separator")).getBytes());
}
Set<Entry<String, String>> setn = hmn.entrySet();
// Get an iterator
Iterator<Entry<String, String>> in = setn.iterator();
// Display elements
while(in.hasNext()) {
Map.Entry men = in.next();
System.out.print("final key : " + men.getKey() + ": ");
System.out.println("final value : " + men.getValue());
}
System.out.println("Its done!! :)");
}
}
我收到以下錯誤,當我運行的代碼
Xfrmr_new.java:51: <identifier> expected
private XPathExpression<Object> xPathExp;
^
Xfrmr_new.java:92: <identifier> expected
public XPathExpression<Object> getXpathExp()
^
Xfrmr_new.java:169: <identifier> expected
private XPathExpression<Element> xPathExpNormalize;
^
Xfrmr_new.java:174: <identifier> expected
public XPathExpression<Element> getXpathExpToNormalize(){
^
Xfrmr_new.java:186: <identifier> expected
public XPathExpression<Document> getXpathRoot()
^
Xfrmr_new.java:199: <identifier> expected
public XPathExpression<Document> getXpathData()
^
Xfrmr_new.java:225: <identifier> expected
private XPathExpression<Object> xPathNew;
^
Xfrmr_new.java:252: <identifier> expected
public XPathExpression<Object> getXpathNew()
^
Xfrmr_new.java:267: ';' expected
^
Xfrmr_new.java:280: <identifier> expected
private static List<FieldMap> fields;
^
Xfrmr_new.java:282: <identifier> expected
public static HashMap<String,String> hm = new HashMap<String,String>();
^
Xfrmr_new.java:283: <identifier> expected
public static HashMap<String,String> hmn = new HashMap<String,String>();
^
Xfrmr_new.java:287: <identifier> expected
public Xfrmr_new(List<FieldMap> fields){
^
Xfrmr_new.java:878: ')' expected
}
^
14 errors
我添加了以下jar文件在我的類路徑
commons-cli-1.2.jar
commons-compress-1.2.jar
commons-io-2.4.jar
hadoop-common-0.21.0.jar
java-rt-jar-stubs-1.5.0.jar
jaxen-1.1.2.jar
jdom-2.0.4.jar
jdom-2.0.4-contrib.jar
jgoodies-common-1.1.1.jar
jgoodies-forms-1.4.1.jar
您需要使用Java 1.5或更高版本進行編譯,以允許您使用泛型。 – 2014-09-01 14:21:23
您將哪個版本的Java編譯爲? – 2014-09-01 14:22:20
版本是1.6.0_65 – sweetpoison 2014-09-01 14:27:30