Search in sources :

Example 21 with SAXBuilder

use of org.jdom.input.SAXBuilder in project gora by apache.

the class HBaseStore method readMapping.

@SuppressWarnings("unchecked")
private HBaseMapping readMapping(String filename) throws IOException {
    HBaseMappingBuilder mappingBuilder = new HBaseMappingBuilder();
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(getClass().getClassLoader().getResourceAsStream(filename));
        Element root = doc.getRootElement();
        List<Element> tableElements = root.getChildren("table");
        for (Element tableElement : tableElements) {
            String tableName = tableElement.getAttributeValue("name");
            List<Element> fieldElements = tableElement.getChildren("family");
            for (Element fieldElement : fieldElements) {
                String familyName = fieldElement.getAttributeValue("name");
                String compression = fieldElement.getAttributeValue("compression");
                String blockCache = fieldElement.getAttributeValue("blockCache");
                String blockSize = fieldElement.getAttributeValue("blockSize");
                String bloomFilter = fieldElement.getAttributeValue("bloomFilter");
                String maxVersions = fieldElement.getAttributeValue("maxVersions");
                String timeToLive = fieldElement.getAttributeValue("timeToLive");
                String inMemory = fieldElement.getAttributeValue("inMemory");
                mappingBuilder.addFamilyProps(tableName, familyName, compression, blockCache, blockSize, bloomFilter, maxVersions, timeToLive, inMemory);
            }
        }
        List<Element> classElements = root.getChildren("class");
        boolean keyClassMatches = false;
        for (Element classElement : classElements) {
            if (classElement.getAttributeValue("keyClass").equals(keyClass.getCanonicalName()) && classElement.getAttributeValue("name").equals(persistentClass.getCanonicalName())) {
                LOG.debug("Keyclass and nameclass match.");
                keyClassMatches = true;
                String tableNameFromMapping = classElement.getAttributeValue("table");
                String tableName = getSchemaName(tableNameFromMapping, persistentClass);
                //tableNameFromMapping could be null here
                if (!tableName.equals(tableNameFromMapping)) {
                    //TODO this might not be the desired behavior as the user might have actually made a mistake.
                    LOG.warn("Mismatching schema's names. Mappingfile schema: '{}'. PersistentClass schema's name: '{}'. Assuming they are the same.", tableNameFromMapping, tableName);
                    if (tableNameFromMapping != null) {
                        mappingBuilder.renameTable(tableNameFromMapping, tableName);
                    }
                }
                mappingBuilder.setTableName(tableName);
                List<Element> fields = classElement.getChildren("field");
                for (Element field : fields) {
                    String fieldName = field.getAttributeValue("name");
                    String family = field.getAttributeValue("family");
                    String qualifier = field.getAttributeValue("qualifier");
                    mappingBuilder.addField(fieldName, family, qualifier);
                    mappingBuilder.addColumnFamily(tableName, family);
                }
                //do not continue on other class definitions
                break;
            }
        }
        if (!keyClassMatches)
            LOG.error("KeyClass in gora-hbase-mapping is not the same as the one in the databean.");
    } catch (MalformedURLException ex) {
        LOG.error("Error while trying to read the mapping file {}. " + "Expected to be in the classpath " + "(ClassLoader#getResource(java.lang.String)).", filename);
        LOG.error("Actual classpath = {}", Arrays.asList(((URLClassLoader) getClass().getClassLoader()).getURLs()));
        throw ex;
    } catch (IOException ex) {
        LOG.error(ex.getMessage(), ex);
        throw ex;
    } catch (Exception ex) {
        LOG.error(ex.getMessage(), ex);
        throw new IOException(ex);
    }
    return mappingBuilder.build();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) MalformedURLException(java.net.MalformedURLException) Element(org.jdom.Element) IOException(java.io.IOException) HBaseMappingBuilder(org.apache.gora.hbase.store.HBaseMapping.HBaseMappingBuilder) Document(org.jdom.Document) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 22 with SAXBuilder

use of org.jdom.input.SAXBuilder in project intellij-community by JetBrains.

the class PlainTextFormatter method convert.

@Override
public void convert(@NotNull final String rawDataDirectoryPath, @Nullable final String outputPath, @NotNull final Map<String, Tools> tools, @NotNull final List<File> inspectionsResults) throws ConversionException {
    final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
    final URL descrExtractorXsltUrl = getClass().getResource("description-text.xsl");
    final Source xslSource;
    final Transformer transformer;
    try {
        xslSource = new StreamSource(URLUtil.openStream(descrExtractorXsltUrl));
        transformer = transformerFactory.newTransformer(xslSource);
    } catch (IOException e) {
        throw new ConversionException("Cannot find inspection descriptions converter.");
    } catch (TransformerConfigurationException e) {
        throw new ConversionException("Fail to load inspection descriptions converter.");
    }
    // write to file/stdout:
    final Writer w;
    if (outputPath != null) {
        final File outputFile = new File(outputPath);
        try {
            w = new FileWriter(outputFile);
        } catch (IOException e) {
            throw new ConversionException("Cannot edit file: " + outputFile.getPath());
        }
    } else {
        w = new PrintWriter(System.out);
    }
    try {
        for (File inspectionData : inspectionsResults) {
            if (inspectionData.isDirectory()) {
                warn("Folder isn't expected here: " + inspectionData.getName());
                continue;
            }
            final String fileNameWithoutExt = FileUtil.getNameWithoutExtension(inspectionData);
            if (InspectionApplication.DESCRIPTIONS.equals(fileNameWithoutExt)) {
                continue;
            }
            InspectionToolWrapper toolWrapper = tools.get(fileNameWithoutExt).getTool();
            // Tool name and group
            w.append(getToolPresentableName(toolWrapper)).append("\n");
            // Description is HTML based, need to be converted in plain text
            writeInspectionDescription(w, toolWrapper, transformer);
            // separator before file list
            w.append("\n");
            // parse xml and output results
            final SAXBuilder builder = new SAXBuilder();
            try {
                final Document doc = builder.build(inspectionData);
                final Element root = doc.getRootElement();
                final List problems = root.getChildren(PROBLEM_ELEMENT);
                // let's count max file path & line_number length to align problem descriptions
                final int maxFileColonLineLength = getMaxFileColonLineNumLength(inspectionData, toolWrapper, problems);
                for (Object problem : problems) {
                    // Format:
                    //   file_path:line_num   [severity] problem description
                    final Element fileElement = ((Element) problem).getChild(FILE_ELEMENT);
                    final String filePath = getPath(fileElement);
                    // skip suppressed results
                    if (resultsIgnored(inspectionData, toolWrapper)) {
                        continue;
                    }
                    final Element lineElement = ((Element) problem).getChild(LINE_ELEMENT);
                    final Element problemDescrElement = ((Element) problem).getChild(DESCRIPTION_ELEMENT);
                    final String severity = ((Element) problem).getChild(PROBLEM_CLASS_ELEMENT).getAttributeValue(SEVERITY_ATTRIBUTE);
                    final String fileLineNum = lineElement.getText();
                    w.append("  ").append(filePath).append(':').append(fileLineNum);
                    // align descriptions
                    for (int i = maxFileColonLineLength - 1 - filePath.length() - fileLineNum.length() + 4; i >= 0; i--) {
                        w.append(' ');
                    }
                    w.append("[").append(severity).append("] ");
                    w.append(problemDescrElement.getText()).append('\n');
                }
            } catch (JDOMException e) {
                throw new ConversionException("Unknown results format, file = " + inspectionData.getPath() + ". Error: " + e.getMessage());
            }
            // separator between neighbour inspections
            w.append("\n");
        }
    } catch (IOException e) {
        throw new ConversionException("Cannot write inspection results: " + e.getMessage());
    } finally {
        if (w != null) {
            try {
                w.close();
            } catch (IOException e) {
                warn("Cannot save inspection results: " + e.getMessage());
            }
        }
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.jdom.Element) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) List(java.util.List)

Example 23 with SAXBuilder

use of org.jdom.input.SAXBuilder in project libresonic by Libresonic.

the class JAXBWriter method getRESTProtocolVersion.

private String getRESTProtocolVersion() throws Exception {
    InputStream in = null;
    try {
        in = StringUtil.class.getResourceAsStream("/libresonic-rest-api.xsd");
        Document document = new SAXBuilder().build(in);
        Attribute version = document.getRootElement().getAttribute("version");
        return version.getValue();
    } finally {
        IOUtils.closeQuietly(in);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Attribute(org.jdom.Attribute) InputStream(java.io.InputStream) StringUtil(org.libresonic.player.util.StringUtil) Document(org.jdom.Document)

Example 24 with SAXBuilder

use of org.jdom.input.SAXBuilder in project yamcs-studio by yamcs.

the class FileUtil method loadXMLFile.

/**
 * Load the root element of an XML file. The element is a JDOM Element.
 *
 * @param filePath
 *            path of the file. It can be an absolute path or a relative path to the OPI that contains the specified
 *            widget. If it is an absolute path, it can be either<br>
 *            a workspace path such as <code>/BOY Examples/Scripts/myfile.xml</code><br>
 *            a local file system path such as <code>C:\myfile.xml</code><br>
 *            or an URL path such as <code>http://mysite.com/myfile.xml</code>.
 * @param widget
 *            a widget in the OPI, which is used to provide relative path reference. It can be null if the path is
 *            an absolute path.
 * @return root element of the XML file.
 * @throws Exception
 *             if the file does not exist or is not a correct XML file.
 */
public static Element loadXMLFile(final String filePath, final AbstractBaseEditPart widget) throws Exception {
    final IPath path = buildAbsolutePath(filePath, widget);
    SAXBuilder saxBuilder = new SAXBuilder();
    saxBuilder.setEntityResolver(new CssEntityResolver());
    File file = ResourceUtil.getFile(path);
    final Document doc;
    if (file == null) {
        InputStream inputStream = ResourceUtil.pathToInputStream(path);
        doc = saxBuilder.build(inputStream);
        inputStream.close();
    } else {
        doc = saxBuilder.build(file);
    }
    return doc.getRootElement();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) IPath(org.eclipse.core.runtime.IPath) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(org.jdom.Document) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 25 with SAXBuilder

use of org.jdom.input.SAXBuilder in project yamcs-studio by yamcs.

the class XMLUtil method inputStreamToXML.

private static Element inputStreamToXML(InputStream stream) throws JDOMException, IOException {
    SAXBuilder saxBuilder = LineAwareXMLParser.createBuilder();
    Document doc = saxBuilder.build(stream);
    Element root = doc.getRootElement();
    return root;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) LineAwareElement(org.csstudio.opibuilder.persistence.LineAwareXMLParser.LineAwareElement) Element(org.jdom.Element) Document(org.jdom.Document)

Aggregations

SAXBuilder (org.jdom.input.SAXBuilder)145 Document (org.jdom.Document)109 Element (org.jdom.Element)84 IOException (java.io.IOException)60 JDOMException (org.jdom.JDOMException)50 StringReader (java.io.StringReader)35 InputStream (java.io.InputStream)29 File (java.io.File)18 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)13 ArrayList (java.util.ArrayList)12 XPath (org.jdom.xpath.XPath)11 HttpMethod (org.apache.commons.httpclient.HttpMethod)10 XMLOutputter (org.jdom.output.XMLOutputter)10 URL (java.net.URL)9 List (java.util.List)8 GoraException (org.apache.gora.util.GoraException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 Namespace (org.jdom.Namespace)6 StringWriter (java.io.StringWriter)5