Example usage for org.xml.sax.helpers AttributesImpl addAttribute

List of usage examples for org.xml.sax.helpers AttributesImpl addAttribute

Introduction

In this page you can find the example usage for org.xml.sax.helpers AttributesImpl addAttribute.

Prototype

public void addAttribute(String uri, String localName, String qName, String type, String value) 

Source Link

Document

Add an attribute to the end of the list.

Usage

From source file:it.polito.tellmefirst.web.rest.interfaces.TextInterface.java

private String produceXML(String title) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;//  w  ww .j  ava  2 s  . c  o  m
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "title", "", title);
        hd.startElement("", "", "Enhancement", null);
        hd.startElement("", "", "Result", atts);
        hd.endElement("", "", "Result");
        hd.endElement("", "", "Enhancement");
        hd.endDocument();
        xml = out.toString("utf-8");
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);

    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.EpubInterface.java

private String produceXML(ArrayList<String[]> topics) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;/* w  w  w. j a  v  a2s  .  c om*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "service", "", "ClassifyEpub");
        hd.startElement("", "", "Classification", atts);
        int i = 0;
        for (String[] topic : topics) {
            if (i == 0) {
                atts.clear();
                hd.startElement("", "", "Resources", atts);
            }
            atts.addAttribute("", "", "uri", "", topic[0]);
            atts.addAttribute("", "", "label", "", topic[1]);
            atts.addAttribute("", "", "title", "", topic[2]);
            atts.addAttribute("", "", "score", "", topic[3]);
            atts.addAttribute("", "", "mergedTypes", "", topic[4]);
            atts.addAttribute("", "", "image", "", topic[5]);
            hd.startElement("", "", "Resource", atts);
            hd.endElement("", "", "Resource");
            i++;
        }
        if (i > 0)
            hd.endElement("", "", "Resources");
        hd.endElement("", "", "Classification");
        hd.endDocument();
        xml = out.toString("utf-8");
        System.out.println(xml);
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.ClassifyInterface.java

private String produceXML(List<String[]> topics) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;/*from   w ww.java2s  . c  o m*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "service", "", "Classify");
        hd.startElement("", "", "Classification", atts);
        int i = 0;
        for (String[] topic : topics) {
            if (i == 0) {
                atts.clear();
                hd.startElement("", "", "Resources", atts);
            }
            atts.addAttribute("", "", "uri", "", topic[0]);
            atts.addAttribute("", "", "label", "", topic[1]);
            atts.addAttribute("", "", "title", "", topic[2]);
            atts.addAttribute("", "", "score", "", topic[3]);
            atts.addAttribute("", "", "mergedTypes", "", topic[4]);
            atts.addAttribute("", "", "image", "", ""); // We should remove this parameter in favour of Wikimedia/Wikidata API
            hd.startElement("", "", "Resource", atts);
            hd.endElement("", "", "Resource");
            i++;
        }
        if (i > 0)
            hd.endElement("", "", "Resources");
        hd.endElement("", "", "Classification");
        hd.endDocument();
        xml = out.toString("utf-8");
        System.out.println(xml);
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}

From source file:it.polito.tellmefirst.web.rest.interfaces.EpubChaptersInterface.java

private String produceXML(HashMap<String, List<String[]>> chapters) throws TMFOutputException {

    LOG.debug("[produceXML] - BEGIN");

    String xml;/*from  w w w.j  a  v a 2 s .  c om*/
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute("", "", "service", "", "ClassifyEpubChapters");
        hd.startElement("", "", "Classification", atts);
        int i = 0;
        Set set = chapters.entrySet();
        Iterator iterator = set.iterator();
        while (iterator.hasNext()) {
            Map.Entry me = (Map.Entry) iterator.next();
            atts.clear();
            if (i == 0) {
                hd.startElement("", "", "Chapters", atts);
            }
            atts.addAttribute("", "", "toc", "", me.getKey().toString());
            hd.startElement("", "", "Chapter", atts);
            atts.clear();
            ArrayList<String[]> topics = (ArrayList<String[]>) me.getValue();
            int j = 0;
            for (String[] topic : topics) {
                atts.clear();
                if (j == 0) {
                    hd.startElement("", "", "Resources", atts);
                }
                atts.addAttribute("", "", "uri", "", topic[0]);
                atts.addAttribute("", "", "label", "", topic[1]);
                atts.addAttribute("", "", "title", "", topic[2]);
                atts.addAttribute("", "", "score", "", topic[3]);
                atts.addAttribute("", "", "mergedTypes", "", topic[4]);
                atts.addAttribute("", "", "image", "", topic[5]);
                hd.startElement("", "", "Resource", atts);
                hd.endElement("", "", "Resource");
                j++;
            }
            if (j > 0)
                hd.endElement("", "", "Resources");
            hd.endElement("", "", "Chapter");
            i++;
        }
        if (i > 0)
            hd.endElement("", "", "Chapters");
        hd.endElement("", "", "Classification");
        hd.endDocument();
        xml = out.toString("utf-8");
        System.out.println(xml);
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }

    LOG.debug("[produceXML] - END");

    return xml;
}

From source file:mj.ocraptor.extraction.tika.parser.pkg.PackageParser.java

private void parseEntry(ArchiveInputStream archive, ArchiveEntry entry, EmbeddedDocumentExtractor extractor,
        XHTMLContentHandler xhtml) throws SAXException, IOException, TikaException {
    String name = entry.getName();
    if (archive.canReadEntryData(entry)) {
        Metadata entrydata = new Metadata();
        if (name != null && name.length() > 0) {
            entrydata.set(Metadata.RESOURCE_NAME_KEY, name);
            AttributesImpl attributes = new AttributesImpl();
            attributes.addAttribute("", "class", "class", "CDATA", "embedded");
            attributes.addAttribute("", "id", "id", "CDATA", name);
            xhtml.startElement("div", attributes);
            xhtml.endElement("div");

            entrydata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, name);
        }/*w ww  . j a  v  a 2  s  .c om*/
        if (extractor.shouldParseEmbedded(entrydata)) {
            // For detectors to work, we need a mark/reset supporting
            // InputStream, which ArchiveInputStream isn't, so wrap
            TemporaryResources tmp = new TemporaryResources();
            try {
                TikaInputStream tis = TikaInputStream.get(archive, tmp);
                extractor.parseEmbedded(tis, xhtml, entrydata, true);
            } finally {
                tmp.dispose();
            }
        }
    } else if (name != null && name.length() > 0) {
        xhtml.element("p", name);
    }
}

From source file:de.tudarmstadt.ukp.csniper.webapp.analysis.uima.HTMLColorMarkerConsumer.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);

    // TODO notify user that the number of arguments is not equal
    // if (markedAnnotations.length != markerColors.length) {
    // throw new IllegalArgumentException(
    // "The amount of colors and annotation types have to be equal.");
    // }// w w  w  .  j  a  v  a2s.  c  om
    typeCount = Math.min(markerColors.length, markedTypes.length);

    // build map: type->color
    colors = new HashMap<String, String>();
    for (int i = 0; i < typeCount; i++) {
        colors.put(markedTypes[i], markerColors[i]);
    }

    handler = new SAXContentHandler();
    try {
        // add stylesheet information
        handler.startDocument();
        AttributesImpl attr = new AttributesImpl();
        attr.addAttribute("", "", "id", "CDATA", outputClass);
        handler.startElement("", "div", "", attr);

        AttributesImpl style = new AttributesImpl();
        style.addAttribute("", "", "type", "CDATA", "text/css");
        handler.startElement("", "style", "", style);

        StringBuffer headerCss = new StringBuffer();
        headerCss.append("div#");
        headerCss.append(outputClass);
        headerCss.append(" span {");
        headerCss.append(" display:inline-block; padding:0 1 0 1;");
        headerCss.append(" margin:1px; border:solid 1px #FFFFFF; }");

        handler.characters(headerCss.toString().toCharArray(), 0, headerCss.toString().length());
        handler.endElement("", "style", "");
    } catch (SAXException e) {
        throw new ResourceInitializationException(e);
    }
}

From source file:de.tudarmstadt.ukp.csniper.webapp.analysis.uima.HTMLColorMarkerConsumer.java

@Override
public void process(CAS cas) throws AnalysisEngineProcessException {
    List<AnnotationFS> filtered = new ArrayList<AnnotationFS>();
    Iterator<CAS> viewIterator = cas.getViewIterator();
    while (viewIterator.hasNext()) {
        CAS view = viewIterator.next();//from   w ww  . ja va 2  s  . c o m

        for (int i = 0; i < typeCount; i++) {
            try {
                Type type = CasUtil.getType(view, markedTypes[i]);
                filtered.addAll(CasUtil.select(view, type));
            } catch (IllegalArgumentException e) {
                // TODO at the moment, don't do anything when a type is not found
            }
        }

        try {
            for (AnnotationFS root : CasUtil.select(view, rootType)) {
                for (AnnotationFS token : CasUtil.selectCovered(view, tokenType, root)) {
                    for (AnnotationFS a : getAnnotationsBeginningAt(token.getBegin(), filtered)) {
                        String color = "background:" + colors.get(a.getType().getName()) + "; color:#DDDDDD;";
                        AttributesImpl attr = new AttributesImpl();
                        attr.addAttribute("", "", "style", "CDATA", color);
                        handler.startElement("", "span", "", attr);
                    }
                    char[] t = token.getCoveredText().toCharArray();
                    handler.characters(t, 0, t.length);
                    for (AnnotationFS a : getAnnotationsEndingAt(token.getEnd(), filtered)) {
                        handler.endElement("", "span", "");
                    }
                    handler.characters(new char[] { ' ' }, 0, 1);
                }
                // newline for each sentence
                handler.startElement("", "br", "", new AttributesImpl());
                handler.endElement("", "br", "");
            }
        } catch (SAXException e) {
            throw new AnalysisEngineProcessException(e);
        }
    }
}

From source file:com.google.enterprise.connector.sharepoint.wsclient.mock.MockClientFactory.java

/**
 * Creates a new {@link ListState}./* w  w w . j ava 2s .  c om*/
 *
 * @param webUrl The URL of the parent web
 * @param listName The name of the new list
 * @param ws The web state of the parent web
 * @param feedType The feed type of the new list
 * @param fixedId If true created an ID from the list URL; otherwise
 *          a new random ID is created
 * @return a new {@link ListState}
 */
protected ListState createListState(final String webUrl, final String listName, final WebState ws,
        final FeedType feedType, final Boolean fixedId) throws SharepointException {
    String listUrl = webUrl + "/" + listName;
    String listId = generateId(listUrl, fixedId);

    AttributesImpl attr = new AttributesImpl();
    attr.addAttribute("", "", SPConstants.STATE_ID, "", listId);
    attr.addAttribute("", "", SPConstants.STATE_BIGGESTID, "", "0");
    attr.addAttribute("", "", SPConstants.STATE_TYPE, "", SPConstants.DOC_LIB);
    attr.addAttribute("", "", SPConstants.STATE_URL, "", listUrl);
    attr.addAttribute("", "", SPConstants.STATE_LASTMODIFIED, "", Util.formatDate(Calendar.getInstance()));
    attr.addAttribute("", "", SPConstants.STATE_CHANGETOKEN, "", "mock-change-token");
    return ListState.loadStateFromXML(ws, attr, feedType);
}

From source file:org.orbeon.oxf.processor.JFreeChartProcessor.java

public ProcessorOutput createOutput(String name) {
    return new ProcessorOutputImpl(JFreeChartProcessor.this, name) {
        public void readImpl(PipelineContext context, XMLReceiver xmlReceiver) {
            JFreeChartSerializer.ChartConfig chartConfig = readChartConfig(context);
            ChartInfo info = createChart(context, chartConfig);
            try {
                xmlReceiver.startDocument();
                xmlReceiver.startElement("", "chart-info", "chart-info", new AttributesImpl());

                xmlReceiver.startElement("", "file", "file", new AttributesImpl());
                xmlReceiver.characters(info.file.toCharArray(), 0, info.file.length());
                xmlReceiver.endElement("", "file", "file");

                if (chartConfig.getMap() != null) {
                    AttributesImpl atts = new AttributesImpl();

                    atts.addAttribute("", "name", "name", "CDATA", chartConfig.getMap());
                    xmlReceiver.startElement("", "map", "map", atts);

                    EntityCollection entities = info.getInfo().getEntityCollection();

                    Iterator iterator = entities.iterator();
                    while (iterator.hasNext()) {
                        ChartEntity entity = (ChartEntity) iterator.next();
                        AttributesImpl attr = new AttributesImpl();
                        attr.addAttribute("", "shape", "shape", "CDATA", entity.getShapeType());
                        attr.addAttribute("", "coords", "coords", "CDATA", entity.getShapeCoords());

                        if (entity.getURLText() != null && !entity.getURLText().equals("")) {
                            attr.addAttribute("", "href", "href", "CDATA", entity.getURLText());
                        }/*from  w  ww .j  a  va 2  s  .c o m*/
                        if (entity.getToolTipText() != null && !entity.getToolTipText().equals("")) {
                            attr.addAttribute("", "title", "title", "CDATA", entity.getToolTipText());

                        }
                        xmlReceiver.startElement("", "area", "area", attr);
                        xmlReceiver.endElement("", "area", "area");
                    }

                    xmlReceiver.endElement("", "map", "map");
                }
                xmlReceiver.endElement("", "chart-info", "chart-info");
                xmlReceiver.endDocument();

            } catch (SAXException e) {
                throw new OXFException(e);
            }
        }
    };
}

From source file:it.polito.tellmefirst.web.rest.interfaces.MapInterface.java

private String produceXML(String[] coordinates) throws TMFOutputException {
    LOG.debug("[produceXML] - BEGIN");
    String xml;/*from   w ww  . ja  v  a2 s.  com*/
    String res1 = "";
    String res2 = "";
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        TransformerHandler hd = initXMLDoc(out);
        AttributesImpl atts = new AttributesImpl();
        if (coordinates[0] != null) {
            res1 = coordinates[0];
        }
        if (coordinates[1] != null) {
            res2 = coordinates[1];
        }
        atts.addAttribute("", "", "lat", "", res1);
        atts.addAttribute("", "", "long", "", res2);
        hd.startElement("", "", "Enhancement", null);
        hd.startElement("", "", "Map", atts);
        hd.endElement("", "", "Map");
        hd.endElement("", "", "Enhancement");
        hd.endDocument();
        xml = out.toString("utf-8");
    } catch (Exception e) {
        throw new TMFOutputException("Error creating XML output.", e);
    }
    LOG.debug("[produceXML] - END");
    return xml;
}