Mercurial > hg > mpdl-group
comparison software/mpdl-services-new/mpiwg-mpdl-cms/src/de/mpg/mpiwg/berlin/mpdl/cms/transform/GetFragmentsContentHandler.java @ 25:e9fe3186670c default tip
letzter Stand eingecheckt
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 21 May 2013 10:19:32 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
23:e845310098ba | 25:e9fe3186670c |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.cms.transform; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.Hashtable; | |
5 | |
6 import org.xml.sax.*; | |
7 | |
8 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
9 import de.mpg.mpiwg.berlin.mpdl.util.StringUtils; | |
10 | |
11 public class GetFragmentsContentHandler implements ContentHandler { | |
12 private String xmlnsString = ""; | |
13 private int currentMilestonePosition = 0; | |
14 private Element currentElement; | |
15 private Element currentMilestoneElement; | |
16 private ArrayList<Element> elementQueue; // queue of opened elements (before they were closed): to connect them to a parent hierarchy | |
17 private Hashtable<Integer, StringBuilder> resultFragments = new Hashtable<Integer, StringBuilder>(); | |
18 private String milestoneElemenName = "pb"; // default is pb | |
19 | |
20 public GetFragmentsContentHandler() throws ApplicationException { | |
21 } | |
22 | |
23 public GetFragmentsContentHandler(String milestoneElemenName) throws ApplicationException { | |
24 this.milestoneElemenName = milestoneElemenName; | |
25 } | |
26 | |
27 public Hashtable<Integer, StringBuilder> getResultPages() { | |
28 return resultFragments; | |
29 } | |
30 | |
31 public int getMilestoneCount() { | |
32 return currentMilestonePosition; | |
33 } | |
34 | |
35 public void startDocument() throws SAXException { | |
36 } | |
37 | |
38 public void endDocument() throws SAXException { | |
39 // write the closePath after the last minus 1 milestone element (the closing path after the last milestone element is automatically written by the normal closing tags) | |
40 if (currentMilestoneElement != null) { | |
41 String msClosePath = currentMilestoneElement.getClosePath(); | |
42 write(msClosePath, currentMilestoneElement.milestonePosition - 1); | |
43 } | |
44 resultFragments.remove(new Integer(0)); // this fragment is filled but should not | |
45 } | |
46 | |
47 public void characters(char[] c, int start, int length) throws SAXException { | |
48 char[] cCopy = new char[length]; | |
49 System.arraycopy(c, start, cCopy, 0, length); | |
50 String charactersStr = String.valueOf(cCopy); | |
51 if (charactersStr != null && ! charactersStr.equals("")) { | |
52 if (currentMilestonePosition > 0) { | |
53 charactersStr = StringUtils.deresolveXmlEntities(charactersStr); | |
54 write(charactersStr); | |
55 } | |
56 } | |
57 } | |
58 | |
59 public void ignorableWhitespace(char[] c, int start, int length) throws SAXException { | |
60 } | |
61 | |
62 public void processingInstruction(String target, String data) throws SAXException { | |
63 } | |
64 | |
65 public void setDocumentLocator(Locator locator) { | |
66 } | |
67 | |
68 public void startPrefixMapping(String prefix, String uri) throws SAXException { | |
69 if (prefix != null && prefix.equals("")) | |
70 xmlnsString += "xmlns" + "=\"" + uri + "\" "; | |
71 else | |
72 xmlnsString += "xmlns:" + prefix + "=\"" + uri + "\" "; | |
73 } | |
74 | |
75 public void endPrefixMapping(String prefix) throws SAXException { | |
76 } | |
77 | |
78 public void skippedEntity(String name) throws SAXException { | |
79 } | |
80 | |
81 public void startElement(String uri, String localName, String name, Attributes attrs) throws SAXException { | |
82 if (elementQueue == null) | |
83 elementQueue = new ArrayList<Element>(); | |
84 Element newElement = new Element(name); | |
85 if (currentElement != null) { | |
86 newElement.parent = currentElement; | |
87 } | |
88 currentElement = newElement; | |
89 if (localName != null && localName.equals(milestoneElemenName)) { | |
90 currentMilestonePosition++; | |
91 if (currentMilestoneElement != null) { | |
92 String msClosePath = currentMilestoneElement.getClosePath(); | |
93 write(msClosePath, currentMilestoneElement.milestonePosition - 1); | |
94 } | |
95 currentMilestoneElement = currentElement; | |
96 currentMilestoneElement.milestonePosition = currentMilestonePosition; | |
97 String msOpenPath = currentMilestoneElement.getOpenPath(); | |
98 write(msOpenPath); | |
99 } | |
100 int attrSize = attrs.getLength(); | |
101 String attrString = ""; | |
102 for (int i=0; i<attrSize; i++) { | |
103 String attrQName = attrs.getQName(i); | |
104 String attrValue = attrs.getValue(i); | |
105 attrValue = StringUtils.forXML(attrValue); | |
106 attrString = attrString + " " + attrQName + "=\"" + attrValue + "\""; | |
107 } | |
108 if (attrString != null && ! attrString.isEmpty()) { | |
109 attrString = attrString.trim(); | |
110 currentElement.attrString = attrString; | |
111 } | |
112 if (xmlnsString != null && ! xmlnsString.isEmpty()) { | |
113 xmlnsString = xmlnsString.trim(); | |
114 currentElement.xmlnsString = xmlnsString; | |
115 } | |
116 if (currentMilestonePosition > 0) { | |
117 write("<" + name); | |
118 if (xmlnsString != null && ! xmlnsString.isEmpty()) | |
119 write(" " + xmlnsString); | |
120 if (attrString != null && ! attrString.isEmpty()) | |
121 write(" " + attrString); | |
122 write(">"); | |
123 } | |
124 xmlnsString = ""; | |
125 elementQueue.add(currentElement); | |
126 } | |
127 | |
128 public void endElement(String uri, String localName, String name) throws SAXException { | |
129 if (currentMilestonePosition > 0) { | |
130 write("</" + name + ">"); | |
131 } | |
132 if (elementQueue != null && elementQueue.size() > 0) { | |
133 int lastIndex = elementQueue.size() - 1; | |
134 elementQueue.remove(lastIndex); | |
135 } | |
136 if (elementQueue != null && elementQueue.size() > 0) { | |
137 int lastIndex = elementQueue.size() - 1; | |
138 currentElement = elementQueue.get(lastIndex); | |
139 } else { | |
140 currentElement = null; | |
141 } | |
142 } | |
143 | |
144 private void write(String outStr) throws SAXException { | |
145 StringBuilder resultFragment = resultFragments.get(new Integer(currentMilestonePosition)); | |
146 if (resultFragment == null) { | |
147 resultFragment = new StringBuilder(); | |
148 resultFragments.put(new Integer(currentMilestonePosition), resultFragment); | |
149 } | |
150 resultFragment.append(outStr); | |
151 } | |
152 | |
153 private void write(String outStr, int milestoneNumber) throws SAXException { | |
154 StringBuilder resultFragment = resultFragments.get(new Integer(milestoneNumber)); | |
155 if (resultFragment == null) { | |
156 resultFragment = new StringBuilder(); | |
157 resultFragments.put(new Integer(milestoneNumber), resultFragment); | |
158 } | |
159 resultFragment.append(outStr); | |
160 } | |
161 | |
162 public class Element { | |
163 public String name; | |
164 private String xmlnsString; | |
165 private String attrString; | |
166 private int milestonePosition; | |
167 private Element parent; | |
168 | |
169 private Element(String name) { | |
170 this.name = name; | |
171 } | |
172 | |
173 private String getOpenPath() { | |
174 StringBuilder ancestorsStrBuilder = new StringBuilder(); | |
175 if (parent != null) { | |
176 ancestorsStrBuilder.append(parent.getOpenPath()); | |
177 ancestorsStrBuilder.append("<"); | |
178 ancestorsStrBuilder.append(parent.name); | |
179 if (parent.xmlnsString != null && ! parent.xmlnsString.isEmpty()) { | |
180 ancestorsStrBuilder.append(" "); | |
181 ancestorsStrBuilder.append(parent.xmlnsString); | |
182 } | |
183 if (parent.attrString != null && ! parent.attrString.isEmpty()) { | |
184 ancestorsStrBuilder.append(" " + parent.attrString); | |
185 } | |
186 ancestorsStrBuilder.append(">"); | |
187 } | |
188 return ancestorsStrBuilder.toString(); | |
189 } | |
190 | |
191 private String getClosePath() { | |
192 StringBuilder ancestorsStrBuilder = new StringBuilder(); | |
193 if (parent != null) { | |
194 ancestorsStrBuilder.append("</"); | |
195 ancestorsStrBuilder.append(parent.name); | |
196 ancestorsStrBuilder.append(">"); | |
197 ancestorsStrBuilder.append(parent.getClosePath()); | |
198 } | |
199 return ancestorsStrBuilder.toString(); | |
200 } | |
201 | |
202 } | |
203 } |