8
8
import javaxt .geospatial .geometry .Point ;
9
9
import javaxt .geospatial .geometry .Points ;
10
10
import javaxt .geospatial .geometry .Polygon ;
11
- import org .w3c .dom .NodeList ;
12
- import org .w3c .dom .Node ;
13
-
14
- import javaxt .xml .DOM ;
11
+ import org .w3c .dom .*;
12
+ import javax .xml .parsers .DocumentBuilder ;
13
+ import javax .xml .parsers .DocumentBuilderFactory ;
15
14
16
15
//******************************************************************************
17
16
//** CoordinateParser Class - By Peter Borissow
@@ -60,22 +59,22 @@ private Geometry getGeometry(String Coordinates){
60
59
try {
61
60
62
61
//Convert Coordinates (String) into a DOM Document
63
- org .w3c .dom .Document doc = DOM .createDocument (Coordinates );
62
+ org .w3c .dom .Document doc = Parser .createDocument (Coordinates );
64
63
65
64
//Get Outer Node
66
- Node OuterNode = DOM .getOuterNode (doc );
65
+ Node OuterNode = Parser .getOuterNode (doc );
67
66
68
67
//Get Name of the First Node
69
68
String nodeName = getNodeName (OuterNode );
70
69
71
70
//Get the srsName Attribute (may return a value if gml)
72
- String srsName = DOM .getAttributeValue (OuterNode ,"srsName" );
71
+ String srsName = Parser .getAttributeValue (OuterNode ,"srsName" );
73
72
74
73
75
74
//Point
76
75
if (nodeName .equalsIgnoreCase ("Point" )){
77
- if (DOM .hasChildren (OuterNode )==false ){
78
- Coordinates = DOM .getNodeValue (OuterNode );
76
+ if (Parser .hasChildren (OuterNode )==false ){
77
+ Coordinates = Parser .getNodeValue (OuterNode );
79
78
return getGeometry (Coordinates .trim ());
80
79
81
80
}
@@ -87,8 +86,8 @@ private Geometry getGeometry(String Coordinates){
87
86
88
87
//Line
89
88
if (nodeName .equalsIgnoreCase ("Line" )){
90
- if (DOM .hasChildren (OuterNode )==false ){
91
- Coordinates = DOM .getNodeValue (OuterNode );
89
+ if (Parser .hasChildren (OuterNode )==false ){
90
+ Coordinates = Parser .getNodeValue (OuterNode );
92
91
return getGeometry (Coordinates .trim ());
93
92
}
94
93
else { //gml? is there such a thing a "line"
@@ -97,29 +96,29 @@ private Geometry getGeometry(String Coordinates){
97
96
98
97
//LineString
99
98
if (nodeName .equalsIgnoreCase ("LineString" )){
100
- if (DOM .hasChildren (OuterNode )==false ){
101
- Coordinates = DOM .getNodeValue (OuterNode );
99
+ if (Parser .hasChildren (OuterNode )==false ){
100
+ Coordinates = Parser .getNodeValue (OuterNode );
102
101
return getGeometry (Coordinates .trim ());
103
102
}
104
103
else { //gml
105
104
Point [] Points = getGMLCoordinates (OuterNode .getChildNodes ());
106
105
Line line = new Line (Points );
107
- line .setSRS (DOM .getAttributeValue (OuterNode ,"srsName" ));
106
+ line .setSRS (Parser .getAttributeValue (OuterNode ,"srsName" ));
108
107
return line ;
109
108
}
110
109
}
111
110
112
111
//Polygon
113
112
if (nodeName .equalsIgnoreCase ("Polygon" )){
114
- if (DOM .hasChildren (OuterNode )==false ){
115
- Coordinates = DOM .getNodeValue (OuterNode );
113
+ if (Parser .hasChildren (OuterNode )==false ){
114
+ Coordinates = Parser .getNodeValue (OuterNode );
116
115
return getGeometry (Coordinates .trim ());
117
116
}
118
117
else { //gml polygon
119
118
120
119
NodeList outerBoundary = OuterNode .getChildNodes ();
121
120
Polygon polygon = getGMLPolygon (outerBoundary );
122
- polygon .setSRS (DOM .getAttributeValue (OuterNode ,"srsName" ));
121
+ polygon .setSRS (Parser .getAttributeValue (OuterNode ,"srsName" ));
123
122
return polygon ;
124
123
125
124
@@ -130,8 +129,8 @@ private Geometry getGeometry(String Coordinates){
130
129
//Box/Envelope
131
130
if (nodeName .equalsIgnoreCase ("Box" ) || nodeName .equalsIgnoreCase ("Envelope" )){
132
131
Points Points = null ;
133
- if (DOM .hasChildren (OuterNode )==false ){ //georss box
134
- Coordinates = DOM .getNodeValue (OuterNode );
132
+ if (Parser .hasChildren (OuterNode )==false ){ //georss box
133
+ Coordinates = Parser .getNodeValue (OuterNode );
135
134
Points = getPoints (Coordinates );
136
135
}
137
136
else { //gml envelope or box
@@ -143,10 +142,10 @@ private Geometry getGeometry(String Coordinates){
143
142
Node node = PointMembers .item (i );
144
143
nodeName = getNodeName (node );
145
144
if (nodeName .equalsIgnoreCase ("lowerCorner" )){
146
- Points .addPoints (getPoints (DOM .getNodeValue (node )));
145
+ Points .addPoints (getPoints (Parser .getNodeValue (node )));
147
146
}
148
147
else if (nodeName .equalsIgnoreCase ("upperCorner" )){
149
- Points .addPoints (getPoints (DOM .getNodeValue (node )));
148
+ Points .addPoints (getPoints (Parser .getNodeValue (node )));
150
149
}
151
150
}
152
151
@@ -158,7 +157,7 @@ else if (nodeName.equalsIgnoreCase("upperCorner")){
158
157
if (Points !=null ) {
159
158
if (Points .getLength ()==2 ){
160
159
Box box = new Box (Points .getFirstPoint (), Points .getLastPoint ());
161
- box .setSRS (DOM .getAttributeValue (OuterNode ,"srsName" ));
160
+ box .setSRS (Parser .getAttributeValue (OuterNode ,"srsName" ));
162
161
return box ;
163
162
}
164
163
}
@@ -167,7 +166,7 @@ else if (nodeName.equalsIgnoreCase("upperCorner")){
167
166
168
167
//MultiPoint
169
168
if (nodeName .equalsIgnoreCase ("MultiPoint" )){
170
- if (DOM .hasChildren (OuterNode )==false ){
169
+ if (Parser .hasChildren (OuterNode )==false ){
171
170
//what to do?
172
171
}
173
172
else { //gml multipoint
@@ -189,7 +188,7 @@ else if (nodeName.equalsIgnoreCase("upperCorner")){
189
188
}
190
189
191
190
192
- MultiPoint .setSRS (DOM .getAttributeValue (OuterNode ,"srsName" ));
191
+ MultiPoint .setSRS (Parser .getAttributeValue (OuterNode ,"srsName" ));
193
192
return MultiPoint ;
194
193
195
194
}
@@ -198,7 +197,7 @@ else if (nodeName.equalsIgnoreCase("upperCorner")){
198
197
199
198
//MultiLine
200
199
if (nodeName .equalsIgnoreCase ("MultiLineString" )){
201
- if (DOM .hasChildren (OuterNode )==false ){
200
+ if (Parser .hasChildren (OuterNode )==false ){
202
201
//what to do?
203
202
}
204
203
else { //gml MultiLine
@@ -222,7 +221,7 @@ else if (nodeName.equalsIgnoreCase("upperCorner")){
222
221
}
223
222
}
224
223
225
- multiLine .setSRS (DOM .getAttributeValue (OuterNode ,"srsName" ));
224
+ multiLine .setSRS (Parser .getAttributeValue (OuterNode ,"srsName" ));
226
225
return multiLine ;
227
226
228
227
}
@@ -232,7 +231,7 @@ else if (nodeName.equalsIgnoreCase("upperCorner")){
232
231
233
232
//MultiPolygon
234
233
if (nodeName .equalsIgnoreCase ("MultiPolygon" )){
235
- if (DOM .hasChildren (OuterNode )==false ){
234
+ if (Parser .hasChildren (OuterNode )==false ){
236
235
//what to do?
237
236
}
238
237
else { //gml MultiPolygon
@@ -370,12 +369,12 @@ private Point[] getGMLCoordinates(org.w3c.dom.NodeList ChildNodes){
370
369
//Parse Coordinate List
371
370
if (NodeName .equalsIgnoreCase ("coordinates" )){
372
371
373
- String cs = DOM .getAttributeValue (Node , "cs" );
374
- String ts = DOM .getAttributeValue (Node , "ts" );
372
+ String cs = Parser .getAttributeValue (Node , "cs" );
373
+ String ts = Parser .getAttributeValue (Node , "ts" );
375
374
if (cs .length ()>0 ) CoordinateSeparator = cs ;
376
375
if (ts .length ()>0 ) TupleSeparator = ts ;
377
376
378
- String Coordinates = DOM .getNodeValue (Node ).trim ();
377
+ String Coordinates = Parser .getNodeValue (Node ).trim ();
379
378
String [] points = Coordinates .split (TupleSeparator );
380
379
381
380
for (int j =0 ; j <points .length ; j ++){
@@ -394,7 +393,7 @@ private Point[] getGMLCoordinates(org.w3c.dom.NodeList ChildNodes){
394
393
395
394
//Parse Position List
396
395
else if (NodeName .equalsIgnoreCase ("pos" ) || NodeName .equalsIgnoreCase ("posList" )){
397
- String Coordinates = DOM .getNodeValue (Node ).trim ();
396
+ String Coordinates = Parser .getNodeValue (Node ).trim ();
398
397
//Points.addPoints(Coordinates);
399
398
Points .addPoints (getPoints (Coordinates ));
400
399
}
@@ -410,10 +409,10 @@ else if (NodeName.equalsIgnoreCase("coord")){
410
409
Node Coord = Coords .item (j );
411
410
String CoordName = getNodeName (Coord );
412
411
if (CoordName .equalsIgnoreCase ("x" )){
413
- x = DOM .getNodeValue (Coord ).trim ();
412
+ x = Parser .getNodeValue (Coord ).trim ();
414
413
}
415
414
if (CoordName .equalsIgnoreCase ("y" )){
416
- y = DOM .getNodeValue (Coord ).trim ();
415
+ y = Parser .getNodeValue (Coord ).trim ();
417
416
}
418
417
}
419
418
@@ -591,5 +590,192 @@ private String[] split(String str, String ch){
591
590
return str .split (ch );
592
591
}
593
592
594
-
593
+
594
+ // <editor-fold defaultstate="collapsed" desc="XML parsing methods copied from javaxt.xml.DOM">
595
+
596
+ //**************************************************************************
597
+ //** createDocument
598
+ //**************************************************************************
599
+ /** Used to create a DOM document from a String. */
600
+
601
+ private static Document createDocument (String xml ){
602
+ xml = xml .trim ();
603
+ String encoding = "UTF-8" ;
604
+
605
+ try {
606
+ String xmlHeader = xml .substring (xml .indexOf ("<?" ), xml .indexOf ("?>" ));
607
+ if (xmlHeader .contains (" encoding" )){
608
+ encoding = xmlHeader .substring (xmlHeader .indexOf (" encoding" )+" encoding" .length ());
609
+ encoding = encoding .substring (encoding .indexOf ("=" )+1 );
610
+ while (encoding .substring (0 , 1 ).equals (" " )){
611
+ encoding = encoding .substring (1 );
612
+ }
613
+
614
+ if (encoding .substring (0 , 1 ).equals ("\" " )){
615
+ encoding = encoding .substring (1 );
616
+ encoding = encoding .substring (0 , encoding .indexOf ("\" " )).trim ();
617
+ }
618
+ else {
619
+ encoding = encoding .substring (0 , encoding .indexOf (" " )).trim ();
620
+ }
621
+
622
+ }
623
+ }
624
+ catch (Exception e ){}
625
+
626
+
627
+
628
+ java .io .InputStream is ;
629
+ try {
630
+ is = (new java .io .ByteArrayInputStream (xml .getBytes (encoding )));
631
+ }
632
+ catch (Exception e ){
633
+ is = (new java .io .ByteArrayInputStream (xml .getBytes ()));
634
+ }
635
+
636
+ try {
637
+ DocumentBuilderFactory builderFactory = DocumentBuilderFactory .newInstance ();
638
+ DocumentBuilder builder = builderFactory .newDocumentBuilder ();
639
+ return builder .parse (is );
640
+ }
641
+ catch (Exception e ){
642
+ return null ;
643
+ }
644
+ }
645
+
646
+
647
+ //**************************************************************************
648
+ //** getOuterNode
649
+ //**************************************************************************
650
+ /** Returns the outer node for a given xml document.
651
+ * @param xml A org.w3c.dom.Document
652
+ */
653
+ private static Node getOuterNode (Document xml ){
654
+ if (xml ==null ) return null ;
655
+ NodeList OuterNodes = xml .getChildNodes ();
656
+ for (int i =0 ; i <OuterNodes .getLength (); i ++ ) {
657
+ if (OuterNodes .item (i ).getNodeType () == 1 ){
658
+ return OuterNodes .item (i );
659
+ }
660
+ }
661
+ return null ;
662
+ }
663
+
664
+ //**************************************************************************
665
+ //** getAttributeValue
666
+ //**************************************************************************
667
+ /** Used to return the value of a given node attribute. The search is case
668
+ * insensitive. If no match is found, returns an empty string.
669
+ */
670
+ private static String getAttributeValue (Node node , String attrName ){
671
+
672
+ NamedNodeMap attrCollection = node .getAttributes ();
673
+ if (attrCollection !=null ){
674
+ for (int i =0 ; i < attrCollection .getLength (); i ++ ) {
675
+ Node attr = attrCollection .item (i );
676
+ if (attr .getNodeName ().equalsIgnoreCase (attrName )) {
677
+ return attr .getNodeValue ();
678
+ }
679
+ }
680
+ }
681
+ return "" ;
682
+ }
683
+
684
+
685
+ //**************************************************************************
686
+ //** getNodeValue
687
+ //**************************************************************************
688
+ /** Returns the value of a given node as text.
689
+ */
690
+ private static String getNodeValue (Node node ){
691
+
692
+ String nodeValue = "" ;
693
+
694
+ if (hasChildren (node )) {
695
+
696
+ StringBuffer xmlTree = new StringBuffer ();
697
+ traverse (node , xmlTree );
698
+ nodeValue = xmlTree .toString ();
699
+
700
+ }
701
+ else {
702
+ nodeValue = node .getTextContent ();
703
+ }
704
+
705
+ if (nodeValue == null ){
706
+ return "" ;
707
+ }
708
+ else {
709
+ return nodeValue ;
710
+ }
711
+ }
712
+
713
+ private static void traverse (Node tree , StringBuffer xmlTree ){
714
+ if (tree .getNodeType ()==1 ){
715
+ String Attributes = getAttributes (tree );
716
+ xmlTree .append ("<" + tree .getNodeName () + Attributes + ">" );
717
+ if (hasChildren (tree )) {
718
+
719
+ NodeList xmlNodeList = tree .getChildNodes ();
720
+ for (int i =0 ; i <xmlNodeList .getLength (); i ++){
721
+ traverse (xmlNodeList .item (i ), xmlTree );
722
+ }
723
+
724
+ }
725
+ else {
726
+
727
+ String nodeValue = tree .getTextContent ();
728
+ if (nodeValue == null ){
729
+ nodeValue = "" ;
730
+ }
731
+
732
+ xmlTree .append (nodeValue );
733
+ }
734
+
735
+ xmlTree .append ("</" + tree .getNodeName () + ">" );
736
+ }
737
+ }
738
+
739
+ //**************************************************************************
740
+ //** getAttributes
741
+ //**************************************************************************
742
+ /** Used to retrieve all of the attributes for a given node. */
743
+
744
+ private static String getAttributes (Node node ){
745
+ if (node ==null ) return "" ;
746
+ NamedNodeMap attr = node .getAttributes ();
747
+ String Attributes = "" ;
748
+ if (attr !=null ){
749
+ for (int j =0 ; j <attr .getLength (); j ++){
750
+ String name = attr .item (j ).getNodeName ();
751
+ String value = attr .item (j ).getTextContent ();
752
+ if (value ==null ) value = attr .item (j ).getNodeValue ();
753
+ if (value ==null ) value = "" ;
754
+ //System.out.println(name + "=" + attr.item(j).getNodeValue());
755
+ Attributes += " " + name + "=\" " + value + "\" " ;
756
+ }
757
+ }
758
+ return Attributes ;
759
+ }
760
+
761
+ //**************************************************************************
762
+ //** hasChildren
763
+ //**************************************************************************
764
+ /** Used to determine whether a given node has any children. Differs from the
765
+ * native DOM implementation in that this function only considers child
766
+ * nodes that have a node type value equal to 1.
767
+ */
768
+ private static boolean hasChildren (Node node ){
769
+
770
+ NodeList nodeList = node .getChildNodes ();
771
+ for (int i =0 ; i <nodeList .getLength (); i ++ ) {
772
+ if (nodeList .item (i ).getNodeType ()==1 ){
773
+ return true ;
774
+ }
775
+ }
776
+ return false ;
777
+ }
778
+
779
+
780
+ // </editor-fold>
595
781
}
0 commit comments