buildframework/helium/sf/java/metadata/src/com/nokia/helium/metadata/ant/types/PolicyLogMetaDataInput.java
author wbernard
Fri, 13 Aug 2010 14:59:05 +0300
changeset 628 7c4a911dc066
parent 588 c7c26511138f
child 645 b8d81fa19e7d
permissions -rw-r--r--
helium_11.0.0-e00f171ca185
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     1
/*
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     2
 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     3
 * All rights reserved.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     4
 * This component and the accompanying materials are made available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     6
 * which accompanies this distribution, and is available
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     8
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
     9
 * Initial Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    10
 * Nokia Corporation - initial contribution.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    11
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    12
 * Contributors:
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    13
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    14
 * Description:  
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    15
 *
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    16
 */
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    17
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    18
package com.nokia.helium.metadata.ant.types;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    19
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    20
import java.io.File;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    21
import java.io.IOException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    22
import java.util.Map;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    23
import java.util.regex.Pattern;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    24
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    25
import javax.persistence.EntityManager;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    26
import javax.persistence.EntityManagerFactory;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    27
import javax.xml.parsers.ParserConfigurationException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    28
import javax.xml.parsers.SAXParser;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    29
import javax.xml.parsers.SAXParserFactory;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    30
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    31
import org.xml.sax.Attributes;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    32
import org.xml.sax.Locator;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    33
import org.xml.sax.SAXException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    34
import org.xml.sax.helpers.DefaultHandler;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    35
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    36
import com.nokia.helium.metadata.AutoCommitEntityManager;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    37
import com.nokia.helium.metadata.MetadataException;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    38
import com.nokia.helium.metadata.model.metadata.LogFile;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    39
import com.nokia.helium.metadata.model.metadata.MetadataEntry;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    40
import com.nokia.helium.metadata.model.metadata.Severity;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    41
import com.nokia.helium.metadata.model.metadata.SeverityDAO;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    42
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    43
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    44
/**
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    45
 * This Type is to specify and use the policy logparsertype to 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    46
 * parse and store the data based on xmlstreamreader.
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    47
 * <pre>
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    48
 * &lt;hlm:metadatafilterset id="policy.metadata.filter"&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    49
 *    &lt;metadatafilterset filterfile="common.csv" /&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    50
 * &lt;/hlm:metadatafilterset&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    51
 * 
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    52
 * &lt;hlm:policymetadatainput&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    53
 *    &lt;fileset dir="${project.dir}/../data/"&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    54
 *        &lt;include name="*validate*policy*.log"/&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    55
 *    &lt;/fileset&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    56
 *    &lt;metadatafilterset refid="policy.metadata.filter" /&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    57
 * &lt;/hlm:policymetadatainput&gt;
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    58
 * </pre>
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    59
 * @ant.task name="policymetadatainput" category="Metadata"
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    60
 */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    61
public class PolicyLogMetaDataInput extends LogMetaDataInput {
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    62
    
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    63
    /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    64
     * {@inheritDoc}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    65
     */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    66
    @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    67
    public void extract(EntityManagerFactory factory, File file)
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    68
        throws MetadataException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    69
        SAXParserFactory saxFactory = SAXParserFactory.newInstance();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    70
        EntityManager em = factory.createEntityManager();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    71
        AutoCommitEntityManager autoCommitEM = new AutoCommitEntityManager(factory);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    72
        try {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    73
            // get the severities
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    74
            SeverityDAO pdao = new SeverityDAO();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    75
            pdao.setEntityManager(em);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    76
            Map<String, Severity> severities = pdao.getSeverities();
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    77
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    78
            // Get the log file
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    79
            LogFile logFile = getLogFile(em, file);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    80
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    81
            SAXParser parser = saxFactory.newSAXParser();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    82
            parser.parse(file, new PolicyFileParser(
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    83
                    severities.get(SeverityEnum.Severity.ERROR.toString()),
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    84
                    autoCommitEM, logFile));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    85
        } catch (SAXException e) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    86
            throw new MetadataException(e.getMessage(), e);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    87
        } catch (IOException e) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    88
            throw new MetadataException(e.getMessage(), e);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    89
        } catch (ParserConfigurationException e) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    90
            throw new MetadataException(e.getMessage(), e);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    91
        } finally {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    92
            em.close();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    93
            autoCommitEM.close();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    94
        }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    95
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    96
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
    97
    /**
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    98
     * SAX handler for Policy XML file format.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
    99
     *
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   100
     */
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   101
    class PolicyFileParser extends DefaultHandler {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   102
        private LogFile logFile;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   103
        private Severity severity;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   104
        private AutoCommitEntityManager autoCommitEM;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   105
        private Locator locator;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   106
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   107
        /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   108
         * Create a new PolicyFileParser.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   109
         * @param severity
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   110
         * @param autoCommitEM
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   111
         * @param logFile
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   112
         */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   113
        public PolicyFileParser(Severity severity, AutoCommitEntityManager autoCommitEM, 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   114
                LogFile logFile) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   115
            this.autoCommitEM = autoCommitEM;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   116
            this.logFile = logFile;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   117
            this.severity = severity;
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   118
        }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   119
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   120
        /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   121
         * Implement the handling of error nodes.
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   122
         */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   123
        @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   124
        public void startElement(String uri, String localName, String qName,
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   125
                Attributes attributes) throws SAXException {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   126
            if (qName.equalsIgnoreCase("error")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   127
                String errorType = attributes.getValue("", "type");
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   128
                MetadataEntry me = new MetadataEntry();
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   129
                me.setLogFile(autoCommitEM.merge(logFile));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   130
                me.setLineNumber(locator.getLineNumber());
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   131
                me.setSeverity(severity);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   132
                if (errorType.equals("unknownstatus")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   133
                    me.setText(attributes.getValue("", "message") + attributes.getValue("", "value"));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   134
                } else if (errorType.equals("A") || errorType.equals("B") 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   135
                        || errorType.equals("C") || errorType.equals("D")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   136
                    int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL ;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   137
                    Pattern pattern = Pattern.compile("([\\\\/][^\\\\/]+?)$", flags);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   138
                    me.setText(pattern.matcher(errorType + "Found incorrect value for " 
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   139
                            + attributes.getValue("", "message")).replaceAll(""));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   140
                } else if (errorType.equals("missing")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   141
                    me.setText(attributes.getValue("", "message"));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   142
                } else if (errorType.equals("invalidencoding")) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   143
                    me.setText(attributes.getValue("", "message"));
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   144
                }
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   145
                autoCommitEM.persist(me);
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   146
            }
588
c7c26511138f helium-10.0.0-bc45d50958fe
wbernard
parents: 587
diff changeset
   147
        }
628
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   148
        
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   149
        /**
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   150
         * {@inheritDoc}
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   151
         */
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   152
        @Override
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   153
        public void setDocumentLocator(Locator locator) {
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   154
            this.locator = locator;
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   155
            super.setDocumentLocator(locator);
7c4a911dc066 helium_11.0.0-e00f171ca185
wbernard
parents: 588
diff changeset
   156
        }
587
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   157
    }
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   158
    
85df38eb4012 helium_9.0-a7879c935424
wbernard
parents:
diff changeset
   159
}