001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.parser;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import org.apache.juneau.*;
018
019/**
020 * Class for listening for certain parse events during a document parse.
021 */
022public class ParserListener {
023
024   /**
025    * Represents no parser listener.
026    */
027   public static final class Null extends ParserListener {}
028
029   /**
030    * Gets called when an unknown bean property is detected in a document.
031    *
032    * <p>
033    * This method only gets called if {@link BeanContext#BEAN_ignoreUnknownBeanProperties} setting is <jk>true</jk>.
034    * Otherwise, the parser will throw a {@link ParseException}.
035    *
036    * @param <T> The class type of the bean.
037    * @param session The parser session.
038    * @param propertyName The property name encountered in the document.
039    * @param beanClass The bean class.
040    * @param bean The bean.
041    */
042   public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean) {
043      onError(session, null,
044         format("Unknown property ''{0}'' encountered while trying to parse into class ''{1}'' at location {2}",
045            propertyName, beanClass, session.getPosition())
046      );
047   }
048
049   /**
050    * Called when an exception is thrown when trying to call a bean setter method.
051    *
052    * @param session The serializer session.
053    * @param t The throwable that was thrown by the setter method.
054    * @param p The bean property we had an issue on.
055    */
056   public void onBeanSetterException(ParserSession session, Throwable t, BeanPropertyMeta p) {
057      onError(session, t, format("Could not call setValue() on property ''{0}'' of class ''{1}'', exception = {2}",
058         p.getName(), p.getBeanMeta().getClassMeta(), t.getLocalizedMessage()));
059   }
060
061   /**
062    * Called when an error occurs during parsing but is ignored.
063    *
064    * @param session The parser session.
065    * @param t The throwable that was thrown by the getter method.
066    * @param msg The error message.
067    */
068   public void onError(ParserSession session, Throwable t, String msg) {
069      // Do something with this information.
070   }
071}