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.common.internal.StringUtils.*;
016
017import org.apache.juneau.*;
018
019/**
020 * Class for listening for certain parse events during a document parse.
021 *
022 * <h5 class='section'>See Also:</h5><ul>
023 *    <li class='link'><a class="doclink" href="../../../../index.html#jm.SerializersAndParsers">Serializers and Parsers</a>
024
025 * </ul>
026 */
027public class ParserListener {
028
029   /**
030    * Represents no parser listener.
031    */
032   public static final class Void extends ParserListener {}
033
034   /**
035    * Gets called when an unknown bean property is detected in a document.
036    *
037    * <p>
038    * This method only gets called if {@link org.apache.juneau.BeanContext.Builder#ignoreUnknownBeanProperties()} setting is <jk>true</jk>.
039    * Otherwise, the parser will throw a {@link ParseException}.
040    *
041    * @param <T> The class type of the bean.
042    * @param session The parser session.
043    * @param propertyName The property name encountered in the document.
044    * @param beanClass The bean class.
045    * @param bean The bean.
046    */
047   public <T> void onUnknownBeanProperty(ParserSession session, String propertyName, Class<T> beanClass, T bean) {
048      onError(session, null,
049         format("Unknown property ''{0}'' encountered while trying to parse into class ''{1}'' at location {2}",
050            propertyName, beanClass, session.getPosition())
051      );
052   }
053
054   /**
055    * Called when an exception is thrown when trying to call a bean setter method.
056    *
057    * @param session The serializer session.
058    * @param t The throwable that was thrown by the setter method.
059    * @param p The bean property we had an issue on.
060    */
061   public void onBeanSetterException(ParserSession session, Throwable t, BeanPropertyMeta p) {
062      onError(session, t, format("Could not call setValue() on property ''{0}'' of class ''{1}'', exception = {2}",
063         p.getName(), p.getBeanMeta().getClassMeta(), t.getLocalizedMessage()));
064   }
065
066   /**
067    * Called when an error occurs during parsing but is ignored.
068    *
069    * @param session The parser session.
070    * @param t The throwable that was thrown by the getter method.
071    * @param msg The error message.
072    */
073   public void onError(ParserSession session, Throwable t, String msg) {
074      // Do something with this information.
075   }
076}