opj_viewer removed the xml functions (for users without Xersus2), opj_viewer_xerces...
[openjpeg.git] / applications / jpip / opj_client / opj_viewer_xerces / src / JP2XMLparser.java
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 import org.w3c.dom.Attr;
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.NodeList;
35 import org.xml.sax.SAXParseException;
36 import org.xml.sax.ErrorHandler;
37 import org.apache.xerces.parsers.DOMParser;
38 import org.xml.sax.InputSource;
39 import java.io.*;
40 import java.lang.Integer;
41
42 public class JP2XMLparser
43 {
44     Document document;
45   
46     public static class ROIparams{
47         public String name = null;
48         public int x = 0;
49         public int y = 0;
50         public int w = 0;
51         public int h = 0;
52     }
53
54     public static class IRTparams{
55         public String refimg = null;
56         public double []mat = { 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
57     }
58
59     public JP2XMLparser( byte[] buf)
60     {
61         try{
62             InputSource source = new InputSource( new ByteArrayInputStream( buf));
63             DOMParser  parser = new DOMParser();
64             parser.setErrorHandler(new MyHandler());
65             parser.parse( source);
66             document = parser.getDocument();
67         }
68         catch (Exception e) {
69             e.printStackTrace();
70         }
71     }
72   
73     public ROIparams [] getROIparams()
74     {
75         ROIparams roi[];
76         NodeList elements = document.getElementsByTagName("roi");
77         int elementCount = elements.getLength();
78     
79         roi = new ROIparams [elementCount];
80
81         for( int i = 0; i < elementCount; i++) {
82             Element element = (Element)elements.item(i);
83       
84             roi[i] = new ROIparams();
85             roi[i].name = element.getAttribute( "name");
86             roi[i].x = Integer.parseInt( element.getAttribute( "x")) ;
87             roi[i].y = Integer.parseInt( element.getAttribute( "y")) ;
88             roi[i].w = Integer.parseInt( element.getAttribute( "w")) ;
89             roi[i].h = Integer.parseInt( element.getAttribute( "h")) ;
90         }
91         return roi;
92     }
93
94     public IRTparams getIRTparams()
95     {
96         IRTparams irt = new IRTparams();
97         NodeList elements = document.getElementsByTagName("irt");
98         int elementCount = elements.getLength();
99         
100         Element element = (Element)elements.item(0);
101         irt.refimg = element.getAttribute( "refimg");
102         for( int i=1; i<=9; i++)
103             irt.mat[i-1] = Double.parseDouble( element.getAttribute("m" + i));
104         
105         return irt;
106     }
107 }
108
109 class MyHandler implements ErrorHandler {
110     public void warning(SAXParseException e) {
111         System.out.println("Warning: line" + e.getLineNumber());
112         System.out.println(e.getMessage());
113     }
114     public void error(SAXParseException e) {
115         System.out.println("Error: line" + e.getLineNumber());
116         System.out.println(e.getMessage());
117     }
118     public void fatalError(SAXParseException e) {
119         System.out.println("Critical error: line" + e.getLineNumber());
120         System.out.println(e.getMessage());
121     }
122 }