Project

General

Profile

1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
5
 * 
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common Development
8
 * and Distribution License("CDDL") (collectively, the "License").  You
9
 * may not use this file except in compliance with the License. You can obtain
10
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
11
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
12
 * language governing permissions and limitations under the License.
13
 * 
14
 * When distributing the software, include this License Header Notice in each
15
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
16
 * Sun designates this particular file as subject to the "Classpath" exception
17
 * as provided by Sun in the GPL Version 2 section of the License file that
18
 * accompanied this code.  If applicable, add the following below the License
19
 * Header, with the fields enclosed by brackets [] replaced by your own
20
 * identifying information: "Portions Copyrighted [year]
21
 * [name of copyright owner]"
22
 * 
23
 * Contributor(s):
24
 * 
25
 * If you wish your version of this file to be governed by only the CDDL or
26
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
27
 * elects to include this software in this distribution under the [CDDL or GPL
28
 * Version 2] license."  If you don't indicate a single choice of license, a
29
 * recipient has the option to distribute your version of this file under
30
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
31
 * its licensees as provided above.  However, if you add GPL Version 2 code
32
 * and therefore, elected the GPL Version 2 license, then the option applies
33
 * only if the new code is made subject to such option by the copyright
34
 * holder.
35
 */
36

    
37
package eu.dnetlib.data.search.resources;
38

    
39
import javax.xml.namespace.QName;
40
import javax.xml.stream.XMLInputFactory;
41
import javax.xml.stream.XMLStreamReader;
42
import javax.xml.stream.events.XMLEvent;
43
import java.io.FileInputStream;
44

    
45
/**
46
 * MyStreamFilter sample is used to demonstrates the use
47
 * of STAX stream filter api's. This filter accepts only 
48
 * StartElement and EndElement events and filters out rest of
49
 * the events.
50
 */
51

    
52
public class MAin implements javax.xml.stream.StreamFilter{
53

    
54
    private static String filename = null;
55

    
56

    
57
    private static void printUsage() {
58
        System.out.println("usage: java -Djava.endorsed.dirs=<jaxp dist/lib directory> stax.filter.MyStreamFilter -f <xmlfile>");
59
    }
60

    
61
    /**
62
     *
63
     * @param args
64
     * @throws Exception
65
     */
66
    public static void main(String[] args) throws Exception {
67

    
68

    
69
        try {
70
            if(args[0].equals("-f")){
71
                filename = args[1];
72
            }
73
            else{
74
                printUsage() ;
75
            }
76
        } catch (ArrayIndexOutOfBoundsException aioobe){
77
            printUsage();
78
            System.exit(0);
79
        } catch (Exception ex){
80
            printUsage();
81
            ex.printStackTrace() ;
82
        }
83

    
84

    
85
        XMLInputFactory xmlif = null ;
86
        try{
87
            xmlif = XMLInputFactory.newInstance();
88
            xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,Boolean.TRUE);
89
            xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,Boolean.FALSE);
90
            xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE , Boolean.TRUE);
91
            xmlif.setProperty(XMLInputFactory.IS_COALESCING , Boolean.TRUE);
92
        }catch(Exception ex){
93
            ex.printStackTrace();
94
        }
95
        System.out.println("FACTORY: " + xmlif);
96
        System.out.println("filename = "+ filename);
97

    
98

    
99

    
100
        try{
101

    
102
            FileInputStream fis = new FileInputStream(filename);
103

    
104
            XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MAin());
105

    
106
            int eventType = xmlr.getEventType();
107
            printEventType(eventType);
108
            while(xmlr.hasNext()){
109
                eventType = xmlr.next();
110
                printEventType(eventType);
111
                printName(xmlr,eventType);
112
                printText(xmlr);
113
                if(xmlr.isStartElement()){
114
                    printAttributes(xmlr);
115
                }
116
                printPIData(xmlr);
117
                System.out.println("-----------------------------");
118
            }
119

    
120
        }catch(Exception ex){
121
            ex.printStackTrace();
122
        }
123

    
124
    }
125

    
126
    /**
127
     * @param eventType
128
     * @return
129
     */
130
    public final static String getEventTypeString(int eventType) {
131
        switch (eventType){
132
            case XMLEvent.START_ELEMENT:
133
                return "START_ELEMENT";
134
            case XMLEvent.END_ELEMENT:
135
                return "END_ELEMENT";
136
            case XMLEvent.PROCESSING_INSTRUCTION:
137
                return "PROCESSING_INSTRUCTION";
138
            case XMLEvent.CHARACTERS:
139
                return "CHARACTERS";
140
            case XMLEvent.COMMENT:
141
                return "COMMENT";
142
            case XMLEvent.START_DOCUMENT:
143
                return "START_DOCUMENT";
144
            case XMLEvent.END_DOCUMENT:
145
                return "END_DOCUMENT";
146
            case XMLEvent.ENTITY_REFERENCE:
147
                return "ENTITY_REFERENCE";
148
            case XMLEvent.ATTRIBUTE:
149
                return "ATTRIBUTE";
150
            case XMLEvent.DTD:
151
                return "DTD";
152
            case XMLEvent.CDATA:
153
                return "CDATA";
154
        }
155
        return "UNKNOWN_EVENT_TYPE";
156
    }
157

    
158
    private static void printEventType(int eventType) {
159
        System.out.print("EVENT TYPE("+eventType+"):");
160
        System.out.println(getEventTypeString(eventType));
161
    }
162

    
163
    private static void printName(XMLStreamReader xmlr,int eventType){
164
        if(xmlr.hasName()){
165
            System.out.println("HAS NAME: " + xmlr.getLocalName());
166
        } else {
167
            System.out.println("HAS NO NAME");
168
        }
169
    }
170

    
171
    private static void printText(XMLStreamReader xmlr){
172
        if(xmlr.hasText()){
173
            System.out.println("HAS TEXT: " + xmlr.getText());
174
        } else {
175
            System.out.println("HAS NO TEXT");
176
        }
177
    }
178

    
179
    private static void printPIData(XMLStreamReader xmlr){
180
        if (xmlr.getEventType() == XMLEvent.PROCESSING_INSTRUCTION){
181
            System.out.println(" PI target = " + xmlr.getPITarget() ) ;
182
            System.out.println(" PI Data = " + xmlr.getPIData() ) ;
183
        }
184
    }
185

    
186
    private static void printAttributes(XMLStreamReader xmlr){
187
        if(xmlr.getAttributeCount() > 0){
188
            System.out.println("\nHAS ATTRIBUTES: ");
189
            int count = xmlr.getAttributeCount() ;
190
            for(int i = 0 ; i < count ; i++) {
191

    
192
                QName name = xmlr.getAttributeName(i) ;
193
                String namespace = xmlr.getAttributeNamespace(i) ;
194
                String  type = xmlr.getAttributeType(i) ;
195
                String prefix = xmlr.getAttributePrefix(i) ;
196
                String value = xmlr.getAttributeValue(i) ;
197

    
198
                System.out.println("ATTRIBUTE-PREFIX: " + prefix );
199
                System.out.println("ATTRIBUTE-NAMESP: " + namespace );
200
                System.out.println("ATTRIBUTE-NAME:   " + name.toString() );
201
                System.out.println("ATTRIBUTE-VALUE:  " + value );
202
                System.out.println("ATTRIBUTE-TYPE:  " + type );
203
                System.out.println();
204

    
205
            }
206

    
207
        } else {
208
            System.out.println("HAS NO ATTRIBUTES");
209
        }
210
    }
211

    
212
    /**
213
     * Accept only StartElement and EndElement events,Filters out rest of the events.
214
     * @param reader
215
     * @return
216
     */
217
    public boolean accept(XMLStreamReader reader) {
218
        if(!reader.isStartElement() && !reader.isEndElement())
219
            return false;
220
        else
221
            return true;
222
    }
223
}
(2-2/7)