Search in sources :

Example 96 with Formatter

use of java.util.Formatter in project mkgmap by openstreetmap.

the class SyntaxException method getMessage.

public String getMessage() {
    Formatter fmt = new Formatter();
    fmt.format("Error: ");
    if (fileName != null)
        fmt.format("(%s:%d): ", fileName, lineNumber);
    fmt.format("%s", super.getMessage());
    return fmt.toString();
}
Also used : Formatter(java.util.Formatter)

Example 97 with Formatter

use of java.util.Formatter in project mkgmap by openstreetmap.

the class GType method toString.

public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter fmt = new Formatter(sb);
    sb.append('[');
    fmt.format("%#x", type);
    if (maxLevel == -1) {
        if (maxResolution == 24)
            fmt.format(" resolution %d", minResolution);
        else
            fmt.format(" resolution %d-%d", maxResolution, minResolution);
    } else {
        if (minLevel == 0)
            fmt.format(" level %d", maxLevel);
        else
            fmt.format(" level %d-%d", minLevel, maxLevel);
    }
    if (hasRoadAttribute)
        fmt.format(" road_class=%d road_speed=%d", roadClass, roadSpeed);
    if (continueSearch)
        fmt.format(" continue");
    if (propogateActionsOnContinue)
        fmt.format(" propagate");
    sb.append(']');
    String res = sb.toString();
    fmt.close();
    return res;
}
Also used : Formatter(java.util.Formatter)

Example 98 with Formatter

use of java.util.Formatter in project xwiki-platform by xwiki.

the class ModelFactory method toRestObject.

public Object toRestObject(URI baseUri, Document doc, BaseObject xwikiObject, boolean useVersion, Boolean withPrettyNames) {
    Object object = this.objectFactory.createObject();
    fillObjectSummary(object, doc, xwikiObject, withPrettyNames);
    XWikiContext xwikiContext = this.xcontextProvider.get();
    BaseClass xwikiClass = xwikiObject.getXClass(xwikiContext);
    for (java.lang.Object propertyClassObject : xwikiClass.getProperties()) {
        com.xpn.xwiki.objects.classes.PropertyClass propertyClass = (com.xpn.xwiki.objects.classes.PropertyClass) propertyClassObject;
        Property property = this.objectFactory.createProperty();
        for (java.lang.Object o : propertyClass.getProperties()) {
            BaseProperty baseProperty = (BaseProperty) o;
            Attribute attribute = this.objectFactory.createAttribute();
            attribute.setName(baseProperty.getName());
            /* Check for null values in order to prevent NPEs */
            if (baseProperty.getValue() != null) {
                attribute.setValue(baseProperty.getValue().toString());
            } else {
                attribute.setValue("");
            }
            property.getAttributes().add(attribute);
        }
        if (propertyClass instanceof ListClass) {
            ListClass listClass = (ListClass) propertyClass;
            List allowedValueList = listClass.getList(xwikiContext);
            if (!allowedValueList.isEmpty()) {
                Formatter f = new Formatter();
                for (int i = 0; i < allowedValueList.size(); i++) {
                    if (i != allowedValueList.size() - 1) {
                        f.format("%s,", allowedValueList.get(i).toString());
                    } else {
                        f.format("%s", allowedValueList.get(i).toString());
                    }
                }
                Attribute attribute = this.objectFactory.createAttribute();
                attribute.setName(Constants.ALLOWED_VALUES_ATTRIBUTE_NAME);
                attribute.setValue(f.toString());
                property.getAttributes().add(attribute);
            }
        }
        property.setName(propertyClass.getName());
        property.setType(propertyClass.getClassType());
        try {
            property.setValue(serializePropertyValue(xwikiObject.get(propertyClass.getName())));
        } catch (XWikiException e) {
        // Should never happen
        }
        String propertyUri;
        if (useVersion) {
            propertyUri = Utils.createURI(baseUri, ObjectPropertyAtPageVersionResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), doc.getVersion(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
        } else {
            propertyUri = Utils.createURI(baseUri, ObjectPropertyResource.class, doc.getWiki(), Utils.getSpacesFromSpaceId(doc.getSpace()), doc.getName(), xwikiObject.getClassName(), xwikiObject.getNumber(), propertyClass.getName()).toString();
        }
        Link propertyLink = this.objectFactory.createLink();
        propertyLink.setHref(propertyUri);
        propertyLink.setRel(Relations.SELF);
        property.getLinks().add(propertyLink);
        object.getProperties().add(property);
    }
    Link objectLink = getObjectLink(this.objectFactory, baseUri, doc, xwikiObject, useVersion, Relations.SELF);
    object.getLinks().add(objectLink);
    return object;
}
Also used : Attribute(org.xwiki.rest.model.jaxb.Attribute) Formatter(java.util.Formatter) XWikiContext(com.xpn.xwiki.XWikiContext) PropertyClass(com.xpn.xwiki.api.PropertyClass) ListClass(com.xpn.xwiki.objects.classes.ListClass) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) BaseObject(com.xpn.xwiki.objects.BaseObject) Object(org.xwiki.rest.model.jaxb.Object) List(java.util.List) ArrayList(java.util.ArrayList) BaseProperty(com.xpn.xwiki.objects.BaseProperty) Property(org.xwiki.rest.model.jaxb.Property) BaseProperty(com.xpn.xwiki.objects.BaseProperty) XWikiException(com.xpn.xwiki.XWikiException) Link(org.xwiki.rest.model.jaxb.Link)

Example 99 with Formatter

use of java.util.Formatter in project xwiki-platform by xwiki.

the class BaseAttachmentsResource method getAttachments.

/**
 * Retrieves the attachments by filtering them.
 *
 * @param wikiName The virtual wiki.
 * @param name Name filter (include only attachments that matches this name)
 * @param page Page filter (include only attachments are attached to a page matches this string)
 * @param space Space filter (include only attachments are attached to a page in a space matching this string)
 * @param author Author filter (include only attachments from an author who matches this string)
 * @param types A comma separated list of string that will be matched against the actual mime type of the
 *            attachments.
 * @return The list of the retrieved attachments.
 */
public Attachments getAttachments(String wikiName, String name, String page, String space, String author, String types, Integer start, Integer number, Boolean withPrettyNames) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    Attachments attachments = objectFactory.createAttachments();
    /* This try is just needed for executing the finally clause. */
    try {
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        Map<String, String> filters = new HashMap<String, String>();
        if (!name.equals("")) {
            filters.put("name", name);
        }
        if (!page.equals("")) {
            filters.put("page", name);
        }
        if (!space.equals("")) {
            filters.put("space", Utils.getLocalSpaceId(parseSpaceSegments(space)));
        }
        if (!author.equals("")) {
            filters.put("author", author);
        }
        /* Build the query */
        Formatter f = new Formatter();
        f.format("select doc.space, doc.name, doc.version, attachment from XWikiDocument as doc," + " XWikiAttachment as attachment where (attachment.docId=doc.id ");
        if (filters.keySet().size() > 0) {
            for (String param : filters.keySet()) {
                if (param.equals("name")) {
                    f.format(" and upper(attachment.filename) like :name ");
                }
                if (param.equals("page")) {
                    f.format(" and upper(doc.fullName) like :page ");
                }
                if (param.equals("space")) {
                    f.format(" and upper(doc.space) like :space ");
                }
                if (param.equals("author")) {
                    f.format(" and upper(attachment.author) like :author ");
                }
            }
        }
        f.format(")");
        String queryString = f.toString();
        /* Execute the query by filling the parameters */
        List<Object> queryResult = null;
        try {
            Query query = queryManager.createQuery(queryString, Query.XWQL).setLimit(number).setOffset(start);
            for (String param : filters.keySet()) {
                query.bindValue(param, String.format("%%%s%%", filters.get(param).toUpperCase()));
            }
            queryResult = query.execute();
        } catch (QueryException e) {
            throw new XWikiRestException(e);
        }
        Set<String> acceptedMimeTypes = new HashSet<String>();
        if (!types.equals("")) {
            String[] acceptedMimetypesArray = types.split(",");
            for (String type : acceptedMimetypesArray) {
                acceptedMimeTypes.add(type);
            }
        }
        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;
            String pageSpaceId = (String) fields[0];
            List<String> pageSpaces = Utils.getSpacesFromSpaceId(pageSpaceId);
            String pageName = (String) fields[1];
            String pageId = Utils.getPageId(wikiName, pageSpaces, pageName);
            String pageVersion = (String) fields[2];
            XWikiAttachment xwikiAttachment = (XWikiAttachment) fields[3];
            String mimeType = xwikiAttachment.getMimeType(Utils.getXWikiContext(componentManager));
            boolean add = true;
            /* Check the mime type filter */
            if (acceptedMimeTypes.size() > 0) {
                add = false;
                for (String type : acceptedMimeTypes) {
                    if (mimeType.toUpperCase().contains(type.toUpperCase())) {
                        add = true;
                        break;
                    }
                }
            }
            if (add) {
                /*
                     * We manufacture attachments in place because we don't have all the data for calling the
                     * DomainObjectFactory method (doing so would require to retrieve an actual Document)
                     */
                Attachment attachment = objectFactory.createAttachment();
                attachment.setId(String.format("%s@%s", pageId, xwikiAttachment.getFilename()));
                attachment.setName(xwikiAttachment.getFilename());
                attachment.setLongSize(xwikiAttachment.getLongSize());
                // Retro compatibility
                attachment.setSize((int) xwikiAttachment.getLongSize());
                attachment.setMimeType(mimeType);
                attachment.setAuthor(xwikiAttachment.getAuthor());
                if (withPrettyNames) {
                    attachment.setAuthorName(Utils.getAuthorName(xwikiAttachment.getAuthorReference(), componentManager));
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(xwikiAttachment.getDate());
                attachment.setDate(calendar);
                attachment.setPageId(pageId);
                attachment.setPageVersion(pageVersion);
                attachment.setVersion(xwikiAttachment.getVersion());
                URL absoluteUrl = Utils.getXWikiContext(componentManager).getURLFactory().createAttachmentURL(xwikiAttachment.getFilename(), pageSpaceId, pageName, "download", null, wikiName, Utils.getXWikiContext(componentManager));
                attachment.setXwikiAbsoluteUrl(absoluteUrl.toString());
                attachment.setXwikiRelativeUrl(Utils.getXWikiContext(componentManager).getURLFactory().getURL(absoluteUrl, Utils.getXWikiContext(componentManager)));
                URI pageUri = Utils.createURI(uriInfo.getBaseUri(), PageResource.class, wikiName, pageSpaces, pageName);
                Link pageLink = objectFactory.createLink();
                pageLink.setHref(pageUri.toString());
                pageLink.setRel(Relations.PAGE);
                attachment.getLinks().add(pageLink);
                URI attachmentUri = Utils.createURI(uriInfo.getBaseUri(), AttachmentResource.class, wikiName, pageSpaces, pageName, xwikiAttachment.getFilename());
                Link attachmentLink = objectFactory.createLink();
                attachmentLink.setHref(attachmentUri.toString());
                attachmentLink.setRel(Relations.ATTACHMENT_DATA);
                attachment.getLinks().add(attachmentLink);
                attachments.getAttachments().add(attachment);
            }
        }
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
    return attachments;
}
Also used : Query(org.xwiki.query.Query) HashMap(java.util.HashMap) Formatter(java.util.Formatter) XWikiRestException(org.xwiki.rest.XWikiRestException) Calendar(java.util.Calendar) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachment(org.xwiki.rest.model.jaxb.Attachment) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) Attachments(org.xwiki.rest.model.jaxb.Attachments) URI(java.net.URI) URL(java.net.URL) QueryException(org.xwiki.query.QueryException) Link(org.xwiki.rest.model.jaxb.Link) HashSet(java.util.HashSet)

Example 100 with Formatter

use of java.util.Formatter in project xwiki-platform by xwiki.

the class BaseSearchResult method searchPages.

/**
 * Search for keyword in the given scopes. Limit the search only to Pages. Search for keyword
 *
 * @param keywords the string that will be used in a "like" XWQL clause.
 * @param number number of results to be returned.
 * @param start 0-based start offset.
 * @param orderField the field to be used to order the results.
 * @param order "asc" or "desc"
 * @return the results.
 */
protected List<SearchResult> searchPages(List<SearchScope> searchScopes, String keywords, String wikiName, String space, boolean hasProgrammingRights, int number, int start, String orderField, String order, Boolean withPrettyNames) throws QueryException, IllegalArgumentException, UriBuilderException, XWikiException {
    XWiki xwikiApi = Utils.getXWikiApi(componentManager);
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    /* This try is just needed for executing the finally clause. */
    try {
        List<SearchResult> result = new ArrayList<SearchResult>();
        if (keywords == null) {
            return result;
        }
        Formatter f = new Formatter();
        /*
             * If the order field is already one of the field hard coded in the base query, then do not add it to the
             * select clause.
             */
        String addColumn = "";
        if (!StringUtils.isBlank(orderField)) {
            addColumn = (orderField.equals("") || orderField.equals("fullName") || orderField.equals("name") || orderField.equals("space")) ? "" : ", doc." + orderField;
        }
        if (space != null) {
            f.format("select distinct doc.fullName, doc.space, doc.name, doc.language");
            f.format(addColumn);
            f.format(" from XWikiDocument as doc where doc.space = :space and ( ");
        } else {
            f.format("select distinct doc.fullName, doc.space, doc.name, doc.language");
            f.format(addColumn);
            f.format(" from XWikiDocument as doc where ( ");
        }
        /* Look for scopes related to pages */
        int acceptedScopes = 0;
        for (int i = 0; i < searchScopes.size(); i++) {
            SearchScope scope = searchScopes.get(i);
            switch(scope) {
                case CONTENT:
                    f.format("upper(doc.content) like :keywords ");
                    acceptedScopes++;
                    break;
                case NAME:
                    f.format("upper(doc.fullName) like :keywords ");
                    acceptedScopes++;
                    break;
                case TITLE:
                    f.format("upper(doc.title) like :keywords ");
                    acceptedScopes++;
                    break;
            }
            if (i != searchScopes.size() - 1) {
                f.format(" or ");
            }
        }
        /* If we don't find any scope related to pages then return empty results */
        if (acceptedScopes == 0) {
            return result;
        }
        /* Build the order clause. */
        String orderClause = null;
        if (StringUtils.isBlank(orderField)) {
            orderClause = "doc.fullName asc";
        } else {
            /* Check if the order parameter is a valid "asc" or "desc" string, otherwise use "asc" */
            if ("asc".equals(order) || "desc".equals(order)) {
                orderClause = String.format("doc.%s %s", orderField, order);
            } else {
                orderClause = String.format("doc.%s asc", orderField);
            }
        }
        // Add ordering
        f.format(") order by %s", orderClause);
        String query = f.toString();
        List<Object> queryResult = null;
        /* This is needed because if the :space placeholder is not in the query, setting it would cause an exception */
        if (space != null) {
            queryResult = this.queryManager.createQuery(query, Query.XWQL).bindValue("keywords", String.format("%%%s%%", keywords.toUpperCase())).bindValue("space", space).addFilter(Utils.getHiddenQueryFilter(this.componentManager)).setOffset(start).setLimit(number).execute();
        } else {
            queryResult = this.queryManager.createQuery(query, Query.XWQL).bindValue("keywords", String.format("%%%s%%", keywords.toUpperCase())).addFilter(Utils.getHiddenQueryFilter(this.componentManager)).setOffset(start).setLimit(number).execute();
        }
        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;
            String spaceId = (String) fields[1];
            List<String> spaces = Utils.getSpacesFromSpaceId(spaceId);
            String pageName = (String) fields[2];
            String language = (String) fields[3];
            String pageId = Utils.getPageId(wikiName, spaces, pageName);
            String pageFullName = Utils.getPageFullName(wikiName, spaces, pageName);
            /* Check if the user has the right to see the found document */
            if (xwikiApi.hasAccessLevel("view", pageId)) {
                Document doc = xwikiApi.getDocument(pageFullName);
                String title = doc.getDisplayTitle();
                SearchResult searchResult = objectFactory.createSearchResult();
                searchResult.setType("page");
                searchResult.setId(pageId);
                searchResult.setPageFullName(pageFullName);
                searchResult.setTitle(title);
                searchResult.setWiki(wikiName);
                searchResult.setSpace(spaceId);
                searchResult.setPageName(pageName);
                searchResult.setVersion(doc.getVersion());
                searchResult.setAuthor(doc.getAuthor());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(doc.getDate());
                searchResult.setModified(calendar);
                if (withPrettyNames) {
                    searchResult.setAuthorName(Utils.getAuthorName(doc.getAuthorReference(), componentManager));
                }
                String pageUri = null;
                if (StringUtils.isBlank(language)) {
                    pageUri = Utils.createURI(this.uriInfo.getBaseUri(), PageResource.class, wikiName, spaces, pageName).toString();
                } else {
                    searchResult.setLanguage(language);
                    pageUri = Utils.createURI(this.uriInfo.getBaseUri(), PageTranslationResource.class, wikiName, spaces, pageName, language).toString();
                }
                Link pageLink = new Link();
                pageLink.setHref(pageUri);
                pageLink.setRel(Relations.PAGE);
                searchResult.getLinks().add(pageLink);
                result.add(searchResult);
            }
        }
        return result;
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
}
Also used : Formatter(java.util.Formatter) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.api.XWiki) SearchResult(org.xwiki.rest.model.jaxb.SearchResult) Document(com.xpn.xwiki.api.Document) Link(org.xwiki.rest.model.jaxb.Link)

Aggregations

Formatter (java.util.Formatter)558 ArrayList (java.util.ArrayList)26 File (java.io.File)25 IOException (java.io.IOException)25 Date (java.util.Date)22 Test (org.junit.Test)19 HashMap (java.util.HashMap)16 Map (java.util.Map)16 AlertDialog (android.app.AlertDialog)14 DialogInterface (android.content.DialogInterface)14 MessageDigest (java.security.MessageDigest)14 PrintWriter (java.io.PrintWriter)13 Justif (aQute.lib.justif.Justif)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 Locale (java.util.Locale)11 BigInteger (java.math.BigInteger)10 PrintStream (java.io.PrintStream)9 Calendar (java.util.Calendar)7 LayoutBuilder (android.text.StaticLayoutTest.LayoutBuilder)6 View (android.view.View)6