587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.metadata.ant.types;
|
|
19 |
|
628
|
20 |
import java.io.File;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.util.Map;
|
587
|
23 |
import java.util.regex.Pattern;
|
|
24 |
|
628
|
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;
|
|
42 |
|
587
|
43 |
|
|
44 |
/**
|
|
45 |
* This Type is to specify and use the policy logparsertype to
|
|
46 |
* parse and store the data based on xmlstreamreader.
|
|
47 |
* <pre>
|
|
48 |
* <hlm:metadatafilterset id="policy.metadata.filter">
|
|
49 |
* <metadatafilterset filterfile="common.csv" />
|
|
50 |
* </hlm:metadatafilterset>
|
|
51 |
*
|
|
52 |
* <hlm:policymetadatainput>
|
|
53 |
* <fileset dir="${project.dir}/../data/">
|
|
54 |
* <include name="*validate*policy*.log"/>
|
|
55 |
* </fileset>
|
|
56 |
* <metadatafilterset refid="policy.metadata.filter" />
|
|
57 |
* </hlm:policymetadatainput>
|
|
58 |
* </pre>
|
|
59 |
* @ant.task name="policymetadatainput" category="Metadata"
|
|
60 |
*/
|
628
|
61 |
public class PolicyLogMetaDataInput extends LogMetaDataInput {
|
587
|
62 |
|
628
|
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();
|
587
|
77 |
|
628
|
78 |
// Get the log file
|
|
79 |
LogFile logFile = getLogFile(em, file);
|
|
80 |
|
|
81 |
SAXParser parser = saxFactory.newSAXParser();
|
|
82 |
parser.parse(file, new PolicyFileParser(
|
|
83 |
severities.get(SeverityEnum.Severity.ERROR.toString()),
|
|
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 |
}
|
587
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
628
|
98 |
* SAX handler for Policy XML file format.
|
|
99 |
*
|
587
|
100 |
*/
|
628
|
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;
|
587
|
118 |
}
|
|
119 |
|
628
|
120 |
/**
|
|
121 |
* Implement the handling of error nodes.
|
|
122 |
*/
|
|
123 |
@Override
|
|
124 |
public void startElement(String uri, String localName, String qName,
|
|
125 |
Attributes attributes) throws SAXException {
|
|
126 |
if (qName.equalsIgnoreCase("error")) {
|
|
127 |
String errorType = attributes.getValue("", "type");
|
|
128 |
MetadataEntry me = new MetadataEntry();
|
|
129 |
me.setLogFile(autoCommitEM.merge(logFile));
|
|
130 |
me.setLineNumber(locator.getLineNumber());
|
|
131 |
me.setSeverity(severity);
|
|
132 |
if (errorType.equals("unknownstatus")) {
|
|
133 |
me.setText(attributes.getValue("", "message") + attributes.getValue("", "value"));
|
|
134 |
} else if (errorType.equals("A") || errorType.equals("B")
|
|
135 |
|| errorType.equals("C") || errorType.equals("D")) {
|
|
136 |
int flags = Pattern.CASE_INSENSITIVE | Pattern.DOTALL ;
|
|
137 |
Pattern pattern = Pattern.compile("([\\\\/][^\\\\/]+?)$", flags);
|
|
138 |
me.setText(pattern.matcher(errorType + "Found incorrect value for "
|
|
139 |
+ attributes.getValue("", "message")).replaceAll(""));
|
|
140 |
} else if (errorType.equals("missing")) {
|
|
141 |
me.setText(attributes.getValue("", "message"));
|
|
142 |
} else if (errorType.equals("invalidencoding")) {
|
|
143 |
me.setText(attributes.getValue("", "message"));
|
|
144 |
}
|
|
145 |
autoCommitEM.persist(me);
|
587
|
146 |
}
|
588
|
147 |
}
|
628
|
148 |
|
|
149 |
/**
|
|
150 |
* {@inheritDoc}
|
|
151 |
*/
|
|
152 |
@Override
|
|
153 |
public void setDocumentLocator(Locator locator) {
|
|
154 |
this.locator = locator;
|
|
155 |
super.setDocumentLocator(locator);
|
|
156 |
}
|
587
|
157 |
}
|
|
158 |
|
|
159 |
} |