use of org.glassfish.hk2.xml.internal.ChildDescriptor in project glassfish-hk2 by eclipse-ee4j.
the class XmlStreamImpl method marshallElement.
@SuppressWarnings("unchecked")
private static void marshallElement(XMLStreamWriter indenter, XmlHk2ConfigurationBean bean, ParentedModel parented) throws XMLStreamException {
ModelImpl model = bean._getModel();
Map<String, Object> beanLikeMap = bean._getBeanLikeMap();
String xmlTag;
if (parented == null) {
QName rootName = model.getRootName();
// TODO, how do I get the prefix?
xmlTag = rootName.getLocalPart();
} else {
xmlTag = parented.getChildXmlTag();
}
indenter.writeStartElement(xmlTag);
Map<QName, ChildDataModel> attributeModels = model.getAllAttributeChildren();
for (Map.Entry<QName, ChildDataModel> entry : attributeModels.entrySet()) {
QName attributeQName = entry.getKey();
ChildDataModel childDataModel = entry.getValue();
String attributeTagKey = attributeQName.getLocalPart();
Object value = beanLikeMap.get(attributeTagKey);
if (value == null)
continue;
String valueAsString;
if (!childDataModel.isReference()) {
valueAsString = value.toString();
} else {
XmlHk2ConfigurationBean reference = (XmlHk2ConfigurationBean) value;
valueAsString = reference._getKeyValue();
}
if (GeneralUtilities.safeEquals(valueAsString, childDataModel.getDefaultAsString()))
continue;
indenter.writeAttribute(attributeTagKey, valueAsString);
}
Map<QName, ChildDescriptor> elementDescriptors = model.getAllElementChildren();
for (Map.Entry<QName, ChildDescriptor> entry : elementDescriptors.entrySet()) {
QName elementQName = entry.getKey();
ChildDescriptor descriptor = entry.getValue();
String elementTagKey = elementQName.getLocalPart();
Object value = beanLikeMap.get(elementTagKey);
if (value == null)
continue;
ParentedModel parentedChild = descriptor.getParentedModel();
if (parentedChild != null) {
if (AliasType.HAS_ALIASES.equals(parentedChild.getAliasType())) {
continue;
}
if (ChildType.LIST.equals(parentedChild.getChildType())) {
List<XmlHk2ConfigurationBean> asList = (List<XmlHk2ConfigurationBean>) value;
if (asList.isEmpty())
continue;
for (XmlHk2ConfigurationBean child : asList) {
marshallElement(indenter, child, parentedChild);
}
} else if (ChildType.ARRAY.equals(parentedChild.getChildType())) {
int length = Array.getLength(value);
if (length <= 0)
continue;
for (int lcv = 0; lcv < length; lcv++) {
XmlHk2ConfigurationBean child = (XmlHk2ConfigurationBean) Array.get(value, lcv);
marshallElement(indenter, child, parentedChild);
}
} else {
// Direct
marshallElement(indenter, (XmlHk2ConfigurationBean) value, parentedChild);
}
} else {
ChildDataModel childDataModel = descriptor.getChildDataModel();
if (!childDataModel.isReference()) {
if (!value.getClass().isArray()) {
String valueAsString = value.toString();
indenter.writeStartElement(elementTagKey);
indenter.writeCharacters(valueAsString);
indenter.writeEndElement();
} else {
int length = Array.getLength(value);
for (int lcv = 0; lcv < length; lcv++) {
Object indexedValue = Array.get(value, lcv);
String valueAsString = indexedValue.toString();
indenter.writeStartElement(elementTagKey);
indenter.writeCharacters(valueAsString);
indenter.writeEndElement();
}
}
} else {
XmlHk2ConfigurationBean reference = (XmlHk2ConfigurationBean) value;
String keyValue = reference._getKeyValue();
indenter.writeStartElement(elementTagKey);
indenter.writeCharacters(keyValue);
indenter.writeEndElement();
}
}
}
indenter.writeEndElement();
}
use of org.glassfish.hk2.xml.internal.ChildDescriptor in project glassfish-hk2 by eclipse-ee4j.
the class PBufParser method convertModelToDescriptor.
private Descriptors.Descriptor convertModelToDescriptor(ModelImpl model, Set<Descriptors.FileDescriptor> knownFiles) throws Exception {
Map<QName, ChildDescriptor> allChildren = model.getAllChildrenDescriptors();
String protoName = getProtoNameFromModel(model);
String packageName = getPackageNameFromModel(model);
DescriptorProtos.DescriptorProto.Builder builder = DescriptorProtos.DescriptorProto.newBuilder();
builder.setName(protoName);
Class<?> originalInterface = model.getOriginalInterfaceAsClass();
validateXmlType(originalInterface, allChildren);
int oneOfNumber = 0;
Map<String, Integer> oneOfToIndexMap = new HashMap<String, Integer>();
String currentOneOf = null;
int number = 1;
for (Map.Entry<QName, ChildDescriptor> entry : allChildren.entrySet()) {
QName entryKey = entry.getKey();
String localPart = entryKey.getLocalPart();
ChildDescriptor childDescriptor = entry.getValue();
String protoPart = PBUtilities.camelCaseToUnderscore(localPart);
DescriptorProtos.FieldDescriptorProto.Builder fBuilder = DescriptorProtos.FieldDescriptorProto.newBuilder().setName(protoPart);
fBuilder.setNumber(number);
number++;
ChildDataModel dataModel = childDescriptor.getChildDataModel();
if (dataModel != null) {
fBuilder.setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL);
Class<?> dataType = dataModel.getChildTypeAsClass();
String originalMethodName = dataModel.getOriginalMethodName();
String oneOfValue = PBUtilities.getOneOf(originalInterface, originalMethodName, dataType);
if (GeneralUtilities.safeEquals(oneOfValue, currentOneOf)) {
if (oneOfValue != null) {
int oneOfDeclIndex = oneOfToIndexMap.get(oneOfValue);
fBuilder.setOneofIndex(oneOfDeclIndex);
}
} else {
if (oneOfValue != null) {
DescriptorProtos.OneofDescriptorProto.Builder oneOfBuilder = DescriptorProtos.OneofDescriptorProto.newBuilder();
oneOfBuilder.setName(oneOfValue);
int oneOfIndex = oneOfNumber++;
builder.addOneofDecl(oneOfIndex, oneOfBuilder.build());
oneOfToIndexMap.put(oneOfValue, oneOfIndex);
fBuilder.setOneofIndex(oneOfIndex);
}
currentOneOf = oneOfValue;
}
if (dataModel.getDefaultAsString() != null) {
fBuilder.setDefaultValue(dataModel.getDefaultAsString());
}
DescriptorProtos.FieldDescriptorProto.Type fieldType = convertChildDataModelToType(dataModel);
if (DescriptorProtos.FieldDescriptorProto.Type.TYPE_ENUM.equals(fieldType)) {
String fieldTypeName = convertEnumToDescriptor(dataModel, knownFiles);
fBuilder.setTypeName(fieldTypeName);
}
fBuilder.setType(fieldType);
} else {
ParentedModel pm = childDescriptor.getParentedModel();
// Set the type
fBuilder.setType(DescriptorProtos.FieldDescriptorProto.Type.TYPE_MESSAGE);
Class<?> childDataType = pm.getChildModel().getOriginalInterfaceAsClass();
String originalMethodName = pm.getOriginalMethodName();
String oneOfValue = PBUtilities.getOneOf(originalInterface, originalMethodName, childDataType);
if (GeneralUtilities.safeEquals(oneOfValue, currentOneOf)) {
if (oneOfValue != null) {
int oneOfDeclIndex = oneOfToIndexMap.get(oneOfValue);
fBuilder.setOneofIndex(oneOfDeclIndex);
}
} else {
if (oneOfValue != null) {
DescriptorProtos.OneofDescriptorProto.Builder oneOfBuilder = DescriptorProtos.OneofDescriptorProto.newBuilder();
oneOfBuilder.setName(oneOfValue);
int oneOfIndex = oneOfNumber++;
builder.addOneofDecl(oneOfIndex, oneOfBuilder.build());
oneOfToIndexMap.put(oneOfValue, oneOfIndex);
fBuilder.setOneofIndex(oneOfIndex);
}
currentOneOf = oneOfValue;
}
ModelImpl childModel = pm.getChildModel();
String childTypeName = childModel.getOriginalInterface();
fBuilder.setTypeName("." + childTypeName);
ChildType childType = pm.getChildType();
if (childType.equals(ChildType.ARRAY) || childType.equals(ChildType.LIST)) {
fBuilder.setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_REPEATED);
} else {
fBuilder.setLabel(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL);
}
}
builder.addField(fBuilder.build());
}
DescriptorProtos.DescriptorProto proto = builder.build();
DescriptorProtos.FileDescriptorProto.Builder fileBuilder = DescriptorProtos.FileDescriptorProto.newBuilder();
fileBuilder.addMessageType(proto);
if (packageName != null) {
fileBuilder.setPackage(packageName);
}
DescriptorProtos.FileDescriptorProto fProto = fileBuilder.build();
Descriptors.FileDescriptor fDesc = Descriptors.FileDescriptor.buildFrom(fProto, knownFiles.toArray(new Descriptors.FileDescriptor[knownFiles.size()]));
Descriptors.Descriptor fD = fDesc.findMessageTypeByName(protoName);
return fD;
}
use of org.glassfish.hk2.xml.internal.ChildDescriptor in project glassfish-hk2 by eclipse-ee4j.
the class PBufParser method validateXmlType.
private static void validateXmlType(Class<?> originalInterface, Map<QName, ChildDescriptor> allChildren) {
XmlType xmlType = originalInterface.getAnnotation(XmlType.class);
if (xmlType == null) {
throw new IllegalStateException("When using protocol buffers the XmlType MUST be on the interface. The interface " + originalInterface.getName() + " does not have one");
}
String[] propOrder = xmlType.propOrder();
Set<String> uniq = new HashSet<String>();
for (String order : propOrder) {
if (uniq.contains(order)) {
throw new IllegalStateException("XmlType propOrder field on " + originalInterface.getName() + " has duplicate value " + order);
}
uniq.add(order);
}
Set<String> extras = new HashSet<String>();
Set<String> missing = new HashSet<String>(uniq);
for (ChildDescriptor cd : allChildren.values()) {
ChildDataModel cdm = cd.getChildDataModel();
String interfaceMethod;
if (cdm != null) {
interfaceMethod = getXmlTypeValueFromMethodName(cdm.getOriginalMethodName(), cdm);
} else {
ParentedModel pm = cd.getParentedModel();
interfaceMethod = getXmlTypeValueFromMethodName(pm.getOriginalMethodName(), pm);
}
missing.remove(interfaceMethod);
if (!uniq.contains(interfaceMethod)) {
extras.add(interfaceMethod);
}
}
if (!missing.isEmpty() || !extras.isEmpty()) {
throw new IllegalStateException("On interface " + originalInterface.getName() + " the XmlType propOrder field had these extra fields " + missing + " or missing fields " + extras);
}
}
use of org.glassfish.hk2.xml.internal.ChildDescriptor in project glassfish-hk2 by eclipse-ee4j.
the class PBufParser method parseDynamicMessage.
private XmlHk2ConfigurationBean parseDynamicMessage(ModelImpl model, XmlHk2ConfigurationBean parent, DynamicMessage message, Listener listener) throws IOException {
BaseHK2JAXBBean bean = (BaseHK2JAXBBean) xmlService.get().createBean(model.getOriginalInterfaceAsClass());
Descriptors.Descriptor descriptor = message.getDescriptorForType();
listener.beforeUnmarshal(bean, parent);
for (Map.Entry<QName, ChildDescriptor> entry : model.getAllChildrenDescriptors().entrySet()) {
QName qname = entry.getKey();
ChildDescriptor childDescriptor = entry.getValue();
String localPart = qname.getLocalPart();
String protoPart = PBUtilities.camelCaseToUnderscore(localPart);
Descriptors.FieldDescriptor fieldDescriptor = descriptor.findFieldByName(protoPart);
if (fieldDescriptor == null) {
throw new IOException("Unknown field " + protoPart + " in " + bean);
}
ChildDataModel childDataModel = childDescriptor.getChildDataModel();
if (childDataModel != null) {
boolean fieldSet = message.hasField(fieldDescriptor);
if (!fieldSet) {
continue;
}
Object value = message.getField(fieldDescriptor);
value = convertFieldForUnmarshal(value, childDataModel);
bean._setProperty(qname, value);
} else {
ParentedModel parentedNode = childDescriptor.getParentedModel();
if (ChildType.DIRECT.equals(parentedNode.getChildType())) {
// Cannot call hasField on repeated fields in pbuf
boolean fieldSet = message.hasField(fieldDescriptor);
if (!fieldSet) {
continue;
}
}
Object value = message.getField(fieldDescriptor);
if (value == null) {
continue;
}
DynamicMessage dynamicChild = null;
XmlHk2ConfigurationBean child = null;
int repeatedFieldCount = 0;
switch(parentedNode.getChildType()) {
case DIRECT:
if (!(value instanceof DynamicMessage)) {
throw new AssertionError("Do not know how to handle a non-dynamic direct message " + value);
}
dynamicChild = (DynamicMessage) value;
child = parseDynamicMessage(parentedNode.getChildModel(), bean, dynamicChild, listener);
bean._setProperty(qname, child);
break;
case LIST:
repeatedFieldCount = message.getRepeatedFieldCount(fieldDescriptor);
ArrayList<XmlHk2ConfigurationBean> list = new ArrayList<XmlHk2ConfigurationBean>(repeatedFieldCount);
for (int lcv = 0; lcv < repeatedFieldCount; lcv++) {
Object childBean = message.getRepeatedField(fieldDescriptor, lcv);
if (!(childBean instanceof DynamicMessage)) {
throw new AssertionError("Do not know how to handle a non-dynamic list message " + childBean);
}
dynamicChild = (DynamicMessage) childBean;
child = parseDynamicMessage(parentedNode.getChildModel(), bean, dynamicChild, listener);
list.add(child);
}
bean._setProperty(qname, list);
break;
case ARRAY:
ModelImpl childModel = parentedNode.getChildModel();
repeatedFieldCount = message.getRepeatedFieldCount(fieldDescriptor);
Object array = Array.newInstance(childModel.getOriginalInterfaceAsClass(), repeatedFieldCount);
for (int lcv = 0; lcv < repeatedFieldCount; lcv++) {
Object childBean = message.getRepeatedField(fieldDescriptor, lcv);
if (!(childBean instanceof DynamicMessage)) {
throw new AssertionError("Do not know how to handle a non-dynamic array message " + childBean);
}
dynamicChild = (DynamicMessage) childBean;
child = parseDynamicMessage(parentedNode.getChildModel(), bean, dynamicChild, listener);
Array.set(array, lcv, child);
}
bean._setProperty(qname, array);
break;
default:
throw new IOException("Unknown child type: " + parentedNode.getChildType());
}
}
}
listener.afterUnmarshal(bean, parent);
return bean;
}
use of org.glassfish.hk2.xml.internal.ChildDescriptor in project glassfish-hk2 by eclipse-ee4j.
the class JsonParser method createJsonObject.
@SuppressWarnings("unchecked")
private JsonObject createJsonObject(BaseHK2JAXBBean bean, JsonObjectBuilder builder) {
if (bean == null) {
return builder.build();
}
ModelImpl model = bean._getModel();
Map<QName, ChildDescriptor> allChildren = model.getAllChildrenDescriptors();
for (Map.Entry<QName, ChildDescriptor> entry : allChildren.entrySet()) {
QName keyNameQName = entry.getKey();
String keyName = keyNameQName.getLocalPart();
if (!bean._isSet(keyName))
continue;
Object value = bean._getProperty(XmlService.DEFAULT_NAMESPACE, keyName);
ParentedModel parented = entry.getValue().getParentedModel();
if (parented != null) {
if (ChildType.DIRECT.equals(parented.getChildType())) {
if (value != null) {
builder = builder.add(keyName, createJsonObject((BaseHK2JAXBBean) value, Json.createObjectBuilder()));
}
} else if (ChildType.LIST.equals(parented.getChildType())) {
List<BaseHK2JAXBBean> list = (List<BaseHK2JAXBBean>) value;
if (list != null && !list.isEmpty()) {
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (BaseHK2JAXBBean item : list) {
JsonObjectBuilder childBuilder = Json.createObjectBuilder();
JsonObject obj = createJsonObject(item, childBuilder);
jsonArray.add(obj);
}
builder.add(keyName, jsonArray);
}
} else if (ChildType.ARRAY.equals(parented.getChildType())) {
int length = Array.getLength(value);
if (length > 0) {
JsonArrayBuilder jsonArray = Json.createArrayBuilder();
for (int lcv = 0; lcv < length; lcv++) {
BaseHK2JAXBBean item = (BaseHK2JAXBBean) Array.get(value, lcv);
JsonObjectBuilder childBuilder = Json.createObjectBuilder();
JsonObject obj = createJsonObject(item, childBuilder);
jsonArray.add(obj);
}
builder.add(keyName, jsonArray);
}
} else {
throw new AssertionError("Unknown childType " + parented.getChildType());
}
} else {
if (value == null) {
builder.addNull(keyName);
} else if (value instanceof Integer) {
builder.add(keyName, ((Integer) value).intValue());
} else if (value instanceof Long) {
builder.add(keyName, ((Long) value).longValue());
} else if (value instanceof Boolean) {
builder.add(keyName, ((Boolean) value).booleanValue());
} else {
builder.add(keyName, value.toString());
}
}
}
return builder.build();
}
Aggregations