/** * Sets logo URI associated with source. * * @param v logo URI */ public void setLogo(URI v) { setElement(LOGO, (v == null) ? null : new Element(LOGO).setTextValue(v)); }
/** * Adds a new accept element. * * @param accept accept element */ public Collection addAccept(Accept accept) { super.addElement(Accept.KEY, accept); return this; }
/** * Copy constructor that initializes a new Element instance to be a wrapper * around another element instance. The element will use the given element * as its source for any content. * * @param elementKey the element key to associate with the copy. * @param source the element to copy data from. */ public Element(ElementKey<?, ?> elementKey, Element source) { this.key = bindKey(elementKey, getClass()); this.state = source.state; }
/** * Sets the value of the child element(s) with the given id. The given element * will replace all existing elements at the given id. If the given element * is {@code null}, this is equivalent to {@link #removeElement(QName)}. */ public Element setElement(QName id, Element element) { removeElement(id); if (element != null) { addElement(id, element); } return this; }
/** * Sets the value of the element and returns the element to allow chaining. * * @param newValue element's value * @return this element * @throws IllegalStateException if the element is immutable * @throws IllegalArgumentException if the object is of an invalid type or * if this element does not allow a value */ public Element setTextValue(Object newValue) { throwExceptionIfLocked(); state.value = checkValue(key, newValue); return this; }
throwExceptionIfLocked(); if (state.elements == null) { state.elements = new LinkedHashMap<QName, Object>(); ElementKey<?, ?> elementKey = element.getElementKey(); key = calculateKey(key, elementKey); if (!key.equals(elementKey)) { try { element = createElement(key, element); } catch (ContentCreationException e) { throw new IllegalArgumentException("Key " + key + " cannot be applied" state.elements.put(id, element); } else if (obj instanceof Collection<?>) { Collection<Element> collect = castElementCollection(obj); collect.add(element); } else { Collection<Element> collect = createCollection(key); collect.add((Element) obj); collect.add(element);
throwExceptionIfLocked(); return removeElement(toRemove); QName id = toRemove.getElementId(); if (!id.equals(toAdd.getElementId())) { boolean removed = removeElement(toRemove); if (removed) { addElement(toAdd); Object obj = state.elements.get(id); if (obj instanceof List<?>) { List<Element> list = castElementList(obj); for (int i = 0; i < list.size(); i++) { if (list.get(i) == toRemove) { Set<Element> set = castElementSet(obj); if (set.remove(toRemove)) { set.add(toAdd);
/** * Returns an immutable list of elements matching the given id. */ public List<Element> getElements(QName id) { // children, and if so add all of the wrapper junk we deal with in Multimap. ImmutableList.Builder<Element> builder = ImmutableList.builder(); Object obj = getElementObject(id); if (obj != null) { if (obj instanceof Element) { builder.add((Element) obj); } else { for (Element e : castElementCollection(obj)) { builder.add(e); } } } return builder.build(); }
throwExceptionIfLocked(); boolean removed = false; if (state.elements != null) { Object obj = getElementObject(childKey); if (obj instanceof Collection<?>) { Collection<Element> collect = castElementCollection(obj); Iterator<Element> iter = collect.iterator(); while (iter.hasNext()) { removeElement(childKey); removeElement(childKey); removed = true;
/** * Get child element matching the specified key. Will try to adapt the * element to the given key if it is not already an instance of the requested * class. This will fail with an exception if the adaptation was not valid. * * @param <T> the type of element to return * @param childKey the metadata key for the child element to retrieve * @return child element, or {@code null} if none was found * @throws IllegalArgumentException if the key referenced a repeating element */ public <D, T extends Element> T getElement(ElementKey<D, T> childKey) { Element child = getElement(childKey.getId()); if (child == null) { return null; } try { return adapt(childKey, child); } catch (ContentCreationException e) { throw new IllegalArgumentException("Unable to adapt to " + childKey.getElementType(), e); } }
/** * Locks this element. A locked element cannot have any changes made to its * content or its attributes or child elements. This will also lock all * attributes and child elements as well. Once this method has been called, * this element can be safely published to other threads. */ public Element lock() { state.locked = true; if (state.attributes != null) { for (Attribute att : state.attributes.values()) { att.lock(); } } if (state.elements != null) { for (Object childObj : state.elements.values()) { if (childObj instanceof Element) { ((Element) childObj).lock(); } else { for (Element child : castElementCollection(childObj)) { child.lock(); } } } } return this; }
&& key.equals(source.getElementKey()) && key.getElementType().isInstance(source)) { return key.getElementType().cast(source); args = new Object[] {key}; return construct(elementClass, argTypes, args); } catch (NoSuchMethodException e) { args = new Object[] {}; return construct(elementClass, argTypes, args);
/** * Returns the number of child elements present on this element. * @return number of elements. */ public int getElementCount() { int elementCount = 0; if (state.elements != null) { for (Object elementValue : state.elements.values()) { if (elementValue instanceof Collection) { elementCount += (castElementCollection(elementValue)).size(); } else { elementCount++; } } } return elementCount; }
/** * Adapts an element based on a key. This will find an adaptation in the * metadata and adapt to that metadata type (and element type). If no * adaptation is found this will return the source element. * * @param source the element we are narrowing from. * @param sourceMeta the source metadata to adapt from. * @param kind the kind name to lookup the adaptation for. * @return the adapted element if one was found. */ protected Element adapt(Element source, ElementMetadata<?, ?> sourceMeta, String kind) { ElementKey<?, ?> adaptorKey = sourceMeta.adapt(kind); if (adaptorKey != null) { try { return adapt(adaptorKey, source); } catch (ContentCreationException e) { // Not usable as a adaptable kind, skip. LOGGER.log(Level.SEVERE, "Unable to adapt " + source.getClass() + " to " + adaptorKey.getElementType(), e); } } return source; }
throwExceptionIfLocked(); if (state.elements == null) { state.elements = new LinkedHashMap<QName, Object>(); ElementKey<?, ?> elementKey = element.getElementKey(); key = calculateKey(key, elementKey); if (!key.equals(elementKey)) { try { element = createElement(key, element); } catch (ContentCreationException e) { throw new IllegalArgumentException("Key " + key + " cannot be applied" state.elements.put(id, element); } else if (obj instanceof Collection<?>) { Collection<Element> collect = castElementCollection(obj); collect.add(element); } else { Collection<Element> collect = createCollection(key); collect.add((Element) obj); collect.add(element);
throwExceptionIfLocked(); return removeElement(toRemove); QName id = toRemove.getElementId(); if (!id.equals(toAdd.getElementId())) { boolean removed = removeElement(toRemove); if (removed) { addElement(toAdd); Object obj = state.elements.get(id); if (obj instanceof List<?>) { List<Element> list = castElementList(obj); for (int i = 0; i < list.size(); i++) { if (list.get(i) == toRemove) { Set<Element> set = castElementSet(obj); if (set.remove(toRemove)) { set.add(toAdd);
/** * Returns an immutable list of elements matching the given id. */ public List<Element> getElements(QName id) { // children, and if so add all of the wrapper junk we deal with in Multimap. ImmutableList.Builder<Element> builder = ImmutableList.builder(); Object obj = getElementObject(id); if (obj != null) { if (obj instanceof Element) { builder.add((Element) obj); } else { for (Element e : castElementCollection(obj)) { builder.add(e); } } } return builder.build(); }
throwExceptionIfLocked(); boolean removed = false; if (state.elements != null) { Object obj = getElementObject(childKey); if (obj instanceof Collection<?>) { Collection<Element> collect = castElementCollection(obj); Iterator<Element> iter = collect.iterator(); while (iter.hasNext()) { removeElement(childKey); removeElement(childKey); removed = true;
/** * Sets the value of the child element(s) with the given id. The given element * will replace all existing elements at the given id. If the given element * is {@code null}, this is equivalent to {@link #removeElement(QName)}. */ public Element setElement(QName id, Element element) { removeElement(id); if (element != null) { addElement(id, element); } return this; }
/** * Sets the value of the element and returns the element to allow chaining. * * @param newValue element's value * @return this element * @throws IllegalStateException if the element is immutable * @throws IllegalArgumentException if the object is of an invalid type or * if this element does not allow a value */ public Element setTextValue(Object newValue) { throwExceptionIfLocked(); state.value = checkValue(key, newValue); return this; }