/** * An attribute will have the following 4bytes words : * * <ul> * <li>0th word : index of namespace uri in StringIndexTable, or 0xFFFFFFFF for default NS * <li>1st word : index of attribute name in StringIndexTable * <li>2nd word : index of attribute value, or 0xFFFFFFFF if value is a typed value * <li>3rd word : value type * <li>4th word : resource id value * </ul> */ private Attribute parseAttribute() { final int attrNSIdx = getLEWord(mParserOffset); final int attrNameIdx = getLEWord(mParserOffset + (1 * WORD_SIZE)); final int attrValueIdx = getLEWord(mParserOffset + (2 * WORD_SIZE)); final int attrType = getLEWord(mParserOffset + (3 * WORD_SIZE)); final int attrData = getLEWord(mParserOffset + (4 * WORD_SIZE)); final Attribute attr = new Attribute(); attr.setName(getString(attrNameIdx)); if (attrNSIdx == 0xFFFFFFFF) { attr.setNamespace(null); attr.setPrefix(null); } else { String uri = getString(attrNSIdx); if (mNamespaces.containsKey(uri)) { attr.setNamespace(uri); attr.setPrefix(mNamespaces.get(uri)); } } if (attrValueIdx == 0xFFFFFFFF) { attr.setValue(getAttributeValue(attrType, attrData)); } else { attr.setValue(getString(attrValueIdx)); } return attr; }
@Test public void testAtt() { String nsUri = "nsUri"; String prefix = "prefix"; String localName = "localName"; String value = "value"; Attribute att = new Attribute(nsUri, localName, value); Assert.assertEquals(nsUri, att.getNamespace()); Assert.assertEquals(localName, att.getName()); Assert.assertEquals(value, att.getValue()); String nsUri2 = "nsUri2"; String prefix2 = "prefix2"; String localName2 = "localName2"; String value2 = "value2"; att.setNsURI(nsUri2); att.setName(localName2); att.setValue(value2); Assert.assertEquals(nsUri2, att.getNamespace()); Assert.assertEquals(localName2, att.getName()); Assert.assertEquals(value2, att.getValue()); }
private void setFinalBuffer(ChannelBuffer buffer) throws ErrorDataDecoderException, IOException { currentAttribute.addContent(buffer, true); String value = decodeAttribute(currentAttribute.getChannelBuffer().toString(charset), charset); currentAttribute.setValue(value); addHttpData(currentAttribute); currentAttribute = null; }
private Attribute buildNS(int index) throws XmlPullParserException { String nsName = mParser.getNamespacePrefix(index); String uri = mParser.getNamespaceUri(index); Attribute attr = new Attribute(nsName, "xmlns", uri); attr.setValue(uri); return attr; }
public void setAttribute(String name, Object value) { Attribute attr = attributes.get(name); if (attr == null) { attr = new Attribute(name); } attr.setValue(value); attributes.put(name, attr); }
@Test(expected = IllegalArgumentException.class) public void testWrapsThrownExceptions() throws Exception { // given final ThrowingTarget target = new ThrowingTarget(); final Method setter = getSetter(target); final Attribute<String> attribute = newSetterAttribute(setter); final String value = "value"; // when attribute.setValue(target, value); }
private static Attribute createAttribute(Map<String, String> beanAttr) throws InternalErrorException { if (beanAttr == null) return null; Attribute attribute = new Attribute(); attribute.setId(Integer.valueOf(beanAttr.get("id")).intValue()); attribute.setFriendlyName(BeansUtils.eraseEscaping(beanAttr.get("friendlyName"))); attribute.setNamespace(BeansUtils.eraseEscaping(beanAttr.get("namespace"))); attribute.setType(BeansUtils.eraseEscaping(beanAttr.get("type"))); attribute.setValue( BeansUtils.stringToAttributeValue( BeansUtils.eraseEscaping(beanAttr.get("value")), attribute.getType())); return attribute; }
private static final <V> void loadAttribute( final Attribute<V> attribute, final Element element, final String stringValue, final LoadingContext context) { Assert.notNull(stringValue, "stringValue"); V value = null; try { value = attribute.parseDomAttributeValue(stringValue); } catch (final QtiParseException ex) { context.modelBuildingError(ex, element); } attribute.setValue(value); }
@Test public void testInjectionInPublicSetter() throws Exception { // given final PublicTarget target = new PublicTarget(); final Method setter = getSetter(target); final Attribute<String> attribute = newSetterAttribute(setter); final String value = "value"; // when attribute.setValue(target, value); // then assertThat(target.property, is(equalTo(value))); }
private Attribute buildAttr(int index) { String name = mParser.getAttributeName(index); // String prefix = mParser.getAttributePrefix( index ); String prefix = null; String namespace = mParser.getAttributeNamespace(index); Attribute attr = new Attribute(name, prefix, namespace); String attrType = mParser.getAttributeType(index); String attrVal = mParser.getAttributeValue(index); attr.setAttrType(attrType); attr.setValue(attrVal); return attr; } // buildAttr
/** * Updates attributes list on node selection change in the tags tree. Loads attributes of the * selected node into the attributes list. Just clears attributes if no node is selected. * * @param e */ public void onTagChange(TreeSelectionEvent e) { if (e.getNewLeadSelectionPath() != null) { TreePath path = e.getNewLeadSelectionPath(); attributes.clear(); if (path.getPathCount() >= 1) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent(); Tag tag = (Tag) node.getUserObject(); for (String name : tag.getAttributes().keySet()) { Attribute attr = new Attribute(); attr.setName(name); attr.setValue(tag.getAttribute(name)); attributes.add(attr); } } } }
/** * Loads attribute's values from given source {@link Element} If there is a foreign attribute, it * creates new optional ForeignAttribute with its foreign property set. * * @param element source {@link Element} to load attributes from */ public void load(final Element element, final LoadingContext context) { /* First clear existing attributes */ for (int i = 0; i < attributes.size(); i++) { final Attribute<?> attribute = attributes.get(i); if (attribute instanceof ForeignAttribute) { /* Foreign attribute, so remove to add in again */ attributes.remove(i); } else { /* Supported attribute, so clear for setting later */ attribute.setValue(null); } } /* Set set values from element */ for (int i = 0; i < element.getAttributes().getLength(); i++) { final Node attributeNode = element.getAttributes().item(i); final String localName = attributeNode.getLocalName(); String namespaceUri = attributeNode.getNamespaceURI(); if (namespaceUri == null) { namespaceUri = ""; } if (XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI.equals(namespaceUri)) { /* (xsi attributes get ignored in our model) */ } else { Attribute<?> attribute = get(localName, namespaceUri, true); if (attribute == null) { /* Foreign attribute, so create new */ attribute = new ForeignAttribute(owner, localName, namespaceUri); attributes.add(attribute); } /* Load value into attribute */ final String attributeValue = attributeNode.getNodeValue(); loadAttribute(attribute, element, attributeValue, context); } } }
/** * This method fill the map and list with as much Attribute as possible from Body in not Multipart * mode. * * @throws ErrorDataDecoderException if there is a problem with the charset decoding or other * errors */ private void parseBodyAttributes() throws ErrorDataDecoderException { int firstpos = undecodedChunk.readerIndex(); int currentpos = firstpos; int equalpos = firstpos; int ampersandpos = firstpos; if (currentStatus == MultiPartStatus.NOTSTARTED) { currentStatus = MultiPartStatus.DISPOSITION; } boolean contRead = true; try { while (undecodedChunk.readable() && contRead) { char read = (char) undecodedChunk.readUnsignedByte(); currentpos++; switch (currentStatus) { case DISPOSITION: // search '=' if (read == '=') { currentStatus = MultiPartStatus.FIELD; equalpos = currentpos - 1; String key = decodeAttribute( undecodedChunk.toString(firstpos, equalpos - firstpos, charset), charset); currentAttribute = factory.createAttribute(request, key); firstpos = currentpos; } else if (read == '&') { // special empty FIELD currentStatus = MultiPartStatus.DISPOSITION; ampersandpos = currentpos - 1; String key = decodeAttribute( undecodedChunk.toString(firstpos, ampersandpos - firstpos, charset), charset); currentAttribute = factory.createAttribute(request, key); currentAttribute.setValue(""); // empty addHttpData(currentAttribute); currentAttribute = null; firstpos = currentpos; contRead = true; } break; case FIELD: // search '&' or end of line if (read == '&') { currentStatus = MultiPartStatus.DISPOSITION; ampersandpos = currentpos - 1; setFinalBuffer(undecodedChunk.slice(firstpos, ampersandpos - firstpos)); firstpos = currentpos; contRead = true; } else if (read == HttpCodecUtil.CR) { if (undecodedChunk.readable()) { read = (char) undecodedChunk.readUnsignedByte(); currentpos++; if (read == HttpCodecUtil.LF) { currentStatus = MultiPartStatus.PREEPILOGUE; ampersandpos = currentpos - 2; setFinalBuffer(undecodedChunk.slice(firstpos, ampersandpos - firstpos)); firstpos = currentpos; contRead = false; } else { // Error contRead = false; throw new ErrorDataDecoderException("Bad end of line"); } } else { currentpos--; } } else if (read == HttpCodecUtil.LF) { currentStatus = MultiPartStatus.PREEPILOGUE; ampersandpos = currentpos - 1; setFinalBuffer(undecodedChunk.slice(firstpos, ampersandpos - firstpos)); firstpos = currentpos; contRead = false; } break; default: // just stop contRead = false; } } if (isLastChunk && currentAttribute != null) { // special case ampersandpos = currentpos; if (ampersandpos > firstpos) { setFinalBuffer(undecodedChunk.slice(firstpos, ampersandpos - firstpos)); } else if (!currentAttribute.isCompleted()) { setFinalBuffer(ChannelBuffers.EMPTY_BUFFER); } firstpos = currentpos; currentStatus = MultiPartStatus.EPILOGUE; return; } if (contRead && currentAttribute != null) { // reset index except if to continue in case of FIELD status if (currentStatus == MultiPartStatus.FIELD) { currentAttribute.addContent(undecodedChunk.slice(firstpos, currentpos - firstpos), false); firstpos = currentpos; } undecodedChunk.readerIndex(firstpos); } else { // end of line so keep index } } catch (ErrorDataDecoderException e) { // error while decoding undecodedChunk.readerIndex(firstpos); throw e; } catch (IOException e) { // error while decoding undecodedChunk.readerIndex(firstpos); throw new ErrorDataDecoderException(e); } }
public void setUp() throws Exception { os = ObjectStoreFactory.getObjectStore("os.unittest"); item = new Item(); item.setClassName("Department"); item.setImplementations("Broke"); item.setIdentifier("1"); Attribute attr1 = new Attribute(); attr1.setName("name"); attr1.setValue("Department1"); item.addAttribute(attr1); Attribute attr2 = new Attribute(); attr2.setName("debt"); attr2.setValue("10"); item.addAttribute(attr2); Reference ref1 = new Reference(); ref1.setName("address"); ref1.setRefId("2"); item.addReference(ref1); ReferenceList col1 = new ReferenceList(); col1.setName("employees"); col1.addRefId("3"); col1.addRefId("4"); item.addCollection(col1); dbItem = new org.intermine.model.fulldata.Item(); dbItem.setClassName("Department"); dbItem.setImplementations("Broke"); dbItem.setIdentifier("1"); dbItem.setId(1001); org.intermine.model.fulldata.Attribute dbAttr1 = new org.intermine.model.fulldata.Attribute(); dbAttr1.setName("name"); dbAttr1.setValue("Department1"); dbAttr1.setItem(dbItem); dbItem.addAttributes(dbAttr1); org.intermine.model.fulldata.Attribute dbAttr2 = new org.intermine.model.fulldata.Attribute(); dbAttr2.setName("debt"); dbAttr2.setValue("10"); dbAttr2.setItem(dbItem); dbItem.addAttributes(dbAttr2); org.intermine.model.fulldata.Reference dbRef1 = new org.intermine.model.fulldata.Reference(); dbRef1.setName("address"); dbRef1.setRefId("2"); dbRef1.setItem(dbItem); dbItem.addReferences(dbRef1); org.intermine.model.fulldata.ReferenceList dbCol1 = new org.intermine.model.fulldata.ReferenceList(); dbCol1.setName("employees"); dbCol1.setRefIds("3 4"); dbCol1.setItem(dbItem); dbItem.addCollections(dbCol1); referenceList = new ReferenceList(); referenceList.setName("employees"); referenceList.setRefIds(Arrays.asList("3", "4")); dbReferenceList = new org.intermine.model.fulldata.ReferenceList(); dbReferenceList.setName("employees"); dbReferenceList.setRefIds("3 4"); departmentProxy = new ProxyReference(os, 1, Department.class); dbReferenceList.proxyItem(departmentProxy); // dbReferenceList.setId(2002); }