Search in sources :

Example 1 with LogParseException

use of com.cosylab.logging.engine.ACS.LogParseException in project ACS by ACS-Community.

the class ACSLogParserDOM method parse.

/* (non-Javadoc)
	 * @see com.cosylab.logging.engine.ACS.ACSLogParser#parse(java.lang.String)
	 */
public synchronized ILogEntry parse(String string) throws LogParseException {
    Document document = null;
    try {
        document = builder.parse(new InputSource(new StringReader(string)));
    } catch (IOException ioe) {
        // cannot get here	
        System.err.println("Exception parsing " + ioe.getMessage());
        throw new LogParseException(ioe);
    } catch (Exception e) {
        /* There was an exception parsing the log, but before giving up 
			 * we try to fix markup issues inside the text that is contained in the XML */
        document = null;
        String newLogString = XmlNormalizer.normalizeXMLEmbeddedTextOnly(string);
        try {
            document = builder.parse(new InputSource(new StringReader(newLogString)));
            System.out.println("Fatal error recovered:");
            System.out.println("\tOriginal log entry: " + string);
            System.out.println("\tCleaned log entry: " + newLogString + "\n");
        } catch (IOException ex1) {
            System.err.println("Failed to parse the following log entry:");
            System.err.println(string);
            System.err.println("with IO exception ");
            throw new LogParseException(ex1);
        } catch (SAXException ex2) {
            System.err.println("Failed to parse the following log entry:");
            System.err.println(string);
            System.err.println("with parser exception ");
            throw new LogParseException(ex2);
        }
    }
    return new LogEntry(new LogEntryXML(document.getFirstChild()));
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) LogParseException(com.cosylab.logging.engine.ACS.LogParseException) LogEntryXML(com.cosylab.logging.engine.log.LogEntryXML) LogParseException(com.cosylab.logging.engine.ACS.LogParseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ILogEntry(com.cosylab.logging.engine.log.ILogEntry) LogEntry(com.cosylab.logging.engine.log.LogEntry) SAXException(org.xml.sax.SAXException)

Example 2 with LogParseException

use of com.cosylab.logging.engine.ACS.LogParseException in project ACS by ACS-Community.

the class ACSLogParserVTD method parse.

/**
	 * Implements required method of ACSLogParser interface.
	 * 
	 * @param xmlString the XML string to parse
	 * @throws LogParseException when problems are encountered parsing an XML message.
	 * @see ACSLogParser
	 */
public synchronized ILogEntry parse(String xmlString) throws LogParseException {
    if (xmlString == null || xmlString.length() == 0) {
        throw new IllegalArgumentException("Invalid string to parse");
    }
    LogEntry retVal = null;
    byte[] bytesArray = xmlString.getBytes();
    try {
        try {
            VTDGen_clear.invoke(vtdGen, nullObj);
            VTDGen_setDoc.invoke(vtdGen, bytesArray);
            // set namespace awareness to false for now
            VTDGen_parse.invoke(vtdGen, false);
        } catch (Exception e) {
            /* There was an exception parsing the log, but before giving up 
				 * we try to fix markup issues inside the text that is contained in the XML */
            VTDGen_clear.invoke(vtdGen, nullObj);
            xmlString = XmlNormalizer.normalizeXMLEmbeddedTextOnly(xmlString);
            bytesArray = xmlString.getBytes();
            VTDGen_setDoc.invoke(vtdGen, xmlString.getBytes());
            VTDGen_parse.invoke(vtdGen, false);
        }
        retVal = makeLogEntryFromParsedXML(bytesArray, xmlString);
    } catch (Exception ex) {
        throw new LogParseException("Error parsing with VTD!", ex);
    }
    return retVal;
}
Also used : LogParseException(com.cosylab.logging.engine.ACS.LogParseException) ILogEntry(com.cosylab.logging.engine.log.ILogEntry) LogEntry(com.cosylab.logging.engine.log.LogEntry) LogParseException(com.cosylab.logging.engine.ACS.LogParseException)

Example 3 with LogParseException

use of com.cosylab.logging.engine.ACS.LogParseException in project ACS by ACS-Community.

the class ACSLogParserVTD method getLongFromTimestamp.

/**
	 * Gets a Long from VTD XML parser (using VTDNav navigation)
	 * 
	 * @param vtdNav the navigator to use to navigate the parsed xml
	 * @param os output stream to use for conversion of bytes to useful formatted data.
	 * @param attrName the name of the field we are searching for
	 * @param bytesXML the bytes (containing XML) that are being referenced by the navigator.
	 * @return Long populated with the attribute's value
	 * @throws NavException if navigation encounters problems
	 * @throws LogParseException if parsing fails
	 */
private Long getLongFromTimestamp(Object vtdNav, ByteArrayOutputStream os, String attrName, byte[] bytesXML) throws LogParseException, Exception {
    Long retVal = null;
    int tIndex = (Integer) VTDNav_getAttrVal.invoke(vtdNav, attrName);
    int tOffset = (Integer) VTDNav_getTokenOffset.invoke(vtdNav, tIndex);
    int tLen = (Integer) VTDNav_getTokenLength.invoke(vtdNav, tIndex);
    os.reset();
    //write the fragment out
    os.write(bytesXML, tOffset, tLen);
    try {
        SimpleDateFormat dateFormat = new IsoDateFormat();
        Date date = dateFormat.parse(os.toString());
        retVal = Long.valueOf(date.getTime());
    } catch (java.text.ParseException pEx) {
        throw new LogParseException("Error parsing date", pEx);
    }
    return retVal;
}
Also used : IsoDateFormat(alma.acs.util.IsoDateFormat) LogParseException(com.cosylab.logging.engine.ACS.LogParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with LogParseException

use of com.cosylab.logging.engine.ACS.LogParseException in project ACS by ACS-Community.

the class ACSLogParserVTD method makeLogEntryFromParsedXML.

/**
	 * Creates a LogEntry from raw XML, using a VTD XML parser. 
	 * 
	 * @param xmlString the XML string that is being parsed.
	 * @param bytesArray the array of bytes (also containing the xml string that we are parsing, in byte form) 
	 *                   to be used by VTD.
	 * @param vtdGen the instance of VTDGen to use for parsing the XML
	 * @return A LogEntry populated with the data for the log entry contained in the XML string passed in.
	 * @throws LogParseException if the parsing fails
	 */
private LogEntry makeLogEntryFromParsedXML(byte[] bytesArray, String xmlString) throws LogParseException {
    // TODO: this method, though relatively simple, is a bit long; consider making it shorter
    LogEntry retVal = null;
    Object vtdNav;
    try {
        vtdNav = VTDGen_getNav.invoke(vtdGen, nullObj);
        if (// to root element
        (Boolean) VTDNav_toElement.invoke(vtdNav, NAV_ROOT)) {
            if ((Boolean) VTDNav_matchElement.invoke(vtdNav, ILogEntry.LOG_ELEMENT_TAG_NAME)) {
                // navigate to child 
                if ((Boolean) VTDNav_toElement.invoke(vtdNav, NAV_FIRST_CHILD)) {
                    if ((Boolean) VTDNav_matchElement.invoke(vtdNav, ILogEntry.HEADER_ELEMENT_TAG_NAME)) {
                        // navigate to sibling
                        VTDNav_toElement.invoke(vtdNav, NAV_NEXT_SIBLING);
                    }
                }
            }
            if ((Boolean) VTDNav_matchElement.invoke(vtdNav, ILogEntry.HEADER_ELEMENT_TAG_NAME)) {
                // navigate to sibling
                VTDNav_toElement.invoke(vtdNav, NAV_NEXT_SIBLING);
            }
            Long milliseconds = null;
            Integer line = null;
            Integer priority = null;
            Integer stackLevel = null;
            String fileName = null;
            String routineName = null;
            String hostName = null;
            String processName = null;
            String contextName = null;
            String threadName = null;
            String logId = null;
            String uri = null;
            String stackId = null;
            String logMessage = null;
            String srcObjectName = null;
            String audience = null;
            String array = null;
            String antenna = null;
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            LogTypeHelper entryType = determineEntryType(vtdNav);
            // test for timestamp attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.TIMESTAMP.getTagAttribute())) {
                milliseconds = getLongFromTimestamp(vtdNav, os, LogField.TIMESTAMP.getTagAttribute(), bytesArray);
            }
            // test for File attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.FILE.getTagAttribute())) {
                fileName = this.getString(vtdNav, os, LogField.FILE.getTagAttribute(), bytesArray);
            }
            // test for Line attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.LINE.getTagAttribute())) {
                line = getInteger(vtdNav, os, LogField.LINE.getTagAttribute(), bytesArray);
            }
            // test for Routine attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.ROUTINE.getTagAttribute())) {
                routineName = this.getString(vtdNav, os, LogField.ROUTINE.getTagAttribute(), bytesArray);
            }
            // test for host attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.HOST.getTagAttribute())) {
                hostName = this.getString(vtdNav, os, LogField.HOST.getTagAttribute(), bytesArray);
            }
            // test for process attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.PROCESS.getTagAttribute())) {
                processName = this.getString(vtdNav, os, LogField.PROCESS.getTagAttribute(), bytesArray);
            }
            // test for context attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.CONTEXT.getTagAttribute())) {
                contextName = this.getString(vtdNav, os, LogField.CONTEXT.getTagAttribute(), bytesArray);
            }
            // test for thread attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.THREAD.getTagAttribute())) {
                threadName = this.getString(vtdNav, os, LogField.THREAD.getTagAttribute(), bytesArray);
            }
            // test for logid attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.LOGID.getTagAttribute())) {
                logId = this.getString(vtdNav, os, LogField.LOGID.getTagAttribute(), bytesArray);
            }
            // test for priority attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.PRIORITY.getTagAttribute())) {
                priority = getInteger(vtdNav, os, LogField.PRIORITY.getTagAttribute(), bytesArray);
            }
            // test for uri attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.URI.getTagAttribute())) {
                uri = this.getString(vtdNav, os, LogField.URI.getTagAttribute(), bytesArray);
            }
            // test for stackid attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.STACKID.getTagAttribute())) {
                stackId = this.getString(vtdNav, os, LogField.STACKID.getTagAttribute(), bytesArray);
            }
            // test for stacklevel attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.STACKLEVEL.getTagAttribute())) {
                stackLevel = getInteger(vtdNav, os, LogField.STACKLEVEL.getTagAttribute(), bytesArray);
            }
            // test for srcObject attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.SOURCEOBJECT.getTagAttribute())) {
                srcObjectName = getString(vtdNav, os, LogField.SOURCEOBJECT.getTagAttribute(), bytesArray);
            }
            // test for Audience attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.AUDIENCE.getTagAttribute())) {
                audience = getString(vtdNav, os, LogField.AUDIENCE.getTagAttribute(), bytesArray);
            }
            // test for Array attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.ARRAY.getTagAttribute())) {
                array = getString(vtdNav, os, LogField.ARRAY.getTagAttribute(), bytesArray);
            }
            // test for Antenna attribute
            if ((Boolean) VTDNav_hasAttr.invoke(vtdNav, LogField.ANTENNA.getTagAttribute())) {
                antenna = getString(vtdNav, os, LogField.ANTENNA.getTagAttribute(), bytesArray);
            }
            // Get the body of the message
            int tIndex = (Integer) VTDNav_getText.invoke(vtdNav, nullObj);
            if (tIndex != -1) {
                //					int tOffset = vn.getTokenOffset(tIndex);
                //					int tLen = vn.getTokenLength(tIndex);
                logMessage = (String) VTDNav_toString.invoke(vtdNav, tIndex);
            }
            // get the additional data, if present
            Vector<AdditionalData> extraDataList = getAdditionalData(vtdNav, os, bytesArray);
            // get again the body of the message.
            if (logMessage == null) {
                try {
                    logMessage = getLogMessage(vtdNav);
                } catch (Exception e) {
                    logMessage = null;
                }
            }
            retVal = new LogEntry(milliseconds, entryType.ordinal(), fileName, line, routineName, hostName, processName, contextName, threadName, logId, priority, uri, stackId, stackLevel, logMessage, srcObjectName, audience, array, antenna, extraDataList);
        } else {
            throw new LogParseException("Error: VTD cannot find root element; string is: " + xmlString);
        }
    } catch (Exception navEx) {
        navEx.printStackTrace();
        throw new LogParseException("Error navigating parsed XML with VTD navigator!", navEx);
    }
    return retVal;
}
Also used : AdditionalData(com.cosylab.logging.engine.log.ILogEntry.AdditionalData) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LogParseException(com.cosylab.logging.engine.ACS.LogParseException) LogTypeHelper(com.cosylab.logging.engine.log.LogTypeHelper) LogParseException(com.cosylab.logging.engine.ACS.LogParseException) ILogEntry(com.cosylab.logging.engine.log.ILogEntry) LogEntry(com.cosylab.logging.engine.log.LogEntry)

Aggregations

LogParseException (com.cosylab.logging.engine.ACS.LogParseException)4 ILogEntry (com.cosylab.logging.engine.log.ILogEntry)3 LogEntry (com.cosylab.logging.engine.log.LogEntry)3 IsoDateFormat (alma.acs.util.IsoDateFormat)1 AdditionalData (com.cosylab.logging.engine.log.ILogEntry.AdditionalData)1 LogEntryXML (com.cosylab.logging.engine.log.LogEntryXML)1 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1