示例#1
0
  StyledLayerDescriptor parse10(
      Object input, ResourceLocator resourceLocator, EntityResolver entityResolver)
      throws IOException {

    Reader reader = null;
    try {
      // we need to close the reader if we grab one, but if it's a file it has
      // to stay as such to allow relative resource resolution during the parse
      if (!(input instanceof File)) {
        reader = toReader(input);
        input = reader;
      }
      SLDParser p = createSld10Parser(input, resourceLocator, entityResolver);
      StyledLayerDescriptor sld = p.parseSLD();
      if (sld.getStyledLayers().length == 0) {
        // most likely a style that is not a valid sld, try to actually parse out a
        // style and then wrap it in an sld
        Style[] style = p.readDOM();
        if (style.length > 0) {
          NamedLayer l = styleFactory.createNamedLayer();
          l.addStyle(style[0]);
          sld.addStyledLayer(l);
        }
      }
      return sld;
    } finally {
      IOUtils.closeQuietly(reader);
    }
  }
  public static StyledLayerDescriptor createDefaultSLD(Style style) {
    StyleFactory factory = CommonFactoryFinder.getStyleFactory();

    StyledLayerDescriptor sld = factory.createStyledLayerDescriptor();
    UserLayer layer = factory.createUserLayer();

    // FeatureTypeConstraint ftc =
    // styleFactory.createFeatureTypeConstraint(null, Filter.INCLUDE, null);
    layer.setLayerFeatureConstraints(new FeatureTypeConstraint[] {null});
    sld.addStyledLayer(layer);
    layer.addUserStyle((org.geotools.styling.Style) style);

    return sld;
  }
  /**
   * Generate SLD file. Creates everything but the FeatureTypeStyle that must be provided.
   *
   * @param fts FeatureTypeStyle. Contains all the rules and therefore the filters and symbolizers
   * @return StyledLayerDescriptor SLD file
   */
  private StyledLayerDescriptor createSLD(FeatureTypeStyle fts) {

    // create SLD
    StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
    StyledLayerDescriptor sld = styleFactory.createStyledLayerDescriptor();

    // add named layer
    NamedLayer layer = styleFactory.createNamedLayer();
    // wfs-ng: ensures we recover the correct typename
    layer.setName(_command.getFeatureTypeName()); // name must match the layer name
    fts.setName(_command.getFeatureTypeName());
    sld.addStyledLayer(layer);

    // add a custom style to the user layer
    Style style = styleFactory.createStyle();
    style.setName(_command.getFeatureTypeName());
    style.setTitle(_command.getFeatureTypeName() + "_classification");
    style.addFeatureTypeStyle(fts);
    layer.addStyle(style);

    return sld;
  }