Skip to content

Commit

Permalink
Merge pull request #132 from occamlabs/gfi-forceproperty
Browse files Browse the repository at this point in the history
GetFeatureInfo forceproperty feature
  • Loading branch information
MrSnyder committed Jul 2, 2013
2 parents 85e9f81 + 904bf9c commit 76f816a
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ TemplateDefinitionStart: '<?template ';
MapDefinitionStart: '<?map ';
FeatureCallStart: '<?feature ';
PropertyCallStart: '<?property ';
ForcePropertyCallStart: '<?forceproperty ';
NameStart: '<?name';
ValueStart: '<?value';
OddStart: '<?odd';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ templatebody[TemplateDefinition def]:
templatebodytext { $def.body.add($templatebodytext.text); }
| featurecall { $def.body.add($featurecall.call); }
| propertycall { $def.body.add($propertycall.call); }
| forcepropertycall { $def.body.add($forcepropertycall.call); }
| name { $def.body.add($name.name); }
| value { $def.body.add($value.value); }
| odd { $def.body.add($odd.even); }
Expand Down Expand Up @@ -80,7 +81,13 @@ propertycall returns [PropertyTemplateCall call]
@init {
List<String> patterns = new ArrayList<String>();
}:
PropertyCallStart templateselector[patterns] Colon ID TagClose { $call = new PropertyTemplateCall($ID.text, patterns, $templateselector.negate); };
PropertyCallStart templateselector[patterns] Colon ID TagClose { $call = new PropertyTemplateCall($ID.text, patterns, $templateselector.negate, false); };

forcepropertycall returns [PropertyTemplateCall call]
@init {
List<String> patterns = new ArrayList<String>();
}:
ForcePropertyCallStart templateselector[patterns] Colon ID TagClose { $call = new PropertyTemplateCall($ID.text, patterns, $templateselector.negate, true); };

templateselector[List<String> patterns] returns [Boolean negate]:
Not WS* BracketLeft templatepatterns[patterns] BracketRight { $negate = true; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ public static void runTemplate( OutputStream response, String fiFile, FeatureCol
HashMap<String, Object> defs = (HashMap) parser.definitions();

StringBuilder sb = new StringBuilder();
new PropertyTemplateCall( "start", singletonList( "*" ), false ).eval( sb, defs, col, geometries );
new PropertyTemplateCall( "start", singletonList( "*" ), false, false ).eval( sb, defs, col, geometries );
out.println( sb.toString() );
} catch ( Throwable e ) {
} catch ( Exception e ) {
e.printStackTrace();
if ( fiFile == null ) {
LOG.error( "Could not load internal template for GFI response." );
} else {
LOG.error( "Could not load template '{}' for GFI response.", fiFile );
LOG.error( "Could not load template '{}' for GFI response: " + e.getLocalizedMessage(), fiFile );
}
LOG.trace( "Stack trace:", e );
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void eval( StringBuilder sb, HashMap<String, Object> defs, Object obj, in
new FeatureTemplateCall( name, singletonList( "*" ), false ).eval( sb, defs, obj, geometries );
}
if ( obj instanceof Property ) {
new PropertyTemplateCall( name, singletonList( "*" ), false ).eval( sb, defs, obj, geometries );
new PropertyTemplateCall( name, singletonList( "*" ), false, false ).eval( sb, defs, obj, geometries );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@
import static org.deegree.featureinfo.templating.lang.Util.getMatchingObjects;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.xml.namespace.QName;

import org.deegree.commons.tom.gml.property.Property;
import org.deegree.commons.tom.primitive.BaseType;
import org.deegree.feature.Feature;
import org.deegree.feature.FeatureCollection;
import org.deegree.feature.property.SimpleProperty;
import org.deegree.feature.types.property.SimplePropertyType;
import org.deegree.geometry.Geometry;
import org.slf4j.Logger;

Expand All @@ -70,15 +76,18 @@ public class PropertyTemplateCall {

private final boolean negate;

private boolean force;

/**
* @param name
* @param patterns
* @param negate
*/
public PropertyTemplateCall( String name, List<String> patterns, boolean negate ) {
public PropertyTemplateCall( String name, List<String> patterns, boolean negate, boolean force ) {
this.name = name;
this.patterns = patterns;
this.negate = negate;
this.force = force;
}

private void eval( StringBuilder sb, TemplateDefinition t, Object obj, HashMap<String, Object> defs,
Expand Down Expand Up @@ -167,7 +176,16 @@ public void eval( StringBuilder sb, HashMap<String, Object> defs, Object obj, bo
List<Property> inputProps = ( (Feature) obj ).getProperties();
Property[] propArray = inputProps.toArray( new Property[inputProps.size()] );

List<Property> props = getMatchingObjects( propArray, patterns, negate, geometries );
List<Property> props;
if ( !force ) {
props = getMatchingObjects( propArray, patterns, negate, geometries );
} else {
props = new ArrayList<Property>();
for ( String p : patterns ) {
SimplePropertyType tp = new SimplePropertyType( new QName( p ), 0, 0, BaseType.STRING, null, null );
props.add( new SimpleProperty( tp, "" ) );
}
}

LOG.debug( "Property template call '{}' matches objects '{}'.", name, props );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ Occam Labs UG (haftungsbeschränkt)
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import junit.framework.Assert;

import org.deegree.feature.FeatureCollection;
import org.junit.Assert;
import org.junit.Test;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,24 @@ Occam Labs UG (haftungsbeschränkt)
----------------------------------------------------------------------------*/
package org.deegree.featureinfo.templating;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;

import junit.framework.Assert;

import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.deegree.commons.tom.gml.property.Property;
import org.deegree.feature.Feature;
import org.deegree.feature.FeatureCollection;
import org.deegree.feature.GenericFeature;
import org.deegree.feature.GenericFeatureCollection;
import org.deegree.featureinfo.templating.lang.Definition;
import org.junit.Assert;
import org.junit.Test;

/**
Expand Down Expand Up @@ -119,6 +125,27 @@ public void testStandardTemplate()
Assert.assertEquals( 0, parser.getNumberOfSyntaxErrors() );
}

@Test
public void testForceProperty()
throws IOException, RecognitionException {
Templating2Parser parser = getParser( "forceproperty.gfi" );
Map<String, Definition> defs = parser.definitions();
Assert.assertEquals( 6, defs.size() );
Assert.assertEquals( 0, parser.getNumberOfSyntaxErrors() );
}

@Test
public void testForcePropertyEval()
throws URISyntaxException, IOException {
File file = new File( Templating2ParserTest.class.getResource( "forcepropertyeval.gfi" ).toURI() );
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FeatureCollection col = new GenericFeatureCollection();
Feature f = new GenericFeature( null, null, new ArrayList<Property>(), null );
col.add( f );
TemplatingUtils.runTemplate( bos, file.toString(), col, false );
Assert.assertTrue( new String( bos.toByteArray() ).contains( "<li>\ntest\n</li>" ) );
}

private static Templating2Parser getParser( String name )
throws IOException {
CharStream input = new ANTLRInputStream( Templating2ParserTest.class.getResourceAsStream( name ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?template start>
<html>
<head>
<title>deegree Feature Info</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'></meta>
</head>
<body onload="loaded()">
<script type="text/javascript">
var headerStyle = 'font-family: Verdana, "Times New Roman", Times, serif; font-size: 110%; font-weight: normal; font-style: italic; background: #c1cde5; text-align: left;'
var oddStyle = 'background-color: #ffffff;'
var evenStyle = 'background-color: #D9E8FB;'
var first

function loaded() {
var sw = document.getElementById('theswitch')
var opt

<?feature *:options>

first()
sw.value = sw.options[0].value // why're they counting from 0 here, and not from 1?
}

function addText(node, text){
if(!node.ownerDocument) node.ownerDocument = document
node.appendChild(node.ownerDocument.createTextNode(text))
}

function newnode(node, name, text){
if(!node.ownerDocument) node.ownerDocument = document
var n = node.appendChild(node.ownerDocument.createElement(name))
if(text) addText(n, text)
return n
}

var tags = ["p", "a", "td", "tr", "th", "h1", "h2", "h3", "h4", "h5", "h6", "ul", "ol", "li", "table", "tbody", "b", "br", "i"]
// beware of the scope...
for(var tag in tags) {
this[tags[tag]] = function(x){
return function (node, text){
return newnode(node, x, text)
}
}(tags[tag])
}

function dotheswitch() {
this[document.getElementById('theswitch').value + "_switchfunction"]()
}
<?feature *:switchfunction>
</script>
<p>
Please select a feature type:
</p>
<form>
<select id="theswitch" onchange="dotheswitch()">
</select>
</form>
<div id="table"></div>
</body>
</html>

<?template options>
opt = document.getElementById('<?name>')
if(!opt){
opt = newnode(sw, 'option', '<?name:map ftname>')
opt.setAttribute('id', '<?name>')
opt.setAttribute('value', '<?name>')
}

<?template switchfunction>
if(! this.<?name>_switchfunction){
this.<?name>_switchfunction = function(){
var div = document.getElementById('table')
while(div.firstChild) div.removeChild(div.firstChild)
var table = this.tbody(this.table(div))
var row = tr(table)
th(row, "Field").style.cssText = headerStyle
th(row, "Value").style.cssText = headerStyle
for(var i in <?name>){
row = tr(table)
td(row, 'GML ID').style.cssText = headerStyle
td(row, '<?gmlid>').style.cssText = headerStyle
<?name>[i](table)
}
}

<?name> = []
}

<?name>[<?name>.length] = function (table) {
var odd = true
var row
<?property *:properties>
<?forceproperty prop1,prop2:properties>
}

if(!first) first = <?name>_switchfunction

<?template properties>
row = tr(table)
td(row, '<?name:map props>').style.cssText = odd ? oddStyle : evenStyle
td(row, '<?value>').style.cssText = odd ? oddStyle : evenStyle
odd = !odd

<?map props>
NAME=Name
POP_2000=Population
STATE=State

<?map ftname>
SGID93_LOCATION_UDOTMap_CityLocations=Cities in Utah County
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?template start>
<html>
<head>
<title>force property test</title>
</head>
<body>
<ol>
<?feature *:featuretmpl>
</ol>
</body>
</html>

<?template featuretmpl>
<li><?forceproperty test:propertytmpl></li>

<?template propertytmpl>
<?name>
Loading

0 comments on commit 76f816a

Please sign in to comment.