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.html;
014
015import java.util.*;
016import java.util.concurrent.*;
017
018import org.apache.juneau.*;
019import org.apache.juneau.annotation.*;
020import org.apache.juneau.parser.*;
021import org.apache.juneau.xml.*;
022
023/**
024 * Parses text generated by the {@link HtmlSerializer} class back into a POJO model.
025 *
026 * <h5 class='topic'>Media types</h5>
027 *
028 * Handles <c>Content-Type</c> types:  <bc>text/html</bc>
029 *
030 * <h5 class='topic'>Description</h5>
031 *
032 * See the {@link HtmlSerializer} class for a description of the HTML generated.
033 * <p>
034 * This class is used primarily for automated testing of the {@link HtmlSerializer} class.
035 */
036@ConfigurableContext
037public class HtmlParser extends XmlParser implements HtmlMetaProvider, HtmlCommon {
038
039   //-------------------------------------------------------------------------------------------------------------------
040   // Configurable properties
041   //-------------------------------------------------------------------------------------------------------------------
042
043   static final String PREFIX = "HtmlParser";
044
045   //-------------------------------------------------------------------------------------------------------------------
046   // Predefined instances
047   //-------------------------------------------------------------------------------------------------------------------
048
049   /** Default parser, all default settings.*/
050   public static final HtmlParser DEFAULT = new HtmlParser(PropertyStore.DEFAULT);
051
052
053   //-------------------------------------------------------------------------------------------------------------------
054   // Instance
055   //-------------------------------------------------------------------------------------------------------------------
056
057   private final Map<ClassMeta<?>,HtmlClassMeta> htmlClassMetas = new ConcurrentHashMap<>();
058   private final Map<BeanPropertyMeta,HtmlBeanPropertyMeta> htmlBeanPropertyMetas = new ConcurrentHashMap<>();
059
060   /**
061    * Constructor.
062    *
063    * @param ps The property store containing all the settings for this object.
064    */
065   public HtmlParser(PropertyStore ps) {
066      super(ps, "text/html", "text/html+stripped");
067   }
068
069   @Override /* Context */
070   public HtmlParserBuilder builder() {
071      return new HtmlParserBuilder(getPropertyStore());
072   }
073
074   /**
075    * Instantiates a new clean-slate {@link HtmlParserBuilder} object.
076    *
077    * <p>
078    * This is equivalent to simply calling <code><jk>new</jk> HtmlParserBuilder()</code>.
079    *
080    * <p>
081    * Note that this method creates a builder initialized to all default settings, whereas {@link #builder()} copies
082    * the settings of the object called on.
083    *
084    * @return A new {@link HtmlParserBuilder} object.
085    */
086   public static HtmlParserBuilder create() {
087      return new HtmlParserBuilder();
088   }
089
090   @Override /* Parser */
091   public HtmlParserSession createSession() {
092      return createSession(createDefaultSessionArgs());
093   }
094
095   @Override /* Parser */
096   public HtmlParserSession createSession(ParserSessionArgs args) {
097      return new HtmlParserSession(this, args);
098   }
099
100   //-----------------------------------------------------------------------------------------------------------------
101   // Extended metadata
102   //-----------------------------------------------------------------------------------------------------------------
103
104   @Override /* HtmlMetaProvider */
105   public HtmlClassMeta getHtmlClassMeta(ClassMeta<?> cm) {
106      HtmlClassMeta m = htmlClassMetas.get(cm);
107      if (m == null) {
108         m = new HtmlClassMeta(cm, this);
109         htmlClassMetas.put(cm, m);
110      }
111      return m;
112   }
113
114   @Override /* HtmlMetaProvider */
115   public HtmlBeanPropertyMeta getHtmlBeanPropertyMeta(BeanPropertyMeta bpm) {
116      if (bpm == null)
117         return HtmlBeanPropertyMeta.DEFAULT;
118      HtmlBeanPropertyMeta m = htmlBeanPropertyMetas.get(bpm);
119      if (m == null) {
120         m = new HtmlBeanPropertyMeta(bpm.getDelegateFor(), this);
121         htmlBeanPropertyMetas.put(bpm, m);
122      }
123      return m;
124   }
125
126   //-----------------------------------------------------------------------------------------------------------------
127   // Other methods
128   //-----------------------------------------------------------------------------------------------------------------
129
130   @Override /* Context */
131   public ObjectMap toMap() {
132      return super.toMap()
133         .append("HtmlParser", new DefaultFilteringObjectMap());
134   }
135}