| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.sling.feature.analyser.task.impl; |
| |
| import org.apache.sling.feature.Extension; |
| import org.apache.sling.feature.Extensions; |
| import org.apache.sling.feature.Feature; |
| import org.apache.sling.feature.analyser.task.AnalyserTask; |
| import org.apache.sling.feature.analyser.task.AnalyserTaskContext; |
| |
| import java.io.StringReader; |
| import java.util.ArrayList; |
| import java.util.List; |
| |
| import javax.json.Json; |
| import javax.json.stream.JsonParser; |
| import javax.json.stream.JsonParser.Event; |
| |
| public class CheckApiRegionsOrder implements AnalyserTask { |
| protected static final String API_REGIONS_KEY = "api-regions"; |
| |
| @Override |
| public String getId() { |
| return "api-regions-check-order"; |
| } |
| |
| @Override |
| public String getName() { |
| return "Api Regions check order analyser task"; |
| } |
| |
| @Override |
| public final void execute(AnalyserTaskContext ctx) throws Exception { |
| String order = ctx.getConfiguration().get("order"); |
| Feature feature = ctx.getFeature(); |
| if (feature == null) { |
| reportError(ctx, "No feature found. Illegal Analyser State."); |
| return; |
| } |
| |
| if (order == null) { |
| reportError(ctx, "This analyser task must be configured: " + getId() + " for feature " + feature.getId()); |
| reportError(ctx, "Must specify configuration key 'order'."); |
| return; |
| } |
| |
| String[] sl = order.split("[,]"); |
| List<String> prescribedOrder = new ArrayList<>(); |
| for (String s : sl) { |
| s = s.trim(); |
| if (s.length() > 0) |
| prescribedOrder.add(s); |
| } |
| if (prescribedOrder.size() == 0) { |
| reportError(ctx, "No regions declared in the 'order' configuration"); |
| return; |
| } |
| |
| // extract and check the api-regions |
| Extensions extensions = feature.getExtensions(); |
| Extension apiRegionsExtension = extensions.getByName(API_REGIONS_KEY); |
| if (apiRegionsExtension == null) { |
| // no need to be analyzed |
| return; |
| } |
| |
| String jsonRepresentation = apiRegionsExtension.getJSON(); |
| if (jsonRepresentation == null || jsonRepresentation.isEmpty()) { |
| // no need to be analyzed |
| return; |
| } |
| |
| int regionIdx = 0; |
| JsonParser parser = Json.createParser(new StringReader(jsonRepresentation)); |
| while (parser.hasNext()) { |
| Event event = parser.next(); |
| if (Event.KEY_NAME == event) { |
| switch (parser.getString()) { |
| case "name": |
| parser.next(); |
| String name = parser.getString(); |
| if (!prescribedOrder.contains(name)) { |
| reportError(ctx, "Region found with undeclared name: " + name); |
| return; |
| } |
| int prevIdx = regionIdx; |
| regionIdx = validateRegion(regionIdx, prescribedOrder, name); |
| if (regionIdx < 0) { |
| reportError(ctx, "Region '" + name + "' appears in the wrong order. It appears after '" |
| + prescribedOrder.get(prevIdx) + "'. Order of regions should be " + prescribedOrder); |
| return; |
| } |
| } |
| } |
| } |
| } |
| |
| private int validateRegion(int regionIdx, List<String> order, String name) { |
| for (int i=regionIdx; i<order.size(); i++) { |
| if (name.equals(order.get(i))) { |
| return i; |
| } |
| } |
| return -1; |
| } |
| |
| private String getPrefix() { |
| return getId() + ": "; |
| } |
| |
| private void reportError(AnalyserTaskContext ctx, String err) { |
| ctx.reportError(getPrefix() + err); |
| } |
| } |