001/* 002 * Copyright 2019 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.plugins.contentio.archive; 017 018import java.util.function.Predicate; 019 020import org.apache.excalibur.xml.sax.ContentHandlerProxy; 021import org.xml.sax.Attributes; 022import org.xml.sax.ContentHandler; 023import org.xml.sax.SAXException; 024 025/** 026 * ContentHandler filtering first level elements from the JCR system view export of a plugin Node. 027 */ 028public class SystemViewHandler extends ContentHandlerProxy 029{ 030 private static final String __SV_NS = "http://www.jcp.org/jcr/sv/1.0"; 031 private static final String __SV_NODE = "node"; 032 private static final String __SV_PROPERTY = "property"; 033 private static final String __SV_NAME = "sv:name"; 034 035 private boolean _inExcludedTag; 036 private int _depth; 037 038 private Predicate<String> _nodeNamePredicate; 039 private Predicate<String> _propertyNamePredicate; 040 041 /** 042 * Constructor. 043 * @param handler the destination {@link ContentHandler}. 044 * @param nodeNamePredicate first level nodes not matching this Predicate will be filtered out 045 * @param propertyNamePredicate first level properties not matching this Predicate will be filtered out 046 */ 047 public SystemViewHandler(ContentHandler handler, Predicate<String> nodeNamePredicate, Predicate<String> propertyNamePredicate) 048 { 049 super(handler); 050 _nodeNamePredicate = nodeNamePredicate; 051 _propertyNamePredicate = propertyNamePredicate; 052 } 053 054 @Override 055 public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException 056 { 057 _depth++; 058 059 if (_inExcludedTag) 060 { 061 return; 062 } 063 else if (_depth == 2 && __SV_NS.equals(uri) && (__SV_NODE.equals(loc) && _nodeNamePredicate.test(a.getValue(__SV_NAME)) 064 || __SV_PROPERTY.equals(loc) && _propertyNamePredicate.test(a.getValue(__SV_NAME)))) 065 { 066 _inExcludedTag = true; 067 return; 068 } 069 070 super.startElement(uri, loc, raw, a); 071 } 072 073 @Override 074 public void characters(char[] ch, int start, int len) throws SAXException 075 { 076 if (_inExcludedTag) 077 { 078 return; 079 } 080 081 super.characters(ch, start, len); 082 } 083 084 @Override 085 public void endElement(String uri, String loc, String raw) throws SAXException 086 { 087 _depth--; 088 089 if (_depth == 1 && __SV_NS.equals(uri)) 090 { 091 if (_inExcludedTag) 092 { 093 _inExcludedTag = false; 094 return; 095 } 096 } 097 else if (_inExcludedTag) 098 { 099 return; 100 } 101 102 super.endElement(uri, loc, raw); 103 } 104}