buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/PolicyLogMetaDataInput.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
    15  *
    15  *
    16  */
    16  */
    17 
    17 
    18 package com.nokia.helium.metadata.ant.types;
    18 package com.nokia.helium.metadata.ant.types;
    19 
    19 
    20 import java.util.*;
    20 import java.io.File;
    21 import org.apache.log4j.Logger;
    21 import java.io.IOException;
    22 import javax.xml.stream.XMLStreamReader;
    22 import java.util.Map;
    23 import java.util.regex.Pattern;
    23 import java.util.regex.Pattern;
       
    24 
       
    25 import javax.persistence.EntityManager;
       
    26 import javax.persistence.EntityManagerFactory;
       
    27 import javax.xml.parsers.ParserConfigurationException;
       
    28 import javax.xml.parsers.SAXParser;
       
    29 import javax.xml.parsers.SAXParserFactory;
       
    30 
       
    31 import org.xml.sax.Attributes;
       
    32 import org.xml.sax.Locator;
       
    33 import org.xml.sax.SAXException;
       
    34 import org.xml.sax.helpers.DefaultHandler;
       
    35 
       
    36 import com.nokia.helium.metadata.AutoCommitEntityManager;
       
    37 import com.nokia.helium.metadata.MetadataException;
       
    38 import com.nokia.helium.metadata.model.metadata.LogFile;
       
    39 import com.nokia.helium.metadata.model.metadata.MetadataEntry;
       
    40 import com.nokia.helium.metadata.model.metadata.Severity;
       
    41 import com.nokia.helium.metadata.model.metadata.SeverityDAO;
    24 
    42 
    25 
    43 
    26 /**
    44 /**
    27  * This Type is to specify and use the policy logparsertype to 
    45  * This Type is to specify and use the policy logparsertype to 
    28  * parse and store the data based on xmlstreamreader.
    46  * parse and store the data based on xmlstreamreader.
    38  *    <metadatafilterset refid="policy.metadata.filter" />
    56  *    <metadatafilterset refid="policy.metadata.filter" />
    39  * </hlm:policymetadatainput>
    57  * </hlm:policymetadatainput>
    40  * </pre>
    58  * </pre>
    41  * @ant.task name="policymetadatainput" category="Metadata"
    59  * @ant.task name="policymetadatainput" category="Metadata"
    42  */
    60  */
    43 public class PolicyLogMetaDataInput extends XMLLogMetaDataInput {
    61 public class PolicyLogMetaDataInput extends LogMetaDataInput {
       
    62     
       
    63     /**
       
    64      * {@inheritDoc}
       
    65      */
       
    66     @Override
       
    67     public void extract(EntityManagerFactory factory, File file)
       
    68         throws MetadataException {
       
    69         SAXParserFactory saxFactory = SAXParserFactory.newInstance();
       
    70         EntityManager em = factory.createEntityManager();
       
    71         AutoCommitEntityManager autoCommitEM = new AutoCommitEntityManager(factory);
       
    72         try {
       
    73             // get the severities
       
    74             SeverityDAO pdao = new SeverityDAO();
       
    75             pdao.setEntityManager(em);
       
    76             Map<String, Severity> severities = pdao.getSeverities();
    44 
    77 
    45     private Logger log = Logger.getLogger(XMLLogMetaDataInput.class);
    78             // Get the log file
    46     
    79             LogFile logFile = getLogFile(em, file);
    47     private Map<String, String> currentAttributeMap;
       
    48     
       
    49 
    80 
    50     /**
    81             SAXParser parser = saxFactory.newSAXParser();
    51      * Constructor
    82             parser.parse(file, new PolicyFileParser(
    52      */
    83                     severities.get(SeverityEnum.Severity.ERROR.toString()),
    53     public PolicyLogMetaDataInput() {
    84                     autoCommitEM, logFile));
       
    85         } catch (SAXException e) {
       
    86             throw new MetadataException(e.getMessage(), e);
       
    87         } catch (IOException e) {
       
    88             throw new MetadataException(e.getMessage(), e);
       
    89         } catch (ParserConfigurationException e) {
       
    90             throw new MetadataException(e.getMessage(), e);
       
    91         } finally {
       
    92             em.close();
       
    93             autoCommitEM.close();
       
    94         }
    54     }
    95     }
    55     
    96     
       
    97     /**
       
    98      * SAX handler for Policy XML file format.
       
    99      *
       
   100      */
       
   101     class PolicyFileParser extends DefaultHandler {
       
   102         private LogFile logFile;
       
   103         private Severity severity;
       
   104         private AutoCommitEntityManager autoCommitEM;
       
   105         private Locator locator;
       
   106         
       
   107         /**
       
   108          * Create a new PolicyFileParser.
       
   109          * @param severity
       
   110          * @param autoCommitEM
       
   111          * @param logFile
       
   112          */
       
   113         public PolicyFileParser(Severity severity, AutoCommitEntityManager autoCommitEM, 
       
   114                 LogFile logFile) {
       
   115             this.autoCommitEM = autoCommitEM;
       
   116             this.logFile = logFile;
       
   117             this.severity = severity;
       
   118         }
    56 
   119 
    57     /**
   120         /**
    58      * Helper function to return the attributes of the stream reader
   121          * Implement the handling of error nodes.
    59      * @returns the attributes as a map.
   122          */
    60      */
   123         @Override
    61     private Map<String, String> getAttributes(XMLStreamReader streamReader) {
   124         public void startElement(String uri, String localName, String qName,
    62         int count = streamReader.getAttributeCount() ;
   125                 Attributes attributes) throws SAXException {
    63         if (count > 0 ) {
   126             if (qName.equalsIgnoreCase("error")) {
    64             Map<String, String> attributesMap = new HashMap<String, String>();
   127                 String errorType = attributes.getValue("", "type");
    65             for (int i = 0 ; i < count ; i++) {
   128                 MetadataEntry me = new MetadataEntry();
    66                 attributesMap.put(streamReader.getAttributeLocalName(i), 
   129                 me.setLogFile(autoCommitEM.merge(logFile));
    67                         streamReader.getAttributeValue(i));
   130                 me.setLineNumber(locator.getLineNumber());
    68             }
   131                 me.setSeverity(severity);
    69             return attributesMap;
   132                 if (errorType.equals("unknownstatus")) {
    70         }
   133                     me.setText(attributes.getValue("", "message") + attributes.getValue("", "value"));
    71         return null;
   134                 } else if (errorType.equals("A") || errorType.equals("B") 
    72     }
   135                         || errorType.equals("C") || errorType.equals("D")) {
    73 
   136                     int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL ;
    74    
   137                     Pattern pattern = Pattern.compile("([\\\\/][^\\\\/]+?)$", flags);
    75     /**
   138                     me.setText(pattern.matcher(errorType + "Found incorrect value for " 
    76      * Function to process the start event of xml stream callback.
   139                             + attributes.getValue("", "message")).replaceAll(""));
    77      * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data.
   140                 } else if (errorType.equals("missing")) {
    78      * @return true if there are any element to be added to the database.
   141                     me.setText(attributes.getValue("", "message"));
    79      */
   142                 } else if (errorType.equals("invalidencoding")) {
    80     boolean startElement(XMLStreamReader streamReader) {
   143                     me.setText(attributes.getValue("", "message"));
    81         String tagName = streamReader.getLocalName();
   144                 }
    82         if (tagName.equalsIgnoreCase("error")) {
   145                 autoCommitEM.persist(me);
    83             currentAttributeMap = getAttributes(streamReader);
       
    84         }
       
    85         return false;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Function to process the end event of xml stream callback.
       
    90      * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data.
       
    91      * @return true if there are any element to be added to the database.
       
    92      */
       
    93     boolean endElement(XMLStreamReader streamReader) {
       
    94         boolean retValue = false;
       
    95         String tagName = streamReader.getLocalName();
       
    96         String priority = "ERROR";
       
    97         log.debug("endElement: " + tagName);
       
    98         if (tagName.equalsIgnoreCase("error")) {
       
    99             log.debug("tagName matches error");
       
   100             String errorType = currentAttributeMap.get("type");
       
   101             log.debug("errorType:" + errorType);
       
   102             if (errorType.equals("unknownstatus")) {
       
   103                 addEntry(priority, "CSV validation", getCurrentFile().toString(), -1, currentAttributeMap.get("message") + 
       
   104                         currentAttributeMap.get("value"));
       
   105                 retValue = true;
       
   106             } else if (errorType.equals("A") || errorType.equals("B") 
       
   107                     || errorType.equals("C") || errorType.equals("D")) {
       
   108                 int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL ;
       
   109                 Pattern pattern = Pattern.compile("([\\\\/][^\\\\/]+?)$", flags);
       
   110                 addEntry(priority, "Issues", getCurrentFile().toString(), -1, 
       
   111                         errorType + "Found incorrect value for" + 
       
   112                         pattern.matcher(currentAttributeMap.get("message")).replaceAll(""));
       
   113                 retValue = true;
       
   114             } else if (errorType.equals("missing")) {
       
   115                 addEntry(priority, "Missing", getCurrentFile().toString(), -1, currentAttributeMap.get("message"));
       
   116                 retValue = true;
       
   117             } else if (errorType.equals("invalidencoding")) {
       
   118                 addEntry(priority, "Incorrect policy files", getCurrentFile().toString(), -1,  currentAttributeMap.get("message"));
       
   119                 retValue = true;
       
   120             }
   146             }
   121         }
   147         }
   122         return retValue;
   148         
       
   149         /**
       
   150          * {@inheritDoc}
       
   151          */
       
   152         @Override
       
   153         public void setDocumentLocator(Locator locator) {
       
   154             this.locator = locator;
       
   155             super.setDocumentLocator(locator);
       
   156         }
   123     }
   157     }
   124     
   158     
   125      /* Function to process the characters event of xml stream callback.
       
   126      * @param streamReader: the input stream reader which contains the xml data to be parsed for recording data.
       
   127      * @return true if there are any element to be added to the database.
       
   128      */
       
   129     boolean characters(XMLStreamReader streamReader) {
       
   130         return false;
       
   131     }
       
   132 }
   159 }