1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
|
/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2006 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors. All rights reserved.
*/
package org.pentaho.reporting.libraries.xmlns.parser;
import java.util.HashMap;
import org.pentaho.reporting.libraries.resourceloader.DependencyCollector;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
import org.pentaho.reporting.libraries.base.config.DefaultConfiguration;
import org.pentaho.reporting.libraries.base.util.FastStack;
import org.pentaho.reporting.libraries.base.util.DebugLog;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* A base root SAX handler.
*
* @author Peter Becker
* @author Thomas Morgner
*/
public class RootXmlReadHandler extends DefaultHandler
{
/**
* Storage for the parser configuration.
*/
private DefaultConfiguration parserConfiguration;
/**
* The DocumentLocator can be used to resolve the current parse position.
*/
private Locator documentLocator;
/**
* The current handlers.
*/
private FastStack currentHandlers;
/**
* The list of parent handlers.
*/
private FastStack outerScopes;
/**
* The root handler.
*/
private XmlReadHandler rootHandler;
/**
* The object registry.
*/
private HashMap objectRegistry;
/**
* A flag indicating whether this handler has initialized the root-element.
*/
private boolean rootHandlerInitialized;
/**
* The current comment handler used to receive xml comments.
*/
private CommentHandler commentHandler;
private DependencyCollector dependencyCollector;
private ResourceKey source;
private ResourceKey context;
private ResourceManager manager;
private FastStack namespaces;
private boolean firstCall;
/**
* Creates a new root-handler using the given versioning information and
* resource-manager.
*
* @param manager the resource manager that loaded this xml-file.
* @param source the source-key that idenfies from where the file was loaded.
* @param version the versioning information for the root-file.
*/
public RootXmlReadHandler(final ResourceManager manager,
final ResourceKey source,
final long version)
{
this(manager, source, source, version);
}
/**
* Creates a new root-handler using the given versioning information and
* resource-manager.
*
* @param manager the resource manager that loaded this xml-file.
* @param source the source-key that idenfies from where the file was loaded.
* @param context the key that should be used to resolve relative paths.
* @param version the versioning information for the root-file.
*/
public RootXmlReadHandler(final ResourceManager manager,
final ResourceKey source,
final ResourceKey context,
final long version)
{
if (manager == null)
{
throw new NullPointerException();
}
if (source == null)
{
throw new NullPointerException();
}
this.firstCall = true;
this.manager = manager;
this.source = source;
this.context = context;
this.dependencyCollector = new DependencyCollector(source, version);
this.objectRegistry = new HashMap();
this.parserConfiguration = new DefaultConfiguration();
this.commentHandler = new CommentHandler();
this.namespaces = new FastStack();
}
/**
* Returns the context key. This key may specity a base context for loading
* resources. (It behaves like the 'base-url' setting of HTML and allows to
* reference external resources as relative paths without being bound to the
* original location of the xml file.)
*
* @return the context.
*/
public ResourceKey getContext()
{
return context;
}
/**
* Returns the resource-manager that is used to load external resources.
*
* @return the resource-manager.
*/
public ResourceManager getResourceManager()
{
return manager;
}
/**
* Checks, whether this is the first call to the handler.
*
* @return true, if this is the first call, false otherwise.
*/
public boolean isFirstCall()
{
return firstCall;
}
/**
* Returns the source key. This key points to the file or stream that is
* currently parsed.
*
* @return the source key.
*/
public ResourceKey getSource()
{
return source;
}
/**
* Returns the current dependency collector for this parse-operation.
* The Collector allows to check compound-keys for changes.
*
* @return the dependency collector.
*/
public DependencyCollector getDependencyCollector()
{
return dependencyCollector;
}
/**
* Returns the comment handler that is used to collect comments.
*
* @return the comment handler.
*/
public CommentHandler getCommentHandler()
{
return this.commentHandler;
}
/**
* Returns the parser-configuration. This can be use to configure
* the parsing process.
*
* @return the parser's configuration.
*/
public DefaultConfiguration getParserConfiguration()
{
return parserConfiguration;
}
/**
* Receive an object for locating the origin of SAX document events.
* <p/>
* The documentLocator allows the application to determine the end position of
* any document-related event, even if the parser is not reporting an error.
* Typically, the application will use this information for reporting its own
* errors (such as character content that does not match an application's
* business rules). The information returned by the documentLocator is
* probably not sufficient for use with a search engine.
*
* @param locator the documentLocator.
*/
public void setDocumentLocator(final Locator locator)
{
this.documentLocator = locator;
}
/**
* Returns the current documentLocator.
*
* @return the documentLocator.
*/
public Locator getDocumentLocator()
{
return this.documentLocator;
}
/**
* Adds an object to the registry.
*
* @param key the key.
* @param value the object.
*/
public void setHelperObject(final String key, final Object value)
{
if (value == null)
{
this.objectRegistry.remove(key);
}
else
{
this.objectRegistry.put(key, value);
}
}
/**
* Returns an object from the registry.
*
* @param key the key.
* @return The object.
*/
public Object getHelperObject(final String key)
{
return this.objectRegistry.get(key);
}
/**
* Returns the array of all currently registered helper-objects. Helper
* objects are used as simple communication process between the various
* handler implementations.
*
* @return the helper object names.
*/
public String[] getHelperObjectNames()
{
return (String[]) this.objectRegistry.keySet().toArray
(new String[objectRegistry.size()]);
}
/**
* Sets the root SAX handler.
*
* @param handler the SAX handler.
*/
protected void setRootHandler(final XmlReadHandler handler)
{
if (handler == null)
{
throw new NullPointerException();
}
this.rootHandler = handler;
this.rootHandlerInitialized = false;
}
/**
* Returns the root SAX handler.
*
* @return the root SAX handler.
*/
protected XmlReadHandler getRootHandler()
{
return this.rootHandler;
}
/**
* Start a new handler stack and delegate to another handler.
*
* @param handler the handler.
* @param uri the namespace uri of the current tag.
* @param tagName the tag name.
* @param attrs the attributes.
* @throws SAXException if there is a problem with the parser.
*/
public void recurse(final XmlReadHandler handler,
final String uri,
final String tagName,
final Attributes attrs)
throws SAXException
{
if (handler == null)
{
throw new NullPointerException();
}
this.outerScopes.push(this.currentHandlers);
this.currentHandlers = new FastStack();
this.currentHandlers.push(handler);
handler.startElement(uri, tagName, attrs);
}
/**
* Delegate to another handler.
*
* @param handler the new handler.
* @param tagName the tag name.
* @param uri the namespace uri of the current tag.
* @param attrs the attributes.
* @throws SAXException if there is a problem with the parser.
*/
public void delegate(final XmlReadHandler handler,
final String uri,
final String tagName,
final Attributes attrs)
throws SAXException
{
if (handler == null)
{
throw new NullPointerException();
}
this.currentHandlers.push(handler);
handler.init(this, uri, tagName);
handler.startElement(uri, tagName, attrs);
}
/**
* Hand control back to the previous handler.
*
* @param tagName the tagname.
* @param uri the namespace uri of the current tag.
* @throws SAXException if there is a problem with the parser.
*/
public void unwind(final String uri, final String tagName)
throws SAXException
{
// remove current handler from stack ..
this.currentHandlers.pop();
if (this.currentHandlers.isEmpty() && !this.outerScopes.isEmpty())
{
// if empty, but "recurse" had been called, then restore the old handler stack ..
// but do not end the recursed element ..
this.currentHandlers = (FastStack) this.outerScopes.pop();
}
else if (!this.currentHandlers.isEmpty())
{
// if there are some handlers open, close them too (these handlers must be delegates)..
getCurrentHandler().endElement(uri, tagName);
}
}
/**
* Returns the current handler.
*
* @return The current handler.
*/
protected XmlReadHandler getCurrentHandler()
{
return (XmlReadHandler) this.currentHandlers.peek();
}
/**
* Starts processing a document.
*
* @throws SAXException not in this implementation.
*/
public void startDocument() throws SAXException
{
this.outerScopes = new FastStack();
this.currentHandlers = new FastStack();
if (rootHandler != null)
{
// When dealing with the multiplexing beast, we cant define a
// root handler unless we've seen the first element and all its
// namespace declarations ...
this.currentHandlers.push(this.rootHandler);
}
}
/**
* Starts processing an element.
*
* @param originalUri the URI.
* @param localName the local name.
* @param qName the qName.
* @param attributes the attributes.
* @throws SAXException if there is a parsing problem.
*/
public final void startElement(final String originalUri,
final String localName,
final String qName,
final Attributes attributes)
throws SAXException
{
// Check the default-namespace ..
if (firstCall)
{
firstCall = false;
interceptFirstStartElement(originalUri, localName, qName, attributes);
return;
}
final String defaultNamespace;
final String nsuri = attributes.getValue("xmlns");
if (nsuri != null)
{
defaultNamespace = nsuri;
}
else if (namespaces.isEmpty())
{
defaultNamespace = "";
}
else
{
defaultNamespace = (String) namespaces.peek();
}
pushDefaultNamespace(defaultNamespace);
final String uri;
if ((originalUri == null || "".equals(originalUri)) &&
defaultNamespace != null)
{
uri = defaultNamespace;
}
else
{
uri = originalUri;
}
if (rootHandlerInitialized == false)
{
rootHandler.init(this, uri, localName);
rootHandlerInitialized = true;
}
final XmlReadHandler currentHandler = getCurrentHandler();
currentHandler.startElement(uri, localName,
wrapAttributes(new FixNamespaceUriAttributes(uri, attributes)));
}
protected Attributes wrapAttributes(final Attributes attributes)
{
return attributes;
}
/**
* A helper call that allows to override the first call to the startElememt
* method. This allows the implementation of an multiplexing parser, which
* requires the information from the root-level elements.
*
* @param uri the namespace uri of the current tag.
* @param localName the unqualified tag-name.
* @param qName the qualified tag-name.
* @param attributes the attributes of the current element.
* @throws SAXException if something goes wrong.
*/
protected void interceptFirstStartElement(final String uri,
final String localName,
final String qName,
final Attributes attributes)
throws SAXException
{
startElement(uri, localName, qName, attributes);
}
/**
* Updates the current default namespace.
*
* @param nsuri the uri of the current namespace.
*/
protected final void pushDefaultNamespace(final String nsuri)
{
namespaces.push(nsuri);
}
/**
* Sets and configures the root handle for the given root-level element.
*
* @param handler the read handler for the root element.
* @param uri the uri of the root elements namespace.
* @param localName the local tagname of the root element.
* @param attributes the attributes of the root element.
* @throws SAXException if something goes wrong.
*/
protected void installRootHandler(final XmlReadHandler handler,
final String uri,
final String localName,
final Attributes attributes)
throws SAXException
{
if (handler == null)
{
throw new NullPointerException();
}
this.rootHandler = handler;
this.rootHandler.init(this, uri, localName);
this.currentHandlers.push(handler);
this.rootHandlerInitialized = true;
this.rootHandler.startElement(uri, localName, attributes);
}
/**
* Process character data.
*
* @param ch the character buffer.
* @param start the start index.
* @param length the length of the character data.
* @throws SAXException if there is a parsing error.
*/
public void characters(final char[] ch, final int start, final int length)
throws SAXException
{
try
{
getCurrentHandler().characters(ch, start, length);
}
catch (SAXException se)
{
throw se;
}
catch (Exception e)
{
throw new ParseException
("Failed at handling character data", e, getDocumentLocator());
}
}
/**
* Finish processing an element.
*
* @param originalUri the URI.
* @param localName the local name.
* @param qName the qName.
* @throws SAXException if there is a parsing error.
*/
public final void endElement(final String originalUri,
final String localName,
final String qName)
throws SAXException
{
final String defaultNamespace = (String) namespaces.pop();
final String uri;
if ((originalUri == null || "".equals(originalUri)) &&
defaultNamespace != null)
{
uri = defaultNamespace;
}
else
{
uri = originalUri;
}
final XmlReadHandler currentHandler = getCurrentHandler();
currentHandler.endElement(uri, localName);
}
/**
* Tries to return the parse-result of the selected root-handler.
*
* @return the parse-result.
* @throws SAXException if an error occurs.
*/
public Object getResult() throws SAXException
{
if (this.rootHandler != null)
{
return this.rootHandler.getObject();
}
return null;
}
}
|