comparison software/mpdl-services/mpiwg-mpdl-xml/src/de/mpg/mpiwg/berlin/mpdl/xml/tmp/GetFragmentContentHandler.java @ 18:dc5e9fcb3fdc

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 09 Nov 2011 15:27:46 +0100
parents
children
comparison
equal deleted inserted replaced
17:7e883ce72fec 18:dc5e9fcb3fdc
1 package de.mpg.mpiwg.berlin.mpdl.xml.tmp;
2
3 import org.xml.sax.*;
4
5 public class GetFragmentContentHandler implements ContentHandler {
6 private String ms1Name;
7 private String ms2Name;
8 private int ms1Pos;
9 private int ms2Pos;
10
11 private int msCounter = 1;
12 private boolean startFragment = false;
13 private String fragment = "";
14
15 public GetFragmentContentHandler(String ms1Name, int ms1Pos, String ms2Name, int ms2Pos) {
16 this.ms1Name = ms1Name;
17 this.ms2Name = ms2Name;
18 this.ms1Pos = ms1Pos;
19 this.ms2Pos = ms2Pos;
20 }
21
22 public void startDocument() throws SAXException {
23 fragment += "<?xml version=\"1.0\"?>\n";
24 }
25
26 public void endDocument() throws SAXException {
27 }
28
29 public void characters(char[] c, int start, int length) throws SAXException {
30 if (startFragment) {
31 char[] cCopy = new char[length];
32 System.arraycopy(c, start, cCopy, 0, length);
33 String charactersStr = String.valueOf(cCopy);
34 if (charactersStr != null && ! (charactersStr.trim().equals(""))) {
35 fragment += charactersStr;
36 }
37 }
38 }
39
40 public void ignorableWhitespace(char[] c, int start, int length) throws SAXException {
41 }
42
43 public void processingInstruction(String target, String data) throws SAXException {
44 }
45
46 public void setDocumentLocator(org.xml.sax.Locator arg1) {
47 }
48
49 public void endPrefixMapping(String prefix) throws SAXException {
50 }
51
52 public void skippedEntity(String name) throws SAXException {
53 }
54
55 public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException {
56 if (localName.equals(ms1Name)) {
57 if (msCounter == ms1Pos) {
58 startFragment = true;
59 }
60 if (msCounter == ms2Pos) {
61 startFragment = false;
62 throw new SAXException("end");
63 }
64 msCounter++;
65 }
66 if (startFragment) {
67 fragment += "<" + name;
68 int attrSize = attrs.getLength();
69 String attrString = "";
70 for (int i=0; i<attrSize; i++) {
71 String attrQName = attrs.getQName(i);
72 String attrValue = attrs.getValue(i);
73 // attrValue = StringUtilEscapeChars.forXML(attrValue);
74 attrString = attrString + " " + attrQName + "=\"" + attrValue + "\"";
75 }
76 fragment += attrString + ">";
77 }
78 }
79
80 public void endElement(String uri, String localName, String name) throws SAXException {
81 if (startFragment) {
82 fragment += "</" + name + ">";
83 }
84 }
85
86 public void startPrefixMapping(String prefix, String uri) throws SAXException {
87 }
88
89 public String getFragment() {
90 return fragment;
91 }
92
93 }