Skip to content

Commit

Permalink
Add support for XML subtypes
Browse files Browse the repository at this point in the history
 - KML, application/vnd.google-earth.kml+xml
 - Collada, model/vnd.collada+xml
 - GML, application/gml+xml
 - GPX, application/gpx+xml
 - TCX, application/vnd.garmin.tcx+xml
  • Loading branch information
gabriel-vasile committed Apr 8, 2019
1 parent 3d7ad68 commit 0ff9399
Show file tree
Hide file tree
Showing 10 changed files with 5,334 additions and 13 deletions.
37 changes: 35 additions & 2 deletions matchers/signature.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
package matchers

import "bytes"
import (
"bytes"
"fmt"
)

type (
markupSig []byte
ciSig []byte // case insensitive signature
shebangSig []byte // matches !# followed by the signature
ftypSig []byte // matches audio/video files. www.ftyps.com
sig interface {
xmlSig struct {
// the local name of the root tag
localName []byte
// the namespace of the XML document
xmlns []byte
}
sig interface {
detect([]byte) bool
}
)

func newXmlSig(localName, xmlns string) xmlSig {
ret := xmlSig{xmlns: []byte(xmlns)}
if localName != "" {
ret.localName = []byte(fmt.Sprintf("<%s", localName))
}

return ret
}

// Implement sig interface.
func (hSig markupSig) detect(in []byte) bool {
if len(in) < len(hSig)+1 {
Expand Down Expand Up @@ -81,6 +99,21 @@ func (fSig ftypSig) detect(in []byte) bool {
bytes.Equal(in[8:12], fSig)
}

func (xSig xmlSig) detect(in []byte) bool {
l := 512
if len(in) < l {
l = len(in)
}
in = in[:l]

if len(xSig.localName) == 0 {
return bytes.Index(in, xSig.xmlns) > 0
}

localNameIndex := bytes.Index(in, xSig.localName)
return 0 < localNameIndex && localNameIndex < bytes.Index(in, xSig.xmlns)
}

func detect(in []byte, sigs []sig) bool {
for _, sig := range sigs {
if sig.detect(in) {
Expand Down
44 changes: 44 additions & 0 deletions matchers/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ var (
xmlSigs = []sig{
markupSig("<?XML"),
}
kmlSigs = []sig{
newXmlSig("kml", `xmlns="http://www.opengis.net/kml/2.2"`),
newXmlSig("kml", `xmlns="http://earth.google.com/kml/2.0"`),
newXmlSig("kml", `xmlns="http://earth.google.com/kml/2.1"`),
newXmlSig("kml", `xmlns="http://earth.google.com/kml/2.2"`),
}
colladaSigs = []sig{
newXmlSig("COLLADA", `xmlns="http://www.collada.org/2005/11/COLLADASchema"`),
}
gmlSigs = []sig{
newXmlSig("", `xmlns:gml="http://www.opengis.net/gml/3.2"`),
newXmlSig("", `xmlns:gml="http://www.opengis.net/gml/3.3/exr"`),
}
gpxSigs = []sig{
newXmlSig("gpx", `xmlns="http://www.topografix.com/GPX/1/1"`),
}
tcxSigs = []sig{
newXmlSig("TrainingCenterDatabase", `xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2"`),
}
phpSigs = []sig{
ciSig("<?PHP"),
ciSig("<?\n"),
Expand Down Expand Up @@ -147,3 +166,28 @@ func Rtf(in []byte) bool {
func Svg(in []byte) bool {
return bytes.Contains(in, []byte("<svg"))
}

// Kml matches a Keyhole Markup Language file.
func Kml(in []byte) bool {
return detect(in, kmlSigs)
}

// Collada matches a COLLAborative Design Activity file.
func Collada(in []byte) bool {
return detect(in, colladaSigs)
}

// Gml matches a Geography Markup Language file.
func Gml(in []byte) bool {
return detect(in, gmlSigs)
}

// Gpx matches a GPS Exchange Format file.
func Gpx(in []byte) bool {
return detect(in, gpxSigs)
}

// Tcx matches a Training Center XML file.
func Tcx(in []byte) bool {
return detect(in, tcxSigs)
}
13 changes: 9 additions & 4 deletions mime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ var files = map[string]*Node{

// source code
"a.html": Html,
"a.xml": Xml,
"a.svg": Svg,
"b.svg": Svg,
"a.txt": Txt,
Expand All @@ -90,6 +89,14 @@ var files = map[string]*Node{
// fonts
"a.woff": Woff,
"a.woff2": Woff2,

// XML and subtypes of XML
"a.xml": Xml,
"a.kml": Kml,
"a.dae": Collada,
"a.gml": Gml,
"a.gpx": Gpx,
"a.tcx": Tcx,
}

func TestMatching(t *testing.T) {
Expand Down Expand Up @@ -181,6 +188,7 @@ func TestGenerateSupportedMimesFile(t *testing.T) {
t.Fatal(err)
}
if _, err := f.WriteString(`## Supported MIME types
This file is automatically generated when running tests. Do not edit manually.
Extension | MIME type
--------- | --------
Expand All @@ -197,9 +205,6 @@ Extension | MIME type
t.Fatal(err)
}
}
if _, err := f.WriteString("\nThis is file automatically generated when running tests.\n"); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions supported_mimes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type
--------- | --------
Expand Down Expand Up @@ -55,6 +56,7 @@ Extension | MIME type
**dae** | model/vnd.collada+xml
**gml** | application/gml+xml
**gpx** | application/gpx+xml
**tcx** | application/vnd.garmin.tcx+xml
**php** | text/x-php; charset=utf-8
**js** | application/javascript
**lua** | text/x-lua
Expand All @@ -71,5 +73,3 @@ Extension | MIME type
**woff** | font/woff
**woff2** | font/woff2
**wasm** | application/wasm

This is file automatically generated when running tests.
198 changes: 198 additions & 0 deletions testdata/a.dae
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?xml version="1.0" encoding="utf-8"?>
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<asset>
<contributor>
<author>Blender User</author>
<authoring_tool>Blender 2.79.0 commit date:2018-03-22, commit time:14:10, hash:f4dc9f9d68b</authoring_tool>
</contributor>
<created>2019-04-08T11:32:58</created>
<modified>2019-04-08T11:32:58</modified>
<unit name="meter" meter="1"/>
<up_axis>Z_UP</up_axis>
</asset>
<library_cameras>
<camera id="Camera-camera" name="Camera">
<optics>
<technique_common>
<perspective>
<xfov sid="xfov">49.13434</xfov>
<aspect_ratio>1.777778</aspect_ratio>
<znear sid="znear">0.1</znear>
<zfar sid="zfar">100</zfar>
</perspective>
</technique_common>
</optics>
<extra>
<technique profile="blender">
<shiftx sid="shiftx" type="float">0</shiftx>
<shifty sid="shifty" type="float">0</shifty>
<YF_dofdist sid="YF_dofdist" type="float">0</YF_dofdist>
</technique>
</extra>
</camera>
</library_cameras>
<library_lights>
<light id="Lamp-light" name="Lamp">
<technique_common>
<point>
<color sid="color">1 1 1</color>
<constant_attenuation>1</constant_attenuation>
<linear_attenuation>0</linear_attenuation>
<quadratic_attenuation>0.00111109</quadratic_attenuation>
</point>
</technique_common>
<extra>
<technique profile="blender">
<type sid="type" type="int">0</type>
<flag sid="flag" type="int">0</flag>
<mode sid="mode" type="int">8192</mode>
<gamma sid="blender_gamma" type="float">1</gamma>
<red sid="red" type="float">1</red>
<green sid="green" type="float">1</green>
<blue sid="blue" type="float">1</blue>
<shadow_r sid="blender_shadow_r" type="float">0</shadow_r>
<shadow_g sid="blender_shadow_g" type="float">0</shadow_g>
<shadow_b sid="blender_shadow_b" type="float">0</shadow_b>
<energy sid="blender_energy" type="float">1</energy>
<dist sid="blender_dist" type="float">29.99998</dist>
<spotsize sid="spotsize" type="float">75</spotsize>
<spotblend sid="spotblend" type="float">0.15</spotblend>
<halo_intensity sid="blnder_halo_intensity" type="float">1</halo_intensity>
<att1 sid="att1" type="float">0</att1>
<att2 sid="att2" type="float">1</att2>
<falloff_type sid="falloff_type" type="int">2</falloff_type>
<clipsta sid="clipsta" type="float">1.000799</clipsta>
<clipend sid="clipend" type="float">30.002</clipend>
<bias sid="bias" type="float">1</bias>
<soft sid="soft" type="float">3</soft>
<compressthresh sid="compressthresh" type="float">0.04999995</compressthresh>
<bufsize sid="bufsize" type="int">2880</bufsize>
<samp sid="samp" type="int">3</samp>
<buffers sid="buffers" type="int">1</buffers>
<filtertype sid="filtertype" type="int">0</filtertype>
<bufflag sid="bufflag" type="int">0</bufflag>
<buftype sid="buftype" type="int">2</buftype>
<ray_samp sid="ray_samp" type="int">1</ray_samp>
<ray_sampy sid="ray_sampy" type="int">1</ray_sampy>
<ray_sampz sid="ray_sampz" type="int">1</ray_sampz>
<ray_samp_type sid="ray_samp_type" type="int">0</ray_samp_type>
<area_shape sid="area_shape" type="int">1</area_shape>
<area_size sid="area_size" type="float">0.1</area_size>
<area_sizey sid="area_sizey" type="float">0.1</area_sizey>
<area_sizez sid="area_sizez" type="float">1</area_sizez>
<adapt_thresh sid="adapt_thresh" type="float">0.000999987</adapt_thresh>
<ray_samp_method sid="ray_samp_method" type="int">1</ray_samp_method>
<shadhalostep sid="shadhalostep" type="int">0</shadhalostep>
<sun_effect_type sid="sun_effect_type" type="int">0</sun_effect_type>
<skyblendtype sid="skyblendtype" type="int">1</skyblendtype>
<horizon_brightness sid="horizon_brightness" type="float">1</horizon_brightness>
<spread sid="spread" type="float">1</spread>
<sun_brightness sid="sun_brightness" type="float">1</sun_brightness>
<sun_size sid="sun_size" type="float">1</sun_size>
<backscattered_light sid="backscattered_light" type="float">1</backscattered_light>
<sun_intensity sid="sun_intensity" type="float">1</sun_intensity>
<atm_turbidity sid="atm_turbidity" type="float">2</atm_turbidity>
<atm_extinction_factor sid="atm_extinction_factor" type="float">1</atm_extinction_factor>
<atm_distance_factor sid="atm_distance_factor" type="float">1</atm_distance_factor>
<skyblendfac sid="skyblendfac" type="float">1</skyblendfac>
<sky_exposure sid="sky_exposure" type="float">1</sky_exposure>
<sky_colorspace sid="sky_colorspace" type="int">0</sky_colorspace>
</technique>
</extra>
</light>
</library_lights>
<library_images/>
<library_effects>
<effect id="Material-effect">
<profile_COMMON>
<technique sid="common">
<phong>
<emission>
<color sid="emission">0 0 0 1</color>
</emission>
<ambient>
<color sid="ambient">0 0 0 1</color>
</ambient>
<diffuse>
<color sid="diffuse">0.64 0.64 0.64 1</color>
</diffuse>
<specular>
<color sid="specular">0.5 0.5 0.5 1</color>
</specular>
<shininess>
<float sid="shininess">50</float>
</shininess>
<index_of_refraction>
<float sid="index_of_refraction">1</float>
</index_of_refraction>
</phong>
</technique>
</profile_COMMON>
</effect>
</library_effects>
<library_materials>
<material id="Material-material" name="Material">
<instance_effect url="#Material-effect"/>
</material>
</library_materials>
<library_geometries>
<geometry id="Cube-mesh" name="Cube">
<mesh>
<source id="Cube-mesh-positions">
<float_array id="Cube-mesh-positions-array" count="24">1 1 -1 1 -1 -1 -1 -0.9999998 -1 -0.9999997 1 -1 1 0.9999995 1 0.9999994 -1.000001 1 -1 -0.9999997 1 -1 1 1</float_array>
<technique_common>
<accessor source="#Cube-mesh-positions-array" count="8" stride="3">
<param name="X" type="float"/>
<param name="Y" type="float"/>
<param name="Z" type="float"/>
</accessor>
</technique_common>
</source>
<source id="Cube-mesh-normals">
<float_array id="Cube-mesh-normals-array" count="36">0 0 -1 0 0 1 1 0 -2.38419e-7 0 -1 -4.76837e-7 -1 2.38419e-7 -1.49012e-7 2.68221e-7 1 2.38419e-7 0 0 -1 0 0 1 1 -5.96046e-7 3.27825e-7 -4.76837e-7 -1 0 -1 2.38419e-7 -1.19209e-7 2.08616e-7 1 0</float_array>
<technique_common>
<accessor source="#Cube-mesh-normals-array" count="12" stride="3">
<param name="X" type="float"/>
<param name="Y" type="float"/>
<param name="Z" type="float"/>
</accessor>
</technique_common>
</source>
<vertices id="Cube-mesh-vertices">
<input semantic="POSITION" source="#Cube-mesh-positions"/>
</vertices>
<triangles material="Material-material" count="12">
<input semantic="VERTEX" source="#Cube-mesh-vertices" offset="0"/>
<input semantic="NORMAL" source="#Cube-mesh-normals" offset="1"/>
<p>0 0 2 0 3 0 7 1 5 1 4 1 4 2 1 2 0 2 5 3 2 3 1 3 2 4 7 4 3 4 0 5 7 5 4 5 0 6 1 6 2 6 7 7 6 7 5 7 4 8 5 8 1 8 5 9 6 9 2 9 2 10 6 10 7 10 0 11 3 11 7 11</p>
</triangles>
</mesh>
</geometry>
</library_geometries>
<library_controllers/>
<library_visual_scenes>
<visual_scene id="Scene" name="Scene">
<node id="Camera" name="Camera" type="NODE">
<matrix sid="transform">0.6859207 -0.3240135 0.6515582 7.481132 0.7276763 0.3054208 -0.6141704 -6.50764 0 0.8953956 0.4452714 5.343665 0 0 0 1</matrix>
<instance_camera url="#Camera-camera"/>
</node>
<node id="Lamp" name="Lamp" type="NODE">
<matrix sid="transform">-0.2908646 -0.7711008 0.5663932 4.076245 0.9551712 -0.1998834 0.2183912 1.005454 -0.05518906 0.6045247 0.7946723 5.903862 0 0 0 1</matrix>
<instance_light url="#Lamp-light"/>
</node>
<node id="Cube" name="Cube" type="NODE">
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
<instance_geometry url="#Cube-mesh" name="Cube">
<bind_material>
<technique_common>
<instance_material symbol="Material-material" target="#Material-material"/>
</technique_common>
</bind_material>
</instance_geometry>
</node>
</visual_scene>
</library_visual_scenes>
<scene>
<instance_visual_scene url="#Scene"/>
</scene>
</COLLADA>
16 changes: 16 additions & 0 deletions testdata/a.gml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<wfs:FeatureCollection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:wfs="http://www.opengis.net/wfs/2.0" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:LR="www.landregistry.gov.uk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" numberMatched="25697" numberReturned="25697" timeStamp="2019-04-07T09:37:45.328Z" xsi:schemaLocation="http://www.opengis.net/gml/3.2 http://192.168.4.121:8080/geoserver/schemas/gml/3.2.1/gml.xsd http://www.opengis.net/wfs/2.0 http://192.168.4.121:8080/geoserver/schemas/wfs/2.0/wfs.xsd www.landregistry.gov.uk http://192.168.4.121:8080/geoserver/LR/wfs?service=WFS&amp;version=2.0.0&amp;request=DescribeFeatureType&amp;typeName=LR%3APREDEFINED">
<gml:Polygon>
<gml:outerBoundaryIs>
<gml:LinearRing>
<gml:coordinates>0,0 100,0 100,100 0,100 0,0</gml:coordinates>
</gml:LinearRing>
</gml:outerBoundaryIs>
</gml:Polygon>
<gml:Point>
<gml:coordinates>100,200</gml:coordinates>
</gml:Point>
<gml:LineString>
<gml:coordinates>100,200 150,300</gml:coordinates>
</gml:LineString>
</wfs:FeatureCollection>
Loading

0 comments on commit 0ff9399

Please sign in to comment.