/** * Parses a Java file. * * @param pJavaFile The Java file that should get parsed. Note that it's up * to the caller to ensure that the file exists, i.e. this * will not be checked by this method. * @param pErrorMessages If this argument is not <code>null</code> error * messages emited by the parser will be added to * this list. Otherwise these error messages will be * written to <code>System.err</code>. * * @return An object of type <code>JSOParser.ParserResult</code> * containing the root node representing a Java file and the token * stream containing the tokens of the parsed source. If parsing * the file has been failed but no exception has been thrown <code> * null</code> will be returned and it's very likely that there's * at least one message within the message list passed to this * method (if a list has been passed at all, of course). * * @ if parsing the Java file failed. */ protected ParserResult parse(JFile pJavaFile, List<String> pErrorMessages) { ParserResult result = null; try { result = parse( new ANTLRFileStream(pJavaFile.getPath()), CodeFragmentType.JAVA_SOURCE, pErrorMessages); } catch (RecognitionException re) { throw new JSourceUnmarshallerException( UnmarshallerMessages .getJavaFileParsingFailedMessage(pJavaFile.ToString()), re); } catch (FileNotFoundException fnfe) { throw new JSourceUnmarshallerException( UnmarshallerMessages .getJavaFileParsingFailedMessage(pJavaFile.ToString()), fnfe); } catch (IOException ioe) { throw new JSourceUnmarshallerException( UnmarshallerMessages .getJavaFileParsingFailedMessage(pJavaFile.ToString()), ioe); } catch (JSourceUnmarshallerException jsue) { // Just catch 'JSourceUnmarshallerException' exceptions in order to // avoid jumping into the following 'catch' block. throw jsue; } catch (Exception e) { throw new JSourceUnmarshallerException( UnmarshallerMessages .getJavaFileParsingFailedMessage(pJavaFile.ToString()), e); } finally { if (pErrorMessages != null) { List<String> parserMessages; bool isMessageHeaderEnabled = true; parserMessages = mLexer.GetMessages(); if (parserMessages.Count > 0) { pErrorMessages.Add( UnmarshallerMessages .getParserMessagesEmitedForFileMessage( pJavaFile.getAbsolutePath())); isMessageHeaderEnabled = false; foreach (String message in parserMessages) { pErrorMessages.Add( UnmarshallerMessages .getLexerMessageMessage() + message); } } if (mParser != null) { parserMessages = mParser.GetMessages(); if (parserMessages.Count > 0) { if (isMessageHeaderEnabled) { pErrorMessages.Add( UnmarshallerMessages .getParserMessagesEmitedForFileMessage( pJavaFile.getAbsolutePath())); } isMessageHeaderEnabled = false; foreach (String message in parserMessages) { pErrorMessages.Add( UnmarshallerMessages .getParserMessageMessage() + message); } } } if (mTreeParser != null) { parserMessages = mTreeParser.GetMessages(); if (parserMessages.Count > 0) { if (isMessageHeaderEnabled) { pErrorMessages.Add( UnmarshallerMessages .getParserMessagesEmitedForFileMessage( pJavaFile.getAbsolutePath())); } String TEST_STR = "JavaTreeParser.g: "; foreach (String message in parserMessages) { int offset = message.IndexOf(TEST_STR); if (offset != -1) { pErrorMessages.Add( UnmarshallerMessages .getTreeParserMessageMessage() + message.Substring( offset + TEST_STR.Length)); } else { pErrorMessages.Add( UnmarshallerMessages .getTreeParserMessageMessage() + message); } } } } } } if (result.mTree != null) { return result; } return null; }
/** * Unmarshals a complete Java source file. * * @param pJavaSource The Java source that should get unmarshalled. * @param pErrorMessages If the parser reports one or more errors it * depends on this argument what will happen. If * this argument is <code>null</code> all the * error messages will be written to <code> * System.err</code>. Otherwise the error * messages will be added to the given list and * each list entry will correspond to one error * line as reported by the parser. * * @return A <code>JSOM</code> object of type <code> * ASTJavaSource</code>. If marshalling the file has been * failed but no exception has been thrown <code>null</code> * will be returned and it's very likely that there's at least * one message within the message list passed to this method * (if a list has been passed at all, of course). * * @ if marshalling the Java file * failed. */ public AST2JavaSource unmarshalAST2JavaSource( JFile pJavaSource, List<String> pErrorMessages) { ParserResult parserResult = parse(pJavaSource, pErrorMessages); if (parserResult != null) { String fileName = pJavaSource.getName(); int offset = fileName.LastIndexOf(".java"); if (offset != -1) { fileName = fileName.Substring(0, offset); } return new AST2JavaSource( parserResult.mTree, fileName, parserResult.mTokenRewriteStream, parserResult.mLineCount); } return null; }