Ver Mensaje Individual
  #16 (permalink)  
Antiguo 10/05/2013, 04:01
sanlajela
 
Fecha de Ingreso: julio-2008
Mensajes: 11
Antigüedad: 15 años, 9 meses
Puntos: 0
Respuesta: Guardar en fichero

Entendido Fuzzylog. Se que es muy descarado y "tener morro", pero puedes traducirlo en código?

El actual código que tengo para leer el XML es este...Cómo sería el SonarBugs.java?

BugSaxSonarParser.java
Cita:
package example;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.regex.Pattern;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.SAXPars er;


public class BugsSaxParserSonar extends DefaultHandler{

private static final String BUG_COLLECTION = "BugCollection";
private static final String BUG_INSTANCE = "BugInstance";
private static final String LONG_MESSAGE = "LongMessage";
...
private static final String PATH_PROP = "sourcepath";
private static final String LINE_PROP = "start";

private boolean inBugCollectionZone = false;
private boolean inBugInstanceZone = false;
private boolean inLongMessage = false;
...

private String message = null;
private String instanceId = null;
private List<String> sourcePath = new ArrayList<String>();
private List<String> sourceLine = new ArrayList<String>();

private Map<String, Object[]> bugs = new HashMap<String, Object[]>(1,1);

private Pattern folderPattern = null;

public BugsSaxParserSonar (String folderPatterString){
folderPattern = Pattern.compile(folderPatterString);
}

public void startElement (String uri, String localName, String qName, Attributes attributes) {
if(inBugCollectionZone){
if(inBugInstanceZone){
if(LONG_MESSAGE.equals(qName)){
inLongMessage = true;
}
else if(inClass == false && inMethod == false && inField == false) && SOURCE_LINE.equals(qName)){
inSourceLine = true;
sourcePath.add(attributes.getValue(PATH_PROP));
sourceLine.add(attributes.getValue(LINE_PROP));
}
}
else if(BUG_INSTANCE.equals(qName)){
instanceId = attributes.getValue(INSTANCE_HASH);
inBugInstanceZone = true;
}
}
else if (BUG_COLLECTION.equals(qName)){
inBugCollectionZone = true;
}
}


public void characters (char ch[], int start, int length) throws SAXException {
if(inBugCollectionZone){
if(inBugInstanceZone){
if(inLongMessage){
if(message == null){
message = "";

}
message += new String(ch, start, length);
}
}
}
}

public void endElement (String uri, String localName, String qName) {
if(inBugCollectionZone){
if(inBugInstanceZone){
if(LONG_MESSAGE.equals(qName)){
inLongMessage = false;
}

}
else if(BUG_INSTANCE.equals(qName)){
inBugInstanceZone = false;
bugs.put(instanceId, new Object[]{new ArrayList<String>(sourcePath), new ArrayList<String>(sourceLine), message});
clearBugsValues();
}
}
else if (BUG_COLLECTION.equals(qName)){
inBugCollectionZone = false;
}
}

private void clearBugsValues(){

inBugCollectionZone = false;
inBugInstanceZone = false;
inLongMessage = false;
...

}

public Map<String, Object[]> getBugs() {
return bugs;
}

public String getWorkingDirectory() {
return workingDirectory;
}

public void setWorkingDirectory(String workingDirectory) {
this.workingDirectory = workingDirectory;
}


public static void main(String[] args){
BugsSaxParserSonar rsp = new BugsSaxParserSonar("/?(DD|DS)/");
try{
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse("D:/tmp/findbugs-result.xml", rsp);
}catch(ParseConfigurationException e){
System.err.println("error de parseo en XML");
}catch(SAXException e2){
e2.printStackTrace();
System.err.println("error de sax : " + e2.getMessage());
}catch(Exception e3){
System.err.println("error de io : " + e3.getMessage());
}
for(String id : rsp.getBugs().keySet()){
Object[] bugsValues = rsp.getBugs().get(id);
List<String> paths = (List<String>)bugsValues[0];
List<String> lines = (List<String>)bugsValues[1];
System.out.println("id:" + id + " " + paths + " " + lines + " " + bugsValues[2]);
}
}

}