--- a/buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/PolicyLogMetaDataInput.java Wed Jun 16 16:51:40 2010 +0300
+++ b/buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/PolicyLogMetaDataInput.java Fri Aug 13 14:59:05 2010 +0300
@@ -17,11 +17,29 @@
package com.nokia.helium.metadata.ant.types;
-import java.util.*;
-import org.apache.log4j.Logger;
-import javax.xml.stream.XMLStreamReader;
+import java.io.File;
+import java.io.IOException;
+import java.util.Map;
import java.util.regex.Pattern;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+import com.nokia.helium.metadata.AutoCommitEntityManager;
+import com.nokia.helium.metadata.MetadataException;
+import com.nokia.helium.metadata.model.metadata.LogFile;
+import com.nokia.helium.metadata.model.metadata.MetadataEntry;
+import com.nokia.helium.metadata.model.metadata.Severity;
+import com.nokia.helium.metadata.model.metadata.SeverityDAO;
+
/**
* This Type is to specify and use the policy logparsertype to
@@ -40,93 +58,102 @@
* </pre>
* @ant.task name="policymetadatainput" category="Metadata"
*/
-public class PolicyLogMetaDataInput extends XMLLogMetaDataInput {
-
- private Logger log = Logger.getLogger(XMLLogMetaDataInput.class);
-
- private Map<String, String> currentAttributeMap;
+public class PolicyLogMetaDataInput extends LogMetaDataInput {
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void extract(EntityManagerFactory factory, File file)
+ throws MetadataException {
+ SAXParserFactory saxFactory = SAXParserFactory.newInstance();
+ EntityManager em = factory.createEntityManager();
+ AutoCommitEntityManager autoCommitEM = new AutoCommitEntityManager(factory);
+ try {
+ // get the severities
+ SeverityDAO pdao = new SeverityDAO();
+ pdao.setEntityManager(em);
+ Map<String, Severity> severities = pdao.getSeverities();
- /**
- * Constructor
- */
- public PolicyLogMetaDataInput() {
+ // Get the log file
+ LogFile logFile = getLogFile(em, file);
+
+ SAXParser parser = saxFactory.newSAXParser();
+ parser.parse(file, new PolicyFileParser(
+ severities.get(SeverityEnum.Severity.ERROR.toString()),
+ autoCommitEM, logFile));
+ } catch (SAXException e) {
+ throw new MetadataException(e.getMessage(), e);
+ } catch (IOException e) {
+ throw new MetadataException(e.getMessage(), e);
+ } catch (ParserConfigurationException e) {
+ throw new MetadataException(e.getMessage(), e);
+ } finally {
+ em.close();
+ autoCommitEM.close();
+ }
}
-
/**
- * Helper function to return the attributes of the stream reader
- * @returns the attributes as a map.
+ * SAX handler for Policy XML file format.
+ *
*/
- private Map<String, String> getAttributes(XMLStreamReader streamReader) {
- int count = streamReader.getAttributeCount() ;
- if (count > 0 ) {
- Map<String, String> attributesMap = new HashMap<String, String>();
- for (int i = 0 ; i < count ; i++) {
- attributesMap.put(streamReader.getAttributeLocalName(i),
- streamReader.getAttributeValue(i));
- }
- return attributesMap;
+ class PolicyFileParser extends DefaultHandler {
+ private LogFile logFile;
+ private Severity severity;
+ private AutoCommitEntityManager autoCommitEM;
+ private Locator locator;
+
+ /**
+ * Create a new PolicyFileParser.
+ * @param severity
+ * @param autoCommitEM
+ * @param logFile
+ */
+ public PolicyFileParser(Severity severity, AutoCommitEntityManager autoCommitEM,
+ LogFile logFile) {
+ this.autoCommitEM = autoCommitEM;
+ this.logFile = logFile;
+ this.severity = severity;
}
- return null;
- }
-
-
- /**
- * Function to process the start event of xml stream callback.
- * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data.
- * @return true if there are any element to be added to the database.
- */
- boolean startElement(XMLStreamReader streamReader) {
- String tagName = streamReader.getLocalName();
- if (tagName.equalsIgnoreCase("error")) {
- currentAttributeMap = getAttributes(streamReader);
- }
- return false;
- }
- /**
- * Function to process the end event of xml stream callback.
- * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data.
- * @return true if there are any element to be added to the database.
- */
- boolean endElement(XMLStreamReader streamReader) {
- boolean retValue = false;
- String tagName = streamReader.getLocalName();
- String priority = "ERROR";
- log.debug("endElement: " + tagName);
- if (tagName.equalsIgnoreCase("error")) {
- log.debug("tagName matches error");
- String errorType = currentAttributeMap.get("type");
- log.debug("errorType:" + errorType);
- if (errorType.equals("unknownstatus")) {
- addEntry(priority, "CSV validation", getCurrentFile().toString(), -1, currentAttributeMap.get("message") +
- currentAttributeMap.get("value"));
- retValue = true;
- } else if (errorType.equals("A") || errorType.equals("B")
- || errorType.equals("C") || errorType.equals("D")) {
- int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL ;
- Pattern pattern = Pattern.compile("([\\\\/][^\\\\/]+?)$", flags);
- addEntry(priority, "Issues", getCurrentFile().toString(), -1,
- errorType + "Found incorrect value for" +
- pattern.matcher(currentAttributeMap.get("message")).replaceAll(""));
- retValue = true;
- } else if (errorType.equals("missing")) {
- addEntry(priority, "Missing", getCurrentFile().toString(), -1, currentAttributeMap.get("message"));
- retValue = true;
- } else if (errorType.equals("invalidencoding")) {
- addEntry(priority, "Incorrect policy files", getCurrentFile().toString(), -1, currentAttributeMap.get("message"));
- retValue = true;
+ /**
+ * Implement the handling of error nodes.
+ */
+ @Override
+ public void startElement(String uri, String localName, String qName,
+ Attributes attributes) throws SAXException {
+ if (qName.equalsIgnoreCase("error")) {
+ String errorType = attributes.getValue("", "type");
+ MetadataEntry me = new MetadataEntry();
+ me.setLogFile(autoCommitEM.merge(logFile));
+ me.setLineNumber(locator.getLineNumber());
+ me.setSeverity(severity);
+ if (errorType.equals("unknownstatus")) {
+ me.setText(attributes.getValue("", "message") + attributes.getValue("", "value"));
+ } else if (errorType.equals("A") || errorType.equals("B")
+ || errorType.equals("C") || errorType.equals("D")) {
+ int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL ;
+ Pattern pattern = Pattern.compile("([\\\\/][^\\\\/]+?)$", flags);
+ me.setText(pattern.matcher(errorType + "Found incorrect value for "
+ + attributes.getValue("", "message")).replaceAll(""));
+ } else if (errorType.equals("missing")) {
+ me.setText(attributes.getValue("", "message"));
+ } else if (errorType.equals("invalidencoding")) {
+ me.setText(attributes.getValue("", "message"));
+ }
+ autoCommitEM.persist(me);
}
}
- return retValue;
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void setDocumentLocator(Locator locator) {
+ this.locator = locator;
+ super.setDocumentLocator(locator);
+ }
}
- /* Function to process the characters event of xml stream callback.
- * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data.
- * @return true if there are any element to be added to the database.
- */
- boolean characters(XMLStreamReader streamReader) {
- return false;
- }
}
\ No newline at end of file