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();
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations