All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.persistence.internal.oxm.XMLFragmentMappingNodeValue Maven / Gradle / Ivy

There is a newer version: 5.0.0-B03
Show newest version
/*
 * Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0,
 * or the Eclipse Distribution License v. 1.0 which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
 */

// Contributors:
//     Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.internal.oxm;

import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.persistence.core.sessions.CoreSession;
import org.eclipse.persistence.internal.core.sessions.CoreAbstractSession;
import org.eclipse.persistence.internal.oxm.mappings.Field;
import org.eclipse.persistence.internal.oxm.mappings.FragmentMapping;
import org.eclipse.persistence.internal.oxm.record.MarshalContext;
import org.eclipse.persistence.internal.oxm.record.MarshalRecord;
import org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext;
import org.eclipse.persistence.internal.oxm.record.UnmarshalRecord;
import org.eclipse.persistence.internal.oxm.record.XMLReader;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * INTERNAL:
 * 

Purpose: This is how the XML Fragment Collection Mapping is handled * when used with the TreeObjectBuilder.

* @author mmacivor */ public class XMLFragmentMappingNodeValue extends MappingNodeValue implements NullCapableValue { private FragmentMapping xmlFragmentMapping; private boolean selfMapping; public XMLFragmentMappingNodeValue(FragmentMapping xmlFragmentMapping) { super(); this.xmlFragmentMapping = xmlFragmentMapping; this.selfMapping = XPathFragment.SELF_XPATH.equals(xmlFragmentMapping.getXPath()); } @Override public boolean isOwningNode(XPathFragment xPathFragment) { return xPathFragment.getNextFragment() == null; } @Override public void setNullValue(Object object, CoreSession session) { Object value = xmlFragmentMapping.getObjectValue(null, session); xmlFragmentMapping.setAttributeValueInObject(object, value); } @Override public boolean isNullCapableValue() { return true; } @Override public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver) { return marshal(xPathFragment, marshalRecord, object, session, namespaceResolver, ObjectMarshalContext.getInstance()); } @Override public boolean marshal(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) { if (xmlFragmentMapping.isReadOnly()) { return false; } Object attributeValue = marshalContext.getAttributeValue(object, xmlFragmentMapping); return this.marshalSingleValue(xPathFragment, marshalRecord, object, attributeValue, session, namespaceResolver, marshalContext); } @Override public boolean marshalSelfAttributes(XPathFragment pathFragment, MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver, Marshaller marshaller) { Node node = (Node) xmlFragmentMapping.getAttributeValueFromObject(object); NamedNodeMap attributes = node.getAttributes(); if(null != attributes) { for(int x=0, attributesLength=attributes.getLength(); x next:((Map) unmarshalRecord.getPrefixesForFragment()).entrySet()) { builder.startPrefixMapping(next.getKey(), next.getValue()); } } builder.startElement(namespaceURI, xPathFragment.getLocalName(), qName, atts); XMLReader xmlReader = unmarshalRecord.getXMLReader(); xmlReader.setContentHandler(builder); xmlReader.setLexicalHandler(null); } catch (SAXException ex) { // Do nothing. } return true; } @Override public void endElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord) { unmarshalRecord.removeNullCapableValue(this); XPathFragment lastFrag = ((Field)xmlFragmentMapping.getField()).getLastXPathFragment(); SAXFragmentBuilder builder = unmarshalRecord.getFragmentBuilder(); if (lastFrag.nameIsText()) { Object attributeValue = builder.buildTextNode(unmarshalRecord.getCharacters().toString()); unmarshalRecord.resetStringBuffer(); xmlFragmentMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), attributeValue); } else if (!lastFrag.isAttribute()) { Object value = builder.getNodes().remove(builder.getNodes().size() -1); unmarshalRecord.setAttributeValue(value, xmlFragmentMapping); } } @Override public void endSelfNodeValue(UnmarshalRecord unmarshalRecord, UnmarshalRecord selfRecord, Attributes atts) { this.endElement(XPathFragment.SELF_FRAGMENT, unmarshalRecord); } @Override public void attribute(UnmarshalRecord unmarshalRecord, String namespaceURI, String localName, String value) { unmarshalRecord.removeNullCapableValue(this); if(namespaceURI == null) { namespaceURI = Constants.EMPTY_STRING; } SAXFragmentBuilder builder = unmarshalRecord.getFragmentBuilder(); Object attributeValue = builder.buildAttributeNode(namespaceURI, localName, value); xmlFragmentMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), attributeValue); } @Override public FragmentMapping getMapping() { return xmlFragmentMapping; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy