001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.content; 017 018import javax.xml.transform.TransformerException; 019 020import org.apache.xpath.XPathAPI; 021import org.w3c.dom.Node; 022import org.w3c.dom.NodeList; 023 024import org.ametys.cms.repository.ModifiableContent; 025import org.ametys.plugins.repository.data.extractor.xml.XMLValuesExtractorAdditionalDataGetter; 026import org.ametys.plugins.repository.tag.TaggableAmetysObject; 027import org.ametys.web.repository.content.ModifiableWebContent; 028import org.ametys.web.repository.content.WebContent; 029 030/** 031 * Component responsible to extract a {@link WebContent} from an XML document, including tags. 032 */ 033public class ContentExtractor extends org.ametys.cms.content.ContentExtractor 034{ 035 /** Avalon role. */ 036 public static final String WEB_CONTENT_EXTACTOR_ROLE = ContentExtractor.class.getName(); 037 038 @Override 039 public void fillContent(ModifiableContent content, Node contentNode, XMLValuesExtractorAdditionalDataGetter additionalDataGetter) throws Exception 040 { 041 super.fillContent(content, contentNode, additionalDataGetter); 042 043 if (content instanceof ModifiableWebContent) 044 { 045 fillSiteName((ModifiableWebContent) content, contentNode); 046 } 047 048 fillTags(content, contentNode); 049 } 050 051 /** 052 * Fills the given content with the site name from the provided {@link org.w3c.dom.Node} 053 * @param content The content to fill 054 * @param contentNode the node to read to get the site name 055 * @throws TransformerException if an error occurs 056 */ 057 protected void fillSiteName(ModifiableWebContent content, Node contentNode) throws TransformerException 058 { 059 String siteName = XPathAPI.eval(contentNode, "site/@name").str(); 060 content.setSiteName(siteName); 061 } 062 063 /** 064 * Fills the given {@link TaggableAmetysObject} with the tags from the provided {@link org.w3c.dom.Node} 065 * @param taggable The {@link TaggableAmetysObject} to fill 066 * @param contentNode the node to read to get the tags 067 * @throws TransformerException if an error occurs 068 */ 069 protected void fillTags(TaggableAmetysObject taggable, Node contentNode) throws TransformerException 070 { 071 NodeList tagsNode = XPathAPI.selectNodeList(contentNode, "content/tags/*"); 072 for (int i = 0; i < tagsNode.getLength(); i++) 073 { 074 Node tagNode = tagsNode.item(i); 075 String tagName = tagNode.getNodeName(); 076 taggable.tag(tagName); 077 } 078 } 079}