Example usage for com.google.gson JsonElement getAsJsonArray

List of usage examples for com.google.gson JsonElement getAsJsonArray

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonArray.

Prototype

public JsonArray getAsJsonArray() 

Source Link

Document

convenience method to get this element as a JsonArray .

Usage

From source file:com.sldeditor.exportdata.esri.MXDParser.java

License:Open Source License

/**
 * Import layer.//from   www  .ja  v  a 2s  .c om
 *
 * @param layer the layer
 * @param outputFormat the output format
 * @return the styled layer descriptor
 */
private SLDDataInterface importLayer(JsonObject layer, SLDOutputFormatEnum outputFormat) {

    StyledLayerDescriptor sld = null;
    String layerName = layer.get("name").getAsString();

    double minScale = layer.get("minScale").getAsDouble();
    double maxScale = layer.get("maxScale").getAsDouble();

    int transparency = layer.get("transparency").getAsInt();

    JsonElement renderElement = layer.get("renderer");
    sld = getRenderer(layerName, minScale, maxScale, transparency, renderElement);

    JsonElement labelRenderArrayElement = layer.get("labelRenderers");
    if (labelRenderArrayElement != null) {
        processLabelRenderer(sld, labelRenderArrayElement.getAsJsonArray(), transparency);
    }

    JsonElement fieldArray = layer.get("fields");
    List<DataSourceFieldInterface> fieldList = processFields(layerName, fieldArray);

    JsonElement dataSourcePropertiesElement = layer.get("dataSource");
    DataSourcePropertiesInterface dataSourceProperties = processDataSource(layerName,
            dataSourcePropertiesElement);

    String sldContents = SLDWriterFactory.createWriter(outputFormat).encodeSLD(sld);

    StyleWrapper styleWrapper = new StyleWrapper(layerName, layerName);
    SLDDataInterface sldData = new SLDData(styleWrapper, sldContents);
    sldData.setDataSourceProperties(dataSourceProperties);
    sldData.setFieldList(fieldList);
    sldData.setReadOnly(true);

    return sldData;
}

From source file:com.sldeditor.exportdata.esri.MXDParser.java

License:Open Source License

/**
 * Process fields.//from   w  w w.  j  ava2 s.  c  o m
 *
 * @param layerName the layer name
 * @param fieldArrayElement the field array element
 * @return the list
 */
private List<DataSourceFieldInterface> processFields(String layerName, JsonElement fieldArrayElement) {
    List<DataSourceFieldInterface> fieldList = new ArrayList<DataSourceFieldInterface>();

    if (fieldArrayElement != null) {
        JsonArray fieldArray = fieldArrayElement.getAsJsonArray();

        for (int index = 0; index < fieldArray.size(); index++) {
            JsonObject fieldObject = null;

            try {
                fieldObject = fieldArray.get(index).getAsJsonObject();
            } catch (IllegalStateException e) {
                ConsoleManager.getInstance().error(this, "Layer : " + layerName);
                ConsoleManager.getInstance().exception(this, e);
            }

            if (fieldObject != null) {
                Class<?> fieldType = convertFieldType(fieldObject.get("type").getAsString());

                DataSourceField esriField = new DataSourceField(fieldObject.get("field").getAsString(),
                        fieldType);

                fieldList.add(esriField);
            }
        }
    }

    return fieldList;
}

From source file:com.sldeditor.exportdata.esri.renderer.UniqueValueRenderer.java

License:Open Source License

@Override
public StyledLayerDescriptor convert(JsonObject json, String layerName, double minScale, double maxScale,
        int transparency) {
    StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();

    NamedLayer namedLayer = styleFactory.createNamedLayer();

    boolean useDefaultSymbol = false;
    JsonElement useDefaultSymbolElement = json.get(UniqueValueRendererKeys.USE_DEFAULTSYMBOL);
    if (useDefaultSymbolElement != null) {
        useDefaultSymbol = useDefaultSymbolElement.getAsBoolean();
    }// www  .j  a va2 s.c  o  m

    JsonElement jsonElement = json.get(CommonRendererKeys.LABEL);
    if (jsonElement != null) {
        namedLayer.setName(jsonElement.getAsString());
    }
    sld.addStyledLayer(namedLayer);

    Style style = styleFactory.createStyle();
    namedLayer.addStyle(style);

    List<FeatureTypeStyle> ftsList = style.featureTypeStyles();

    // style.setAbstract(json.get(IntermediateFileKeys.DESCRIPTION).getAsString());

    FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();

    ftsList.add(fts);

    JsonElement element = json.get(UniqueValueRendererKeys.VALUES);
    if (element != null) {
        JsonArray valueList = element.getAsJsonArray();

        for (int index = 0; index < valueList.size(); index++) {
            JsonElement valueElement = valueList.get(index);
            if (valueElement != null) {
                Rule rule = styleFactory.createRule();
                JsonObject valueObj = valueElement.getAsJsonObject();

                String value = getString(valueObj, UniqueValueRendererKeys.VALUES_VALUE);

                String label = getString(valueObj, UniqueValueRendererKeys.VALUES_LABEL);
                rule.setName(label);

                createFilter(rule, json.get(UniqueValueRendererKeys.FIELDS),
                        json.get(UniqueValueRendererKeys.FIELD_DELIMETER), value);

                // Heading /description
                String heading = getString(valueObj, UniqueValueRendererKeys.VALUES_HEADING);
                @SuppressWarnings("unused")
                String description = getString(valueObj, UniqueValueRendererKeys.VALUES_DESCRIPTION);

                if ((heading != null) && !heading.isEmpty() || (label != null) && !label.isEmpty()) {
                    if (label == null) {
                        label = "";
                    }
                    InternationalString titleString = Text.text(label);

                    if (heading == null) {
                        heading = "";
                    }

                    InternationalString descriptionString = Text.text(heading);

                    Description descriptionObj = styleFactory.description(titleString, descriptionString);

                    rule.setDescription(descriptionObj);
                }

                if (minScale > 0.0) {
                    rule.setMinScaleDenominator(minScale);
                }

                if (maxScale > 0.0) {
                    rule.setMaxScaleDenominator(maxScale);
                }

                JsonElement jsonSymbolElement = valueObj.get(UniqueValueRendererKeys.VALUES_SYMBOL);
                SymbolManager.getInstance().convertSymbols(rule, layerName, transparency, jsonSymbolElement);

                if (useDefaultSymbol && value == null) {
                    rule.setIsElseFilter(true);
                }
                fts.rules().add(rule);
            }
        }
    }
    return sld;
}

From source file:com.sldeditor.exportdata.esri.renderer.UniqueValueRenderer.java

License:Open Source License

/**
 * Creates the filter.//  w w  w  .  j  a va2s.  c om
 *
 * @param rule the rule
 * @param fieldsElement the fields element
 * @param fieldDelimiterElement the field delimiter element
 * @param value the value
 */
private void createFilter(Rule rule, JsonElement fieldsElement, JsonElement fieldDelimiterElement,
        String value) {
    if (fieldsElement == null)
        return;

    if (value == null) {
        rule.setElseFilter(true);
    } else {
        List<String> fieldList = new ArrayList<String>();

        JsonArray fieldArray = fieldsElement.getAsJsonArray();

        for (int fieldIndex = 0; fieldIndex < fieldArray.size(); fieldIndex++) {
            JsonElement jsonFieldElement = fieldArray.get(fieldIndex);
            if (jsonFieldElement != null) {
                JsonObject fieldObj = jsonFieldElement.getAsJsonObject();
                fieldList.add(fieldObj.get("name").getAsString());
            }
        }

        String[] values = null;

        if (fieldDelimiterElement != null) {
            values = value.split(fieldDelimiterElement.getAsString());
        } else {
            values = new String[1];
            values[0] = value;
        }

        List<Filter> filterList = new ArrayList<Filter>();

        int index = 0;
        while (index < values.length) {
            Expression fieldExpression = filterFactory.property(fieldList.get(index));
            Expression valueExpression = filterFactory.literal(values[index]);

            PropertyIsEqualTo filter = filterFactory.equals(fieldExpression, valueExpression);

            filterList.add(filter);
            index++;
        }

        Filter completeFilter = null;

        if (filterList.size() > 1) {
            completeFilter = filterFactory.and(filterList);
        } else if (filterList.size() == 1) {
            completeFilter = filterList.get(0);
        }

        rule.setFilter(completeFilter);
    }
}

From source file:com.sldeditor.exportdata.esri.symbols.CartographicLineSymbol.java

License:Open Source License

/**
 * Convert.//from   w w w. j  a v a2  s  . co  m
 *
 * @param element the element
 * @return the list of strokes
 */
@SuppressWarnings("deprecation")
@Override
public List<Stroke> convert(JsonElement element) {

    if (element == null)
        return null;

    JsonObject obj = element.getAsJsonObject();

    List<Stroke> strokeList = new ArrayList<Stroke>();

    double width = getDouble(obj, CommonSymbolKeys.WIDTH);

    Stroke stroke = styleFactory.createStroke(getColour(obj.get(CommonSymbolKeys.COLOUR)), ff.literal(width));

    // Line dash pattern
    JsonElement templateElement = obj.get(CartographicLineSymbolKeys.TEMPLATE);
    if (templateElement != null) {
        JsonArray templateArray = templateElement.getAsJsonArray();
        List<Float> patternList = new ArrayList<Float>();

        for (int index = 0; index < templateArray.size(); index++) {
            JsonObject patternElement = templateArray.get(index).getAsJsonObject();
            float mark = 0.0f;
            JsonElement markElement = patternElement.get(CartographicLineSymbolKeys.MARK);
            if (markElement != null) {
                mark = markElement.getAsFloat();
            }
            patternList.add(mark);

            float gap = 0.0f;
            JsonElement gapElement = patternElement.get(CartographicLineSymbolKeys.GAP);
            if (gapElement != null) {
                gap = gapElement.getAsFloat();
            }
            patternList.add(gap);
        }

        float[] dashArray = new float[patternList.size()];

        int index = 0;
        for (Float value : patternList) {
            dashArray[index] = value;
            index++;
        }

        stroke.setDashArray(dashArray);

        // Start of line offset
        JsonElement lineStartOffsetElement = obj.get(CartographicLineSymbolKeys.LINE_START_OFFSET);

        if (lineStartOffsetElement != null) {
            stroke.setDashOffset(ff.literal(lineStartOffsetElement.getAsDouble()));
        }
    }

    strokeList.add(stroke);

    return strokeList;
}

From source file:com.sldeditor.exportdata.esri.symbols.MultiLayerFillSymbol.java

License:Open Source License

@Override
public void convert(Rule rule, JsonElement element, String layerName, int transparency) {
    if (rule == null)
        return;//www  .  j ava2 s  . c o m
    if (element == null)
        return;

    JsonArray layerArray = element.getAsJsonArray();

    List<Symbolizer> symbolizerList = rule.symbolizers();

    if (layerArray.size() > 0) {
        for (int index = 0; index < layerArray.size(); index++) {
            JsonObject obj = layerArray.get(index).getAsJsonObject();

            // Handle fills
            List<Symbolizer> createdSymbolizerList = SymbolManager.getInstance().getFillSymbol(layerName,
                    obj.get(MultiLayerFillSymbolKeys.FILL), transparency);

            if (symbolizerList != null) {
                symbolizerList.addAll(createdSymbolizerList);
            }

            // Handle strokes
            JsonElement jsonOutlineElement = obj.get(MultiLayerFillSymbolKeys.OUTLINE);

            List<Stroke> strokeList = SymbolManager.getInstance().getStrokeList(jsonOutlineElement);

            if (strokeList != null) {
                List<Symbolizer> createdLineSymbolizerList = new ArrayList<Symbolizer>();
                for (Stroke stroke : strokeList) {
                    LineSymbolizer lineSymbol = styleFactory.createLineSymbolizer();

                    lineSymbol.setStroke(stroke);

                    createdLineSymbolizerList.add(lineSymbol);
                }
                symbolizerList.addAll(createdLineSymbolizerList);
            }
        }
    }
}

From source file:com.sldeditor.exportdata.esri.symbols.MultiLayerLineSymbol.java

License:Open Source License

@Override
public List<Stroke> convert(JsonElement element) {

    if (element == null)
        return null;

    List<Stroke> strokeList = null;

    JsonArray layerList = element.getAsJsonArray();
    if (layerList.size() > 0) {
        strokeList = new ArrayList<Stroke>();

        for (int index = 0; index < layerList.size(); index++) {
            JsonObject obj = layerList.get(index).getAsJsonObject();

            List<Stroke> strokeSymbolList = SymbolManager.getInstance()
                    .getStrokeList(obj.get(MultiLayerLineSymbolKeys.LINE));
            if (strokeSymbolList != null) {
                strokeList.addAll(strokeSymbolList);
            }/*from   w w w .jav  a 2 s.c om*/
        }
    }
    return strokeList;
}

From source file:com.sldeditor.exportdata.esri.symbols.MultiLayerLineSymbol.java

License:Open Source License

/**
 * Convert.//from   www  . j  a v a  2 s.  c o  m
 *
 * @param rule the rule
 * @param element the element
 * @param layerName the layer name
 * @param transparency the transparency
 */
@Override
public void convert(Rule rule, JsonElement element, String layerName, int transparency) {
    if (rule == null)
        return;
    if (element == null)
        return;

    JsonArray layerArray = element.getAsJsonArray();

    List<Symbolizer> symbolizerList = rule.symbolizers();

    if (layerArray.size() > 0) {
        for (int index = 0; index < layerArray.size(); index++) {
            JsonObject obj = layerArray.get(index).getAsJsonObject();

            List<Stroke> strokeList = null;
            JsonElement jsonOutlineElement = obj.get(MultiLayerLineSymbolKeys.LINE);

            strokeList = SymbolManager.getInstance().getStrokeList(jsonOutlineElement);

            if (strokeList != null) {
                for (Stroke stroke : strokeList) {
                    LineSymbolizer lineSymbolizer = styleFactory.createLineSymbolizer(stroke, null);

                    symbolizerList.add(lineSymbolizer);
                }
            }
        }
    }
}

From source file:com.sldeditor.exportdata.esri.symbols.MultiLayerLineSymbol.java

License:Open Source License

@Override
public List<Symbolizer> convertToFill(String layerName, JsonElement element, int transparency) {
    if (element == null)
        return null;

    List<Symbolizer> symbolizerList = null;

    JsonArray layerList = element.getAsJsonArray();
    if (layerList.size() > 0) {
        symbolizerList = new ArrayList<Symbolizer>();

        for (int index = 0; index < layerList.size(); index++) {
            JsonElement layerElement = layerList.get(index);

            symbolizerList//from w  w w.j a v  a2 s .com
                    .addAll(SymbolManager.getInstance().getFillSymbol(layerName, layerElement, transparency));
        }
    }
    return symbolizerList;
}

From source file:com.sldeditor.exportdata.esri.symbols.MultiLayerMarkerSymbol.java

License:Open Source License

@Override
public List<Graphic> convert(JsonElement element) {

    if (element == null)
        return null;

    List<Graphic> markList = null;

    JsonArray layerList = element.getAsJsonArray();
    if (layerList.size() > 0) {
        markList = new ArrayList<Graphic>();

        for (int index = 0; index < layerList.size(); index++) {
            JsonObject obj = layerList.get(index).getAsJsonObject();

            List<Graphic> markerSymbolList = SymbolManager.getInstance()
                    .getMarkerList(obj.get(MultiLayerMarkerSymbolKeys.MARKER).getAsJsonObject());
            if (markerSymbolList != null) {
                markList.addAll(markerSymbolList);
            }// w w  w  . ja v  a  2s  . c  o  m
        }
    }
    return markList;
}