@Override public Collection<ElementKey<?, ?>> getElements() { return ImmutableList.<ElementKey<?, ?>>of(childMetadata.getKey()); }
/** * Checks that a particular piece of metadata is not multiple cardinality. * * @throws IllegalStateException if the metadata is multiple cardinality */ private static void checkNotMultiple(ElementMetadata<?, ?> meta) { if (meta != null && meta.getCardinality() != Cardinality.SINGLE) { throw new IllegalStateException("Metadata for key " + meta.getKey() + " represents a multiple-cardinality element." + " The path cannot contain more than one multiple-cardinality" + " element."); } }
/** * Checks that a particular piece of metadata is not multiple cardinality. * * @throws IllegalStateException if the metadata is multiple cardinality */ private static void checkNotMultiple(ElementMetadata<?, ?> meta) { if (meta != null && meta.getCardinality() != Cardinality.SINGLE) { throw new IllegalStateException("Metadata for key " + meta.getKey() + " represents a multiple-cardinality element." + " The path cannot contain more than one multiple-cardinality" + " element."); } }
public ElementKey<D, E> getKey() { return delegate().getKey(); }
/** * Checks that a particular piece of metadata is not multiple cardinality. * * @throws IllegalStateException if the metadata is multiple cardinality */ private static void checkNotMultiple(ElementMetadata<?, ?> meta) { if (meta != null && meta.getCardinality() != Cardinality.SINGLE) { throw new IllegalStateException("Metadata for key " + meta.getKey() + " represents a multiple-cardinality element." + " The path cannot contain more than one multiple-cardinality" + " element."); } }
public ElementKey<D, E> getKey() { return delegate().getKey(); }
public ElementKey<D, E> getKey() { return delegate().getKey(); }
public void generate(Element element, ElementMetadata<?, ?> metadata) throws IOException { if (metadata != null && !metadata.getKey().equals(element.getElementKey())) { throw new IllegalStateException( "Element key (" + element.getElementKey() + ") does not match metadata key (" + metadata.getKey() + ")"); } try { element.visit(this, metadata); } catch (StoppedException se) { Throwable cause = se.getCause(); if (cause instanceof IOException) { throw (IOException) cause; } throw se; // unexpected } }
public void generate(Element element, ElementMetadata<?, ?> metadata) throws IOException { if (metadata != null && !metadata.getKey().equals(element.getElementKey())) { throw new IllegalStateException( "Element key (" + element.getElementKey() + ") does not match metadata key (" + metadata.getKey() + ")"); } try { element.visit(this, metadata); } catch (StoppedException se) { Throwable cause = se.getCause(); if (cause instanceof IOException) { throw (IOException) cause; } throw se; // unexpected } }
public void generate(Element element, ElementMetadata<?, ?> metadata) throws IOException { if (metadata != null && !metadata.getKey().equals(element.getElementKey())) { throw new IllegalStateException( "Element key (" + element.getElementKey() + ") does not match metadata key (" + metadata.getKey() + ")"); } try { element.visit(this, metadata); } catch (StoppedException se) { Throwable cause = se.getCause(); if (cause instanceof IOException) { throw (IOException) cause; } throw se; // unexpected } }
/** * Generate multiple elements based on this path. This only allows a single * multiple cardinality element in the path, but that multiple cardinality * element can be anywhere in the path. Once found we only follow single * cardinality children after that point. * * cardinality along the path. */ public Collection<? extends Element> generateMultiple(Element parent, ElementMetadata<?, ?> parentMetadata, ElementMetadata<?, ?> metadata) { Preconditions.checkState(path.selectsElement(), "An attribute path cannot be used to generate elements."); Collection<? extends Element> elements = getFinalElements(path, parent); if (elements.isEmpty()) { return elements; } List<Element> result = Lists.newArrayListWithCapacity(elements.size()); for (Element e : elements) { try { ElementKey<?, ?> realKey = mergeKeys( e.getElementKey(), metadata.getKey()); result.add(Element.createElement(realKey, e)); } catch (ContentCreationException ex) { throw new IllegalArgumentException("Invalid metadata", ex); } } return result; }
/** * Generate multiple elements based on this path. This only allows a single * multiple cardinality element in the path, but that multiple cardinality * element can be anywhere in the path. Once found we only follow single * cardinality children after that point. * * cardinality along the path. */ public Collection<? extends Element> generateMultiple(Element parent, ElementMetadata<?, ?> parentMetadata, ElementMetadata<?, ?> metadata) { Preconditions.checkState(path.selectsElement(), "An attribute path cannot be used to generate elements."); Collection<? extends Element> elements = getFinalElements(path, parent); if (elements.isEmpty()) { return elements; } List<Element> result = Lists.newArrayListWithCapacity(elements.size()); for (Element e : elements) { try { ElementKey<?, ?> realKey = mergeKeys( e.getElementKey(), metadata.getKey()); result.add(Element.createElement(realKey, e)); } catch (ContentCreationException ex) { throw new IllegalArgumentException("Invalid metadata", ex); } } return result; }
@Override public <K, L extends Element> ElementMetadata<K, L> bindElement( ElementKey<K, L> key) { // Use the provided partial metadata for any child with a matching id if (childMetadata.getKey().getId().equals(key.getId())) { @SuppressWarnings("unchecked") ElementMetadata<K, L> metadata = (ElementMetadata<K, L>) childMetadata; return metadata; } return super.bindElement(key); }
@Override public boolean isDeclared(ElementKey<?, ?> key) { if (childMetadata.getKey().getId().equals(key.getId())) { return true; } return super.isDeclared(key); }
private static void addReferencedNamespaces(ElementMetadata<?, ?> metadata, ImmutableSet.Builder<XmlNamespace> builder, Set<ElementKey<?, ?>> added) { // Avoid recursive looping if (added.contains(metadata.getKey())) { return; } added.add(metadata.getKey()); // Add namespace for this element (if any) XmlNamespace elemNs = metadata.getName().getNs(); if (elemNs != null) { builder.add(elemNs); } // Add namespace for all attributes (if any) for (AttributeKey<?> attrKey : metadata.getAttributes()) { AttributeMetadata<?> attrMetadata = metadata.bindAttribute(attrKey); XmlNamespace attrNs = attrMetadata.getName().getNs(); if (attrNs != null) { builder.add(attrNs); } } // Add namespace for all child elements (recursively) for (ElementKey<?, ?> elemKey : metadata.getElements()) { ElementMetadata<?, ?> childMetadata = metadata.bindElement(elemKey); addReferencedNamespaces(childMetadata, builder, added); } }
/** * Generates a single element on the parent by using this path. This * will follow the path to its end, and then use that same element content * as the returned element, after wrapping it with the correct key (id). * * elements along it. */ public Element generateSingle(Element parent, ElementMetadata<?, ?> parentMetadata, ElementMetadata<?, ?> metadata) { Preconditions.checkState(path.selectsElement(), "An attribute path cannot be used to generate elements."); Element element = getFinalElement(path, parent); if (element == null) { return null; } try { ElementKey<?, ?> realKey = mergeKeys( element.getElementKey(), metadata.getKey()); return Element.createElement(realKey, element); } catch (ContentCreationException e) { throw new IllegalArgumentException("Invalid metadata", e); } }
/** * Generates a single element on the parent by using this path. This * will follow the path to its end, and then use that same element content * as the returned element, after wrapping it with the correct key (id). * * elements along it. */ public Element generateSingle(Element parent, ElementMetadata<?, ?> parentMetadata, ElementMetadata<?, ?> metadata) { Preconditions.checkState(path.selectsElement(), "An attribute path cannot be used to generate elements."); Element element = getFinalElement(path, parent); if (element == null) { return null; } try { ElementKey<?, ?> realKey = mergeKeys( element.getElementKey(), metadata.getKey()); return Element.createElement(realKey, element); } catch (ContentCreationException e) { throw new IllegalArgumentException("Invalid metadata", e); } }
/** * Generates a single element on the parent by using this path. This * will follow the path to its end, and then use that same element content * as the returned element, after wrapping it with the correct key (id). * * elements along it. */ public Element generateSingle(Element parent, ElementMetadata<?, ?> parentMetadata, ElementMetadata<?, ?> metadata) { Preconditions.checkState(path.selectsElement(), "An attribute path cannot be used to generate elements."); Element element = getFinalElement(path, parent); if (element == null) { return null; } try { ElementKey<?, ?> realKey = mergeKeys( element.getElementKey(), metadata.getKey()); return Element.createElement(realKey, element); } catch (ContentCreationException e) { throw new IllegalArgumentException("Invalid metadata", e); } }
public <R extends T> R parse(ParseSource parseSource, InputProperties inProps, Class<R> resultClass) throws IOException, ServiceException { // Copy-modify the input properties to expect a partial element that // wraps the expected type ElementMetadata<?, ?> metadata = inProps.getRootMetadata(); InputProperties partialProperties = new InputPropertiesBuilder(inProps) .setElementMetadata(new PartialMetadata(metadata)) .build(); // Create a parser to parse the partial-wrapped representation InputParser<Partial> partialParser = new PartialParser(fullParser); Partial p = partialParser.parse(parseSource, partialProperties, Partial.class); // Unwrap and return the inner partial representation return resultClass.cast(p.getElement(metadata.getKey())); }