@Override protected final void startElement(Attributes attrs) { children = declareChildren(); if (children != null) { for (ElementHandler child : children.values()) { child.setParent(this); } } startSelf(attrs); }
@SuppressWarnings({"rawtypes", "unchecked"}) public ParserContext(ConfigurationBuilder builder) { List<ElementHandler> loadedHandlers = Iterators.asList(ServiceLoader.load(ElementHandler.class)); for (ElementHandler handler : loadedHandlers) { NamespaceElementHandler annotation = Annotations.getAnnotation(handler.getClass(), NamespaceElementHandler.class); if (annotation != null) { handlers.put(new HandlerId(annotation.namespace(), annotation.elementName()), handler); } } this.builder = builder; }
@SuppressWarnings("unchecked") public <T> T processElement(Element element) throws ConfigurationException { String namespace = $(element).namespaceURI(); String tagName = $(element).tag(); ElementHandler<?> handler = handlers.get(new HandlerId(namespace, tagName)); if (handler != null) { Object o = handler.processElement(this, element); return (T) o; } throw new ConfigurationException( "No Handler registered for element named [" + tagName + "] in namespace: [" + namespace + "]"); }
/** * Parses attributes of the element. The following attributes are supported: * * <dl> * <dt>idref * <dd>the identifier to refer to the variable * <dt>id * <dd>the identifier of the variable that is intended to store the result * </dl> * * @param name the attribute name * @param value the attribute value */ @Override public void addAttribute(String name, String value) { if (name.equals("idref")) { // NON-NLS: the attribute name this.value = ValueObjectImpl.create(getVariable(value)); } else { super.addAttribute(name, value); } }
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String elem = localName; if (elem.isEmpty()) { elem = qName; } elemStack.addFirst(elem); ElementHandler contextHandler = handlerStack.getFirst(); ElementHandler elemHandler = contextHandler.getHandler(elem); if (elemHandler == null) { throw new IllegalStateException( String.format( "Context handler %s have not returned handler for elem %s", contextHandler, elemStack)); } handlerStack.addFirst(elemHandler); elemHandler.startElement(attributes); }
public static int handleElements(NodeList nodeList, ElementHandler handler) throws Exception { int count = 0; for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node instanceof Element) { Element element = (Element) node; handler.handleElement(element); ++count; } } return count; }
@Override public void endElement(String uri, String localName, String qName) throws SAXException { String elem = localName; if (elem.isEmpty()) { elem = qName; } // check doc structure sanity if (!elemStack.getFirst().equals(elem)) { throw new IllegalStateException( String.format("Elem ending expected: %s, but was: %s", elemStack.getFirst(), elem)); } ElementHandler elemHandler = handlerStack.removeFirst(); if (sb != null) { String txt = sb.toString(); // ! txt.trim(); elemHandler.characters(txt); sb = null; } elemHandler.endElement(); elemStack.removeFirst(); }