Example usage for javax.xml XMLConstants W3C_XML_SCHEMA_INSTANCE_NS_URI

List of usage examples for javax.xml XMLConstants W3C_XML_SCHEMA_INSTANCE_NS_URI

Introduction

In this page you can find the example usage for javax.xml XMLConstants W3C_XML_SCHEMA_INSTANCE_NS_URI.

Prototype

String W3C_XML_SCHEMA_INSTANCE_NS_URI

To view the source code for javax.xml XMLConstants W3C_XML_SCHEMA_INSTANCE_NS_URI.

Click Source Link

Document

W3C XML Schema Instance Namespace URI.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(User.class);

    XMLInputFactory xif = XMLInputFactory.newInstance();
    FileInputStream fis = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(fis);
    xsr.nextTag();/*  w  w  w. j a va2s .  co m*/
    String noNamespaceSchemaLocation = xsr.getAttributeValue(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI,
            "noNamespaceSchemaLocation");
    System.out.println(noNamespaceSchemaLocation);

    Unmarshaller um = context.createUnmarshaller();
    User response = (User) um.unmarshal(xsr);
}

From source file:Main.java

/**
 * Sets the namespace to specific element.
 *
 * @param element//from  ww w  .  j a  v  a  2 s .c o m
 *          the element to set
 * @param namespace
 *          the namespace to set
 * @param schemaLocation
 *          the XML schema file location URI
 */
public static void setNamespace(Element element, String namespace, String schemaLocation) {
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE, namespace);
    element.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLNS_XSI,
            XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    element.setAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XSI_SCHEMA_LOCATION, schemaLocation);
}

From source file:Main.java

/**
 * Tests if an element is nil./*  w  w w.  j a  v  a2  s .co m*/
 * @param element the element to test
 * @return <tt>true</tt> if the element is nil
 */
public static boolean isNil(Element element) {
    String nil = element.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "nil");
    return "true".equals(nil) || "1".equals(nil);
}

From source file:com.amalto.core.storage.record.ViewSearchResultsWriter.java

@Override
public void write(DataRecord record, Writer writer) throws IOException {
    writer.write("<result xmlns:metadata=\"" + DataRecordReader.METADATA_NAMESPACE + "\"" //$NON-NLS-1$ //$NON-NLS-2$
            + " xmlns:xsi=\"" + XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
    for (FieldMetadata fieldMetadata : record.getSetFields()) {
        Object value = record.get(fieldMetadata);
        String name = fieldMetadata.getName();
        if (value != null) {
            writer.append("\t<").append(name).append(">"); //$NON-NLS-1$ //$NON-NLS-2$
            processValue(writer, fieldMetadata, value);
            writer.append("</").append(name).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$
        } else {/*from   ww w. j a v  a2 s  .  c  o  m*/
            writer.append("\t<").append(name).append("/>\n"); //$NON-NLS-1$//$NON-NLS-2$
        }
    }
    writer.append("</result>"); //$NON-NLS-1$
    writer.flush();
}

From source file:com.evolveum.midpoint.util.UglyHacks.java

public static String forceXsiNsDeclaration(String originalXml) {
    int iOpeningBracket = -1;
    while (true) {
        iOpeningBracket = originalXml.indexOf('<', iOpeningBracket + 1);
        if (iOpeningBracket < 0) {
            // No element ?
            return originalXml;
        }/*from w ww .  j av  a2s  . c o  m*/
        if (Character.isLetter(originalXml.charAt(iOpeningBracket + 1))) {
            break;
        }
        // Processing instruction, skip it
    }
    int iClosingBracket = originalXml.indexOf('>', iOpeningBracket);
    if (iClosingBracket < 0) {
        // Element not closed?
        return originalXml;
    }
    String firstElement = originalXml.substring(iOpeningBracket, iClosingBracket);
    // Not perfect, but should be good enough. All this is a hack anyway
    if (firstElement.indexOf("xmlns:xsi") >= 0) {
        // Already has xsi declaration
        return originalXml;
    }
    int iEndOfElementName = iOpeningBracket;
    while (iEndOfElementName < iClosingBracket) {
        char ch = originalXml.charAt(iEndOfElementName);
        if (ch == ' ' || ch == '>') {
            break;
        }
        iEndOfElementName++;
    }
    StringBuilder sb = new StringBuilder();
    sb.append(originalXml.substring(0, iEndOfElementName));
    sb.append(" xmlns:xsi='");
    sb.append(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    sb.append("'");
    sb.append(originalXml.substring(iEndOfElementName));
    return sb.toString();
}

From source file:com.amalto.core.history.accessor.AttributeAccessor.java

private Attr createAttribute(Node parentNode, Document domDocument) {
    // Ensure xsi prefix is declared
    if (attributeName.indexOf(':') > 0) {
        String attributePrefix = StringUtils.substringBefore(attributeName, ":"); //$NON-NLS-1$
        String namespaceURI = domDocument.lookupNamespaceURI(attributePrefix);
        if (namespaceURI == null) {
            if ("xsi".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); //$NON-NLS-1$
            } else if ("tmdm".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:tmdm", SkipAttributeDocumentBuilder.TALEND_NAMESPACE); //$NON-NLS-1$
            } else {
                throw new IllegalArgumentException("Unrecognized attribute prefix: '" + attributePrefix + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }/*from  w  w w  .j av a 2 s  .  co m*/
        }
    }
    QName qName = getQName(domDocument);
    Attr newAttribute = domDocument.createAttributeNS(qName.getNamespaceURI(), qName.getLocalPart());
    String prefix = qName.getPrefix();
    newAttribute.setPrefix(prefix);
    parentNode.getAttributes().setNamedItemNS(newAttribute);
    return newAttribute;
}

From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityXmlParser.java

/**
 * Constructs a new activity XML string parser.
 *
 * @throws ParserConfigurationException//from   ww w  .  jav  a  2s .  co  m
 *             if any errors configuring the parser
 */
public ActivityXmlParser() throws ParserConfigurationException {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    builder = domFactory.newDocumentBuilder();
    xPath = StreamsXMLUtils.getStreamsXPath();

    if (namespaces == null) {
        if (xPath.getNamespaceContext() instanceof NamespaceMap) {
            namespaces = (NamespaceMap) xPath.getNamespaceContext();
        } else {
            namespaces = new NamespaceMap();
            xPath.setNamespaceContext(namespaces);
        }
    }

    namespaces.addPrefixUriMapping(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    namespaces.addPrefixUriMapping("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); // NON-NLS
}

From source file:com.amalto.core.storage.record.XmlDOMDataRecordReader.java

private void _read(MetadataRepository repository, DataRecord dataRecord, ComplexTypeMetadata type,
        Element element) {/*from   ww  w  .  j a  va 2  s.  c  om*/
    // TODO Don't use getChildNodes() but getNextSibling() calls (but cause regressions -> see TMDM-5410).
    String tagName = element.getTagName();
    NodeList children = element.getChildNodes();
    NamedNodeMap attributes = element.getAttributes();
    for (int i = 0; i < attributes.getLength(); i++) {
        Node attribute = attributes.item(i);
        if (!type.hasField(attribute.getNodeName())) {
            continue;
        }
        FieldMetadata field = type.getField(attribute.getNodeName());
        dataRecord.set(field, StorageMetadataUtils.convert(attribute.getNodeValue(), field));
    }
    for (int i = 0; i < children.getLength(); i++) {
        Node currentChild = children.item(i);
        if (currentChild instanceof Element) {
            Element child = (Element) currentChild;
            if (METADATA_NAMESPACE.equals(child.getNamespaceURI())) {
                DataRecordMetadataHelper.setMetadataValue(dataRecord.getRecordMetadata(), child.getLocalName(),
                        child.getTextContent());
            } else {
                if (!type.hasField(child.getTagName())) {
                    continue;
                }
                FieldMetadata field = type.getField(child.getTagName());
                if (field.getType() instanceof ContainedComplexTypeMetadata) {
                    ComplexTypeMetadata containedType = (ComplexTypeMetadata) field.getType();
                    String xsiType = child.getAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); //$NON-NLS-1$
                    if (xsiType.startsWith("java:")) { //$NON-NLS-1$
                        // Special format for 'java:' type names (used in Castor XML to indicate actual class name)
                        xsiType = ClassRepository.format(StringUtils
                                .substringAfterLast(StringUtils.substringAfter(xsiType, "java:"), ".")); //$NON-NLS-1$ //$NON-NLS-2$
                    }
                    if (!xsiType.isEmpty()) {
                        for (ComplexTypeMetadata subType : containedType.getSubTypes()) {
                            if (subType.getName().equals(xsiType)) {
                                containedType = subType;
                                break;
                            }
                        }
                    }
                    DataRecord containedRecord = new DataRecord(containedType,
                            UnsupportedDataRecordMetadata.INSTANCE);
                    dataRecord.set(field, containedRecord);
                    _read(repository, containedRecord, containedType, child);
                } else if (ClassRepository.EMBEDDED_XML.equals(field.getType().getName())) {
                    try {
                        dataRecord.set(field, Util.nodeToString(element));
                    } catch (TransformerException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    _read(repository, dataRecord, type, child);
                }
            }
        } else if (currentChild instanceof Text) {
            StringBuilder builder = new StringBuilder();
            for (int j = 0; j < element.getChildNodes().getLength(); j++) {
                String nodeValue = element.getChildNodes().item(j).getNodeValue();
                if (nodeValue != null) {
                    builder.append(nodeValue.trim());
                }
            }
            String textContent = builder.toString();
            if (!textContent.isEmpty()) {
                FieldMetadata field = type.getField(tagName);
                if (field instanceof ReferenceFieldMetadata) {
                    ComplexTypeMetadata actualType = ((ReferenceFieldMetadata) field).getReferencedType();
                    String mdmType = element.getAttributeNS(SkipAttributeDocumentBuilder.TALEND_NAMESPACE,
                            "type"); //$NON-NLS-1$
                    if (!mdmType.isEmpty()) {
                        actualType = repository.getComplexType(mdmType);
                    }
                    if (actualType == null) {
                        throw new IllegalArgumentException("Type '" + mdmType + "' does not exist.");
                    }
                    dataRecord.set(field, StorageMetadataUtils.convert(textContent, field, actualType));
                } else {
                    dataRecord.set(field, StorageMetadataUtils.convert(textContent, field, type));
                }
            }
        }
    }
}

From source file:net.sf.firemox.xml.magic.Oracle2XmlNoRules.java

/**
 * @param oracleFile// w ww  .  j  ava  2  s .com
 *          the oracle format text file.
 * @param destinationDir
 *          the directory where built card will be placed.
 * @param recycledDir
 *          the directory where already built card are placed.
 */
public void serialize(File oracleFile, File destinationDir, File recycledDir) {
    if (!oracleFile.exists() || !oracleFile.isFile()) {
        System.err.println("The file '" + oracleFile + "' does not exist");
        System.exit(-1);
        return;
    }

    if (!destinationDir.exists() || !destinationDir.isDirectory()) {
        System.err.println("The destination directory '" + destinationDir + "' does not exist");
        System.exit(-1);
        return;
    }
    final StringBuilder cardText = new StringBuilder();
    int nbCard = 0;
    MToolKit.tbsName = "norules-mtg";
    try {
        final BufferedReader in = new BufferedReader(new FileReader(oracleFile));
        PrintWriter out = null;
        while (nbCard < MAXI) {
            String line = in.readLine();
            if (line == null)
                break;
            String cardName = line.trim();
            if (cardName.length() == 0) {
                continue;
            }

            // a new card starts
            String fileName = MToolKit.getExactKeyName(cardName) + ".xml";
            if (new File(recycledDir, fileName).exists()) {
                if (UPDATE_CARD) {
                    File patchFile = MToolKit.getFile("tbs/norules-mtg/recycled/" + fileName);
                    File tempFile = File.createTempFile(fileName, "temp");
                    FileUtils.copyFile(patchFile, tempFile);
                    final BufferedReader inExist = new BufferedReader(new FileReader(tempFile));
                    final PrintWriter outExist = new PrintWriter(new FileOutputStream(patchFile));
                    String lineExist = null;
                    boolean found = false;
                    while ((lineExist = inExist.readLine()) != null) {
                        if (!found && lineExist.contains("name=\"")) {
                            lineExist = lineExist.substring(0, lineExist.indexOf("name=\"")) + "name=\""
                                    + cardName + lineExist.substring(lineExist.indexOf("\"",
                                            lineExist.indexOf("name=\"") + "name=\"".length() + 2));
                            found = true;
                        }
                        outExist.println(lineExist);
                    }
                    IOUtils.closeQuietly(inExist);
                    IOUtils.closeQuietly(outExist);

                    if (!found) {
                        System.err.println(">> Error patching card '" + cardName + "'");
                        // } else {
                        // patching card
                    }
                }
                skipCard(in);
                continue;
            }

            out = new PrintWriter(new FileOutputStream(new File(destinationDir, fileName)));

            // record the card text
            cardText.setLength(0);
            cardText.append("<!--\n\t");

            line = in.readLine().trim().replaceAll("\\(.*\\)", "").toLowerCase();
            cardText.append('\t').append(line).append("\n");

            while (true) {
                line = in.readLine();
                if (line == null || line.length() == 0)
                    break;
                line = line.replaceAll("--", "");
                cardText.append('\t').append(line).append('\n');
            }

            cardText.append(" -->");
            out.println("<?xml version='1.0'?>");

            out.print("<card xmlns='");
            out.println(IdConst.NAME_SPACE + "'");
            out.print("\txmlns:xsi='");
            out.print(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
            out.println("'");
            out.print("\txsi:schemaLocation='");
            out.println(IdConst.NAME_SPACE + " ../../" + IdConst.TBS_XSD + "'");
            out.println("\tname=\"" + cardName + "\">");
            out.println("<rules-author-comment>riclas</rules-author-comment>\n");
            out.println(cardText.toString());
            out.println("</card>");
            IOUtils.closeQuietly(out);
            nbCard++;
        }
    } catch (FileNotFoundException e) {
        System.err.println("Error openning file '" + oracleFile + "' : " + e);
    } catch (IOException e) {
        System.err.println("IOError reading file '" + oracleFile + "' : " + e);
    }
    System.out.println("Success Parsing : " + nbCard + " card(s)");
}

From source file:com.amalto.core.history.accessor.UnaryFieldAccessor.java

@Override
public String getActualType() {
    Attr type = ((Element) getNode()).getAttributeNodeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); //$NON-NLS-1$
    if (type == null) {
        type = ((Element) getNode()).getAttributeNodeNS(SkipAttributeDocumentBuilder.TALEND_NAMESPACE, "type"); //$NON-NLS-1$
    }/*  w ww  .  j av  a 2  s.c  o m*/
    if (type == null) {
        return StringUtils.EMPTY;
    } else {
        return type.getValue();
    }
}