@Override
 public void toResource(Resource resource, ResourceFilter filter) throws RepositoryException {
   super.toResource(resource, filter);
   ResourceFilter.FilterSet filterSet = (ResourceFilter.FilterSet) filter;
   ResourceHandle handle = ResourceHandle.use(resource);
   ResourceFilter.FilterSet.Rule rule = filterSet.getRule();
   handle.setProperty(PROPERTY_RULE, rule.name());
   List<ResourceFilter> set = filterSet.getSet();
   ResourceHandle entry;
   for (int i = 0; i < set.size(); i++) {
     entry =
         ResourceHandle.use(
             ResourceUtil.getOrCreateChild(
                 resource, NODE_NAME_ENTRY + "-" + i, ResourceUtil.TYPE_UNSTRUCTURED));
     ResourceFilterMapping.toResource(entry, set.get(i));
   }
 }
 public static ResourceFilter fromString(String rules) {
   ResourceFilter filter = ResourceFilter.ALL;
   Matcher matcher = FILTER_SET_PATTERN.matcher(rules);
   if (matcher.matches()) {
     String type = matcher.group(1);
     String values = matcher.group(2);
     try {
       ResourceFilter.FilterSet.Rule rule = ResourceFilter.FilterSet.Rule.valueOf(type);
       List<ResourceFilter> filters = new ArrayList<>();
       String nextRule = "";
       for (String item : StringUtils.split(values, ',')) {
         nextRule += item;
         if (StringUtils.isBlank(nextRule)
             || STRING_PATTERN.matcher(nextRule).matches()
             || FILTER_SET_PATTERN.matcher(nextRule).matches()) {
           filters.add(fromString(nextRule));
           nextRule = "";
         } else {
           nextRule += ",";
         }
       }
       filter = new ResourceFilter.FilterSet(rule, filters);
     } catch (Exception ex) {
       LOG.error(ex.getMessage(), ex);
     }
   } else {
     matcher = STRING_PATTERN.matcher(rules);
     if (matcher.matches()) {
       String type = matcher.group(1);
       if (StringUtils.isBlank(type)) {
         type = DEFAULT_FILTER_TYPE;
       }
       String values = matcher.group(2);
       try {
         StringFilter stringFilter = StringFilterMapping.fromString(values);
         Class<?> filterClass =
             Class.forName(ResourceFilter.class.getName() + "$" + type + "Filter");
         if (ResourceFilter.AllFilter.class.equals(filterClass)) {
           filter = ResourceFilter.ALL;
         } else if (ResourceFilter.FolderFilter.class.equals(filterClass)) {
           filter = ResourceFilter.FOLDER;
         } else {
           filter =
               (ResourceFilter)
                   filterClass.getConstructor(StringFilter.class).newInstance(stringFilter);
         }
       } catch (Exception ex) {
         LOG.error(ex.getMessage(), ex);
       }
     } else {
       LOG.error("invalid filter rule: '" + rules + "'");
     }
   }
   return filter;
 }
 @Override
 protected ResourceFilter createInstance(
     ResourceHandle resource, Class<? extends ResourceFilter> type) throws Exception {
   ResourceFilter.FilterSet.Rule rule =
       ResourceFilter.FilterSet.Rule.valueOf(resource.getProperty(PROPERTY_RULE, (String) null));
   List<ResourceHandle> filterResources =
       resource.getChildrenByResourceType(RESOURCE_FILTER_TYPE);
   List<ResourceFilter> filterList = new ArrayList<>();
   for (ResourceHandle filterRes : filterResources) {
     ResourceFilter filter = ResourceFilterMapping.fromResource(filterRes);
     filterList.add(filter);
   }
   ResourceFilter filter =
       type.getConstructor(ResourceFilter.FilterSet.Rule.class, List.class)
           .newInstance(rule, filterList);
   return filter;
 }