0N/A/*
2362N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage com.sun.beans.decoder;
0N/A
0N/Aimport com.sun.beans.finder.ClassFinder;
0N/A
0N/Aimport java.beans.ExceptionListener;
0N/A
0N/Aimport java.io.IOException;
0N/A
0N/Aimport java.lang.ref.Reference;
0N/Aimport java.lang.ref.WeakReference;
0N/A
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.List;
0N/Aimport java.util.Map;
0N/Aimport java.security.AccessControlContext;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/A
0N/Aimport javax.xml.parsers.ParserConfigurationException;
0N/Aimport javax.xml.parsers.SAXParserFactory;
0N/A
0N/Aimport org.xml.sax.Attributes;
0N/Aimport org.xml.sax.InputSource;
0N/Aimport org.xml.sax.SAXException;
0N/Aimport org.xml.sax.helpers.DefaultHandler;
0N/A
0N/Aimport sun.misc.SharedSecrets;
0N/A
0N/A/**
0N/A * The main class to parse JavaBeans XML archive.
0N/A *
0N/A * @since 1.7
0N/A *
0N/A * @author Sergey A. Malenkov
0N/A *
0N/A * @see ElementHandler
0N/A */
0N/Apublic final class DocumentHandler extends DefaultHandler {
0N/A private final AccessControlContext acc = AccessController.getContext();
0N/A private final Map<String, Class<? extends ElementHandler>> handlers = new HashMap<>();
0N/A private final Map<String, Object> environment = new HashMap<>();
0N/A private final List<Object> objects = new ArrayList<>();
0N/A
0N/A private Reference<ClassLoader> loader;
0N/A private ExceptionListener listener;
0N/A private Object owner;
0N/A
0N/A private ElementHandler handler;
0N/A
0N/A /**
0N/A * Creates new instance of document handler.
0N/A */
0N/A public DocumentHandler() {
0N/A setElementHandler("java", JavaElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("null", NullElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("array", ArrayElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("class", ClassElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("string", StringElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("object", ObjectElementHandler.class); // NON-NLS: the element name
0N/A
0N/A setElementHandler("void", VoidElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("char", CharElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("byte", ByteElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("short", ShortElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("int", IntElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("long", LongElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("float", FloatElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("double", DoubleElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("boolean", BooleanElementHandler.class); // NON-NLS: the element name
0N/A
0N/A // some handlers for new elements
0N/A setElementHandler("new", NewElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("var", VarElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("true", TrueElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("false", FalseElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("field", FieldElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("method", MethodElementHandler.class); // NON-NLS: the element name
0N/A setElementHandler("property", PropertyElementHandler.class); // NON-NLS: the element name
0N/A }
0N/A
0N/A /**
0N/A * Returns the class loader used to instantiate objects.
0N/A * If the class loader has not been explicitly set
0N/A * then {@code null} is returned.
0N/A *
0N/A * @return the class loader used to instantiate objects
0N/A */
0N/A public ClassLoader getClassLoader() {
0N/A return (this.loader != null)
0N/A ? this.loader.get()
0N/A : null;
0N/A }
0N/A
0N/A /**
0N/A * Sets the class loader used to instantiate objects.
0N/A * If the class loader is not set
0N/A * then default class loader will be used.
0N/A *
0N/A * @param loader a classloader to use
0N/A */
0N/A public void setClassLoader(ClassLoader loader) {
this.loader = new WeakReference<ClassLoader>(loader);
}
/**
* Returns the exception listener for parsing.
* The exception listener is notified
* when handler catches recoverable exceptions.
* If the exception listener has not been explicitly set
* then default exception listener is returned.
*
* @return the exception listener for parsing
*/
public ExceptionListener getExceptionListener() {
return this.listener;
}
/**
* Sets the exception listener for parsing.
* The exception listener is notified
* when handler catches recoverable exceptions.
*
* @param listener the exception listener for parsing
*/
public void setExceptionListener(ExceptionListener listener) {
this.listener = listener;
}
/**
* Returns the owner of this document handler.
*
* @return the owner of this document handler
*/
public Object getOwner() {
return this.owner;
}
/**
* Sets the owner of this document handler.
*
* @param owner the owner of this document handler
*/
public void setOwner(Object owner) {
this.owner = owner;
}
/**
* Returns the handler for the element with specified name.
*
* @param name the name of the element
* @return the corresponding element handler
*/
public Class<? extends ElementHandler> getElementHandler(String name) {
Class<? extends ElementHandler> type = this.handlers.get(name);
if (type == null) {
throw new IllegalArgumentException("Unsupported element: " + name);
}
return type;
}
/**
* Sets the handler for the element with specified name.
*
* @param name the name of the element
* @param handler the corresponding element handler
*/
public void setElementHandler(String name, Class<? extends ElementHandler> handler) {
this.handlers.put(name, handler);
}
/**
* Indicates whether the variable with specified identifier is defined.
*
* @param id the identifier
* @return @{code true} if the variable is defined;
* @{code false} otherwise
*/
public boolean hasVariable(String id) {
return this.environment.containsKey(id);
}
/**
* Returns the value of the variable with specified identifier.
*
* @param id the identifier
* @return the value of the variable
*/
public Object getVariable(String id) {
if (!this.environment.containsKey(id)) {
throw new IllegalArgumentException("Unbound variable: " + id);
}
return this.environment.get(id);
}
/**
* Sets new value of the variable with specified identifier.
*
* @param id the identifier
* @param value new value of the variable
*/
public void setVariable(String id, Object value) {
this.environment.put(id, value);
}
/**
* Returns the array of readed objects.
*
* @return the array of readed objects
*/
public Object[] getObjects() {
return this.objects.toArray();
}
/**
* Adds the object to the list of readed objects.
*
* @param object the object that is readed from XML document
*/
void addObject(Object object) {
this.objects.add(object);
}
/**
* Prepares this handler to read objects from XML document.
*/
@Override
public void startDocument() {
this.objects.clear();
this.handler = null;
}
/**
* Parses opening tag of XML element
* using corresponding element handler.
*
* @param uri the namespace URI, or the empty string
* if the element has no namespace URI or
* if namespace processing is not being performed
* @param localName the local name (without prefix), or the empty string
* if namespace processing is not being performed
* @param qName the qualified name (with prefix), or the empty string
* if qualified names are not available
* @param attributes the attributes attached to the element
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
ElementHandler parent = this.handler;
try {
this.handler = getElementHandler(qName).newInstance();
this.handler.setOwner(this);
this.handler.setParent(parent);
}
catch (Exception exception) {
throw new SAXException(exception);
}
for (int i = 0; i < attributes.getLength(); i++)
try {
String name = attributes.getQName(i);
String value = attributes.getValue(i);
this.handler.addAttribute(name, value);
}
catch (RuntimeException exception) {
handleException(exception);
}
this.handler.startElement();
}
/**
* Parses closing tag of XML element
* using corresponding element handler.
*
* @param uri the namespace URI, or the empty string
* if the element has no namespace URI or
* if namespace processing is not being performed
* @param localName the local name (without prefix), or the empty string
* if namespace processing is not being performed
* @param qName the qualified name (with prefix), or the empty string
* if qualified names are not available
*/
@Override
public void endElement(String uri, String localName, String qName) {
try {
this.handler.endElement();
}
catch (RuntimeException exception) {
handleException(exception);
}
finally {
this.handler = this.handler.getParent();
}
}
/**
* Parses character data inside XML element.
*
* @param chars the array of characters
* @param start the start position in the character array
* @param length the number of characters to use
*/
@Override
public void characters(char[] chars, int start, int length) {
if (this.handler != null) {
try {
while (0 < length--) {
this.handler.addCharacter(chars[start++]);
}
}
catch (RuntimeException exception) {
handleException(exception);
}
}
}
/**
* Handles an exception using current exception listener.
*
* @param exception an exception to handle
* @see #setExceptionListener
*/
public void handleException(Exception exception) {
if (this.listener == null) {
throw new IllegalStateException(exception);
}
this.listener.exceptionThrown(exception);
}
/**
* Starts parsing of the specified input source.
*
* @param input the input source to parse
*/
public void parse(final InputSource input) {
if ((this.acc == null) && (null != System.getSecurityManager())) {
throw new SecurityException("AccessControlContext is not set");
}
AccessControlContext stack = AccessController.getContext();
SharedSecrets.getJavaSecurityAccess().doIntersectionPrivilege(new PrivilegedAction<Void>() {
public Void run() {
try {
SAXParserFactory.newInstance().newSAXParser().parse(input, DocumentHandler.this);
}
catch (ParserConfigurationException exception) {
handleException(exception);
}
catch (SAXException wrapper) {
Exception exception = wrapper.getException();
if (exception == null) {
exception = wrapper;
}
handleException(exception);
}
catch (IOException exception) {
handleException(exception);
}
return null;
}
}, stack, this.acc);
}
/**
* Resolves class by name using current class loader.
* This method handles exception using current exception listener.
*
* @param name the name of the class
* @return the object that represents the class
*/
public Class<?> findClass(String name) {
try {
return ClassFinder.resolveClass(name, getClassLoader());
}
catch (ClassNotFoundException exception) {
handleException(exception);
return null;
}
}
}