📄 getlegendgraphickvpreader.java
字号:
// the LEGEND_OPTIONS parameter gets parsed here.
req.setLegendOptions(Requests.parseOptionParameter(getValue("LEGEND_OPTIONS")));
}
/**
* Parses the STYLE, SLD and SLD_BODY parameters, as well as RULE.
*
* <p>
* STYLE, SLD and SLD_BODY are mutually exclusive. STYLE refers to a named
* style known by the server and applicable to the requested layer (i.e., it
* is exposed as one of the layer's styles in the Capabilities document).
* SLD is a URL to an externally available SLD document, and SLD_BODY is a
* string containing the SLD document itself.
* </p>
*
* <p>
* As I don't completelly understand which takes priority over which from
* the spec, I assume the precedence order as follow: SLD, SLD_BODY, STYLE,
* in decrecent order of precedence.
* </p>
*
* @param req
* @param ftype
*/
private void parseStyleAndRule(GetLegendGraphicRequest req, MapLayerInfo layer) {
String styleName = getValue("STYLE");
String sldUrl = getValue("SLD");
String sldBody = getValue("SLD_BODY");
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(new StringBuffer("looking for style ").append(styleName).toString());
}
Style sldStyle = null;
if (sldUrl != null) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("taking style from SLD parameter");
}
Style[] styles = loadRemoteStyle(sldUrl); // may throw an
// exception
sldStyle = findStyle(styleName, styles);
} else if (sldBody != null) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("taking style from SLD_BODY parameter");
}
Style[] styles = parseSldBody(sldBody); // may throw an exception
sldStyle = findStyle(styleName, styles);
} else if ((styleName != null) && !"".equals(styleName)) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("taking style from STYLE parameter");
}
sldStyle = req.getWMS().getData().getStyle(styleName);
} else {
sldStyle = layer.getDefaultStyle();
}
req.setStyle(sldStyle);
String rule = getValue("RULE");
Rule sldRule = extractRule(sldStyle, rule);
if (sldRule != null) {
req.setRule(sldRule);
}
}
/**
* Finds the Style named <code>styleName</code> in <code>styles</code>.
*
* @param styleName
* name of style to search for in the list of styles. If
* <code>null</code>, it is assumed the request is made in
* literal mode and the user has requested the first style.
* @param styles
* non null, non empty, list of styles
* @return
* @throws NoSuchElementException
* if no style named <code>styleName</code> is found in
* <code>styles</code>
*/
private Style findStyle(String styleName, Style[] styles) throws NoSuchElementException {
if ((styles == null) || (styles.length == 0)) {
throw new NoSuchElementException("No styles have been provided to search for "
+ styleName);
}
if (styleName == null) {
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer("styleName is null, request in literal mode, returning first style");
}
return styles[0];
}
if (LOGGER.isLoggable(Level.FINER)) {
LOGGER.finer(new StringBuffer("request in library mode, looking for style ").append(
styleName).toString());
}
StringBuffer noMatchNames = new StringBuffer();
for (int i = 0; i < styles.length; i++) {
if ((styles[i] != null) && styleName.equals(styles[i].getName())) {
return styles[i];
}
noMatchNames.append(styles[i].getName());
if (i < styles.length) {
noMatchNames.append(", ");
}
}
throw new NoSuchElementException(styleName + " not found. Provided style names: "
+ noMatchNames);
}
/**
* Loads a remote SLD document and parses it to a Style object
*
* @param sldUrl
* an URL to a SLD document
*
* @return the document parsed to a Style object
*
* @throws WmsException
* if <code>sldUrl</code> is not a valid URL, a stream can't
* be opened or a parsing error occurs
*/
private Style[] loadRemoteStyle(String sldUrl) throws WmsException {
InputStream in;
try {
URL url = new URL(sldUrl);
in = url.openStream();
} catch (MalformedURLException e) {
throw new WmsException(e, "Not a valid URL to an SLD document " + sldUrl,
"loadRemoteStyle");
} catch (IOException e) {
throw new WmsException(e, "Can't open the SLD URL " + sldUrl, "loadRemoteStyle");
}
return parseSld(new InputStreamReader(in));
}
/**
* Parses a SLD Style from a xml string
*
* @param sldBody
* the string containing the SLD document
*
* @return the SLD document string parsed to a Style object
*
* @throws WmsException
* if a parsing error occurs.
*/
private Style[] parseSldBody(String sldBody) throws WmsException {
// return parseSld(new StringBufferInputStream(sldBody));
return parseSld(new StringReader(sldBody));
}
/**
* Parses the content of the given input stream to an SLD Style, provided
* that a valid SLD document can be read from <code>xmlIn</code>.
*
* @param xmlIn
* where to read the SLD document from.
*
* @return the parsed Style
*
* @throws WmsException
* if a parsing error occurs
*/
private Style[] parseSld(Reader xmlIn) throws WmsException {
SLDParser parser = new SLDParser(styleFactory, xmlIn);
Style[] styles = null;
try {
styles = parser.readXML();
} catch (RuntimeException e) {
throw new WmsException(e);
}
if ((styles == null) || (styles.length == 0)) {
throw new WmsException("Document contains no styles");
}
return styles;
}
/**
* DOCUMENT ME!
*
* @param sldStyle
* @param rule
*
* @return DOCUMENT ME!
*
* @throws WmsException
*/
private Rule extractRule(Style sldStyle, String rule) throws WmsException {
Rule sldRule = null;
if ((rule != null) && !"".equals(rule)) {
FeatureTypeStyle[] fts = sldStyle.getFeatureTypeStyles();
for (int i = 0; i < fts.length; i++) {
Rule[] rules = fts[i].getRules();
for (int r = 0; r < rules.length; r++) {
if (rule.equalsIgnoreCase(rules[r].getName())) {
sldRule = rules[r];
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(new StringBuffer("found requested rule: ").append(rule)
.toString());
}
break;
}
}
}
if (sldRule == null) {
throw new WmsException("Style " + sldStyle.getName()
+ " does not contains a rule named " + rule);
}
}
return sldRule;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -